当用户输入for循环c ++时,添加1乘1的数字 [英] Add 1 by 1 numbers when user input in for loop c++
本文介绍了当用户输入for循环c ++时,添加1乘1的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨每一个;
当用户输入数字1到30次时,我试图添加1 + 1,最后它必须拍摄总数。但总是我用来得到总数为2只我期待得到30.
Hi every one;
I am trying to add 1 + 1 when ever user inputs number 1 upto 30 times and finally it has to shot the total . but always i use to get the total as number 2 only i am expecting to get 30.
// Student attendence
#include <iostream>
#include <string>
#include <conio.h>
#include <limits>
using namespace std;
int main ()
{
cout<<"Enter Attendance for CP : ";
int cptotal;
for(int i=0;i<31;i++){
cin>>cptotal;
if(cptotal==1)
cptotal=cptotal+1;
}
cout<<cptotal;
}
return 0;
}
推荐答案
执行cin>> cptotal
并且用户输入有效数字,之前的cptotal
值将丢失。对于递增值的输入和累积,您可以使用太多不同的变量。
(顺便说一句,许多C ++开发人员在看到cptotal = cptotal + 1;
时会大发雷霆,当++ cptotal
或cptotal ++
可以使用时看起来太陌生了。)
-SA
When you executecin>>cptotal
and the user enters a valid number, the previous value ofcptotal
is lost. You can have too different variables for input and accumulation of incremented values.
(By the way, many C++ developers will go furious when they seecptotal=cptotal+1;
, which looks way too foreign when++cptotal
orcptotal++
can be used.)
—SA
这篇关于当用户输入for循环c ++时,添加1乘1的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文