[英]C loop skipping an iteration over input
It is a very simple program, for calculating the cgpa. 这是一个非常简单的程序,用于计算cgpa。 But loop is skipping an iteration over input and call default for every that skipped iteration.
但是循环正在跳过对输入的迭代,并为每次跳过的迭代调用默认值。
#include <stdio.h>
#include <stdlib.h>
int main(){
float A=4.0, b=3.50, B=3.0, c=2.50,C=2.0;
float cgpa=0;
char grade;
for (int i=0; i<5;i++){
printf("\nEnter grade of your subject:\n");
grade = getchar( );
switch(grade){
case 'A':
cgpa=cgpa+A;
break;
case 'b':
cgpa=cgpa+b;
break;
case 'B':
cgpa=cgpa+B;
break;
case 'c':
cgpa=cgpa+c;
break;
case 'C':
cgpa=cgpa+C;
break;
default:
printf("\nSorry you have entered a wrong value please try again\n");
}}
printf("\n Your cgpa is:%f", cgpa/5);
return 0;
}
When you are calling getchar()
, you are getting two chars from the command line (f.ex. A\\n
), but you are only fetching one at a time. 当你调用
getchar()
,你从命令行得到两个字符(f.ex. A\\n
),但你一次只能获取一个字符。 Thus you get the \\n
the second time you are looping. 因此,您第二次循环时会得到
\\n
。 Fix? 固定? Do
getchar()
again after your switch
-case while omitting the result. 在
switch
-case后再次执行getchar()
,同时省略结果。
Furthermore, getchar()
returns an int
-value, but you are making a char
out of it, which can lead to strange behaviour. 此外,
getchar()
返回一个int
值,但是你正在制作一个char
,这会导致奇怪的行为。
When you enter a character and hit enter, character gets accepted by getchar()
and when the loop starts again \\n
is accepted because of the enter key you have given in the previous iteration, so.... Everytime you enter a character and hit enter... two inputs are taken and thus, this behaviour is observed. 当你输入一个字符然后按Enter键时,字符会被
getchar()
接受,并且当循环重新开始时,由于你在上一次迭代中给出的回车键,接受了\\n
,所以......每当你输入一个字符时点击输入...两个输入被采用,因此,观察到这种行为。 To overcome this, use 要克服这一点,请使用
scanf(" %c",&grade);
Note : Here a space
is given in front of %c
to omit empty spaces 注意: 这里在
%c
前面给出一个space
以省略空格
Instead of: 代替:
grade=getchar()
In the loop. 在循环。
the following code compiles cleanly, and properly performs the desired algorithm and handles white space, like newlines and invalid grade values. 以下代码干净地编译,并正确执行所需的算法并处理空格,如换行符和无效的等级值。
Notice the ease of readability when appropriate indenting and code block separation are implemented 请注意,在实现适当的缩进和代码块分离时,易于阅读
#include <stdio.h> // printf(), getchar()
#include <ctype.h> // toupper()
// MAX_GRADES makes it easy to adjust number of 'grade' inputs
#define MAX_GRADES (5)
int main()
{
// list variables one per line
// and it would be better if these were #defines
// instead of taking up stack space
float A=4.0f;
float b=3.50f;
float B=3.0f;
float c=2.50f;
float C=2.0f;
float cgpa=0.0f;
int grade; // << getchar() returns an integer not a character
for (int i=0; i<MAX_GRADES; i++)
{
printf("\nEnter grade of your subject:\n");
grade = getchar( );
if( 'A' <= toupper(grade) && 'Z' >= toupper(grade) )
{
switch(grade)
{
case 'A':
cgpa=cgpa+A;
break;
case 'b':
cgpa=cgpa+b;
break;
case 'B':
cgpa=cgpa+B;
break;
case 'c':
cgpa=cgpa+c;
break;
case 'C':
cgpa=cgpa+C;
break;
default:
printf("\nSorry you have entered a wrong value please try again\n");
i--; // adjust to not count bad input
break;
} // end switch
}
else
{
i--; // adjust to not count newline, non-alpha inputs, etc
}
} // end for
printf("\n Your cgpa is:%f", cgpa/MAX_GRADES);
return 0;
} // end function: main
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.