在C ++中变量的输入值始终输出为0
问题描述:
当我将用户输入存储在变量中并输出时, peoples()
函数的值为0.
When I store user input in a variable and output it it gives a value of 0 for my peoples()
function.
#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();
}
如果您不了解任何内容,请对此事发表评论.
If anythings you don't understand just comment the matter.
答
将名称"变量转换为不是整数的字符串.
Turn the "name" variable into a string not an int.