Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;对于循环初学者,输出不正确_C++_Loops_For Loop_Int - Fatal编程技术网

C++ C++;对于循环初学者,输出不正确

C++ C++;对于循环初学者,输出不正确,c++,loops,for-loop,int,C++,Loops,For Loop,Int,我今年开始上大学,修了一门软件开发课程。我刚开始在C++中做循环,并且分配了一些问题要解决。我已经完成了第一个问题的代码,但部分输出无法正常工作,我无法找出原因 问题是在测试中读入10名学生的分数,然后输出获得荣誉分数(70分以上)的学生的分数 这是我的密码 int _tmain(int argc, _TCHAR* argv[]) { int grade; int numfirstclass = 0; int percentfirstclass; for

我今年开始上大学,修了一门软件开发课程。我刚开始在C++中做循环,并且分配了一些问题要解决。我已经完成了第一个问题的代码,但部分输出无法正常工作,我无法找出原因

问题是在测试中读入10名学生的分数,然后输出获得荣誉分数(70分以上)的学生的分数

这是我的密码

  int _tmain(int argc, _TCHAR* argv[])

{
    int grade;
    int numfirstclass = 0;
    int percentfirstclass;


    for (int count = 0; count < 10; count++)// For loop to run 10 times to allow 10 grades to be entered
    {
        cout << "Enter your grade ";
        cin >> grade;

        if (grade >= 70)
            numfirstclass++;
    }


    cout << "The number of students with a first class honours degree is:" <<  numfirstclass;
    percentfirstclass = (numfirstclass / 10) * 100;
    cout << endl << "The Percentage of students that recieved a first class degree is: " << percentfirstclass;



    return 0;
}
int-tmain(int-argc,_-TCHAR*argv[]
{
国际等级;
int numfirstclass=0;
一等舱;
for(int count=0;count<10;count++)//for循环运行10次以允许输入10个等级
{
cout>等级;
如果(等级>=70)
numfirstclass++;
}
不能使用

numfirstclass/10
将始终计算为0(除非
numfirstclass
为1),因为它是整数除法,并且乘以100和0始终为0


使用cast将使
numfirstclass/10(双精度)
生成一个具有小数部分的数字,然后将其乘以100。该数字将被分配给
percentfirstclass
,并且由于
percentfirstclass
是一个
int
,小数部分将被截断。

问题在于子表达式

(numfirstclass / 10)
percentfirstclass = (numfirstclass / 10) * 100;
表达方式

(numfirstclass / 10)
percentfirstclass = (numfirstclass / 10) * 100;
始终等于0,因为除了numfirstclass等于10的一种情况外,
numfirstclass
始终小于10。:)使用了整数算术

您可以将
numfirstclass
定义为具有类型float(或double),也可以将语句重写为

percentfirstclass = (numfirstclass * 100 ) / 10;
或强制将表达式计算为浮点数

percentfirstclass = (numfirstclass / 10.0) * 100;

正如cool guy告诉我们的,将percentfirstclass更改为浮点或双精度,在上面的代码中,您试图将一个小于10的整数除以10,因此程序返回0作为输出 i、 e

int c=1; int d=c/10;//给出0,因为整数不能支持小数

如果你使用

float d=c/10;//您将获得所需的输出


希望您能收到。

只需将
类型加倍。
.Write

double percentfirstclass;
percentfirstclass = (numfirstclass / 10.0) * 100;
反而

int percentfirstclass;
percentfirstclass = (numfirstclass / 10) * 100;

<代码> No.FixStCys/ 10 总是0,因为代码> NoMuthStCys< /C> >值> <代码> >值> 10。注意您正在执行<代码> int >代码>计算,将其改为<代码>双< /代码>,并让我们知道,如果您仍然有问题。您强迫C++评估“代码> NuxFristCype/10 < /Cord>”,这将是<代码> 0 < /代码>。小于10的数字(因为这些是整数)。乘以100太晚了。去掉括号,把
100
放在开头…或者乘以(100/10)。谢谢伙计们,我把它改成了双精度,现在可以工作了。对于noob的错误,我很抱歉。我现在明白了