[Dev-C++] The strange action of getchar( ).
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <per...@ya...> - 2004-06-20 03:37:00
|
My program is list belove:
/* *********************************************** */
#include <stdio.h>
#define MAXVEX 8
main()
{
char ga[MAXVEX];
int i, v1, v2, vexnum, edgenum;
printf("Input the number of vertex:");
scanf("%d", &vexnum);
getchar(); /* sentence 1 */
printf("Input the information of vertex:");
for (i = 1; i <= vexnum; i++) {
ga[i] = getchar();
}
printf("Input the number of edge:");
scanf("%d", &edgenum); /* sentence 2 */
printf("The pair of the vertvex in the edge:");
for (i = 1; i <= edgenum; i++) {
scanf("%d%d", &v1, &v2); /* sentence 3 */
}
system("PAUSE");
}
/* ****************************************** */
If I remove the sentence 1 from the program.The result is list here:
/* *********result 1************************ */
Input the number of vertex:2
Input the information of vertex:ab
Input the number of edge:The pair of the vertvex in the edge:
/* ********************************* */
It seem that the sentence 2 and 3 are not executed.If I put the sentence 1 to the original place,the program is result this:
/* ************result 2********************* */
Input the number of vertex:2
Input the information of vertex:ab
Input the number of edge:1
The pair of the vertvex in the edge:1 2
Please press any key to continue...
/* ********************************* */
This result 2 I think it it is right.Is it the problem caused by the first getchar( ) where is sentence 1? Why I remove the sentence 1 will bring the issue?Up to now I still don't know why.
Thanks,
Bruce .J Sam
---------------------------------
Do You Yahoo!?
嫌邮箱太小?雅虎电邮自助扩容! |