[英]Input values of variable always outputting as 0 in C++
当我将用户输入存储在变量中并输出时,它给我的peoples()
函数一个值0。
#include <iostream>
using namespace std;
int number1, number2, result;
// My result should like this
void addison()
{
cout << "Enter first number:";
cin >> number1;
cout << "Enter second number:";
cin >> number2;
result = number1 + number2;
cout << number1 << "+" << number2 << "= " << result << endl;
}
int name, age;
// This is the problem and always giving value of variable as 0 after inputting value.
void peoples()
{
cout <<"Enter your name:";
cin >> name;
cout <<"Enter your age:";
cin >> age;
cout << "Your name is: " << name << " Your age is: " << age << endl;
}
int main()
{
int input;
cout<<"People: 1 \n";
cout<<"People: 2 \n";
cout<<"People: 3 \n";
cin>>input;
switch (input){
case 1:
peoples();
break;
case 2:
addison();
break;
case 3:
cout<<"This is people 3";
break;
default:
cout<<"You have entered Invalid Value";
break;
}
cin.ignore();
cin.get();
}
如果您不了解任何内容,请对此事发表评论。
将“名称”变量转换为字符串而不是整数。
int name, age; // This is the problem and always giving value of variable as 0 after inputting value. void peoples() { cout <<"Enter your name:"; cin >> name;
这是麻烦开始的地方。 name
是一个int
,没有意义。 它应该是一个std::string
。
当用户输入无法解析为整数的内容并尝试将其提取为int
, std::cin
设置为错误状态 。 此外,根据所使用的C ++版本,会发生以下情况:
name
。 name
设置为0。 参见http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt :
如果提取失败(例如,如果在预期的数字中输入了字母),则值将保持不变,并设置故障位。 (直到C ++ 11)
如果提取失败,则将零写入值并设置故障位。 (...) (自C ++ 11起)
让我们看看这对其余的代码意味着什么:
cout <<"Enter your age:"; cin >> age;
由于std::cin
仍处于错误状态,因此不会发生任何输入。 age
要么没有被修改(在C ++ 11之前),要么被设置为0(从C ++ 11开始)。
cout << "Your name is: " << name << " Your age is: " << age << endl;
在C ++ 11之前,这将导致不确定的行为,因为您从未初始化过name
和age
,并且在没有任何事先分配的情况下尝试打印它们意味着任何事情都可能发生。 在今天,实际上,您将“仅”获得看似随机的值,但是从理论上讲,您的程序也可能崩溃或执行其他所有奇怪的事情。
从C ++ 11开始,至少可以保证std::cin
提取运算符将变量设置为0,因此这里不再有未定义的行为。 可以说这会更好,因为您会立即发现出问题了。
无论哪种情况,解决方案都是在处理人工输入时不直接使用operator>>
。 当您给人类用户输入键盘并要求他们键入内容时,人类用户会犯错。
请改用std::getline
函数。 它将整行输入读入std::string
对象。 对于name
,如果您执行了我之前建议的操作并将变量转换为std::string
,这已经足够了。 它甚至可以允许名称中带有空格,例如“ Computer Lover”。
对于age
,请使用C ++ 11 std::stoi
函数将输入转换为int
并通过捕获可能引发的异常来处理输入错误的错误。
这是一个入门的简单示例:
int name;
std::string age;
void peoples()
{
std::cout <<"Enter your name:";
std::getline(std::cin, name);
std::cout <<"Enter your age:";
std::string age_as_string;
std::getline(std::cin, age_as_string);
try
{
age = std::stoi(age_as_string);
std::cout << "Your name is: " << name << " Your age is: " << age << "\n";
}
catch (std::exception const&)
{
// do something meaningful if the user entered
// something bad, e.g. "xxx"
}
}
当您使它起作用时,下一步将是放弃全局变量 。 它们与封装和结构化编程完全相反。
发生此问题的原因是,当name
应为string
时,已将其定义为int
。 像这样更改name
和age
的定义:
string name; //This should be string
int age; //This could be a string too, if you want
void peoples()
{
cout <<"Enter your name:";
cin >> name;
cout <<"Enter your age:";
cin >> age;
cout << "Your name is: " << name << " Your age is: " << age << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.