[英]How to format input in C
I need help figuring out how to take input in specific format in C {1,2,3,4}
// that is right input - numbers in {} separated by comma.我需要帮助弄清楚如何以特定格式输入 C {1,2,3,4}
// 这是正确的输入 - {} 中的数字用逗号分隔。
znamky = (int*) malloc (n * sizeof(int)); // I allocate memory here
if (znamky == NULL)
return EXIT_FAILURE;
printf("Pocty bodu:\n");
scanf("%c",&overeni[0]); // here I check if the first character is {; if not I exit program
if (overeni[0]!=zavorka[0])
{
printf("Nespravny vstup.\n");
return EXIT_FAILURE;
}
while (scanf("%d",&znamky[count])!=EOF) //here I continue loading numbers, and char fight
{ //if the character after number is "," i load another
if (scanf("%c",&znaminko[0])==1) //number, if it is "}"I exit the while cycle
{ // else I exit the program
if (znaminko[0]==carka[0]) // if equals ","
{
printf("sdf\n");
count++;
if (count==n) // if I run out of memory I allocate more
{
n = n*2;
znamky = (int*) realloc (znamky, n * sizeof(int) + 4);
}
continue;
}
if (znaminko[0]==zavorka2[0]) // if equals "}"
count++;
printf("utikam\n");
break;
}
else
{
printf("Nespravny vstup\n");
return EXIT_FAILURE;
}
}
printf("%d ",count); //here I just print length of my array
printArray(znamky,count);
free(znamky);
return 0;
My problem is this: when I run the code and give it right input, it works just fine, but if I for example input {2 and press enter it thinks, that the input is equal "}"and exits the cycle.我的问题是:当我运行代码并给它正确的输入时,它工作得很好,但是如果我输入 {2 并按 enter 它认为,输入等于“}”并退出循环。 Why does it think that?为什么会这样想? Above when I compare char to "{" it seems to be working fine... Also any tips on how to improve the code are welcome, I am still new to programming so I don't see all the obvious mistakes.上面当我将 char 与“{”进行比较时,它似乎工作正常......也欢迎任何有关如何改进代码的提示,我对编程仍然是新手,所以我看不到所有明显的错误。 Thanks a lot.非常感谢。
My problem is this: when I run the code and give it right input, it works just fine, but if I for example input {2 and press enter it thinks, that the input is equal "}"and exits the cycle.我的问题是:当我运行代码并给它正确的输入时,它工作得很好,但是如果我输入 {2 并按 enter 它认为,输入等于“}”并退出循环。 Why does it think that?为什么会这样想? Above when I compare char to "{" it seems to be working fine...在上面,当我将 char 与“{”进行比较时,它似乎工作正常......
Also any tips on how to improve the code are welcome, I am still new to programming so I don't see all the obvious mistakes.也欢迎任何关于如何改进代码的提示,我还是编程新手,所以我看不到所有明显的错误。
but if I for example input {2 and press enter it thinks, that the input is equal "}"但是如果我例如输入 {2 并按回车键它认为,输入等于“}”
The problem is here:问题在这里:
if (znaminko[0]==zavorka2[0]) // if equals "}"
count++;
printf("utikam\n");
break;
it should be:它应该是:
if (znaminko[0]==zavorka2[0]) // if equals "}"
{
count++;
printf("utikam\n");
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.