C++ 变量的输入值在C+;中始终输出为0+;

C++ 变量的输入值在C+;中始终输出为0+;,c++,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 << "E

当我将用户输入存储在变量中并输出它时,我的
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();
}
#包括
使用名称空间std;
整数1,数2,结果;
//我的成绩应该是这样的
void addison()
{
cout>1号;
cout>2号;
结果=1号+2号;

无法将“name”变量转换为字符串而不是int。

出现此问题的原因是您已将
name
定义为
int
,而该变量应为
string
。请更改
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;
}
string name;//这应该是字符串
int age;//如果需要,也可以是字符串
无效民族()
{
姓名;
年龄;
库特
由于
std::cin
仍处于错误状态,因此不会发生任何输入。
age
未修改(在C++11之前)或设置为0(从C++11开始)



当这项工作开始时,下一步将是放弃全局变量。它们与封装和结构化编程完全相反。

请在此处发布代码、输入、预期输出和观察到的输出。我猜您可以将
名称
作为
字符串
输入,尽管它是s定义为
int
@ComputerLover不客气。请不要回答通过另一个站点的链接提供代码的问题。如果该链接被破坏,您的答案将毫无意义。好的,谢谢您的建议,但我现在确实尝试添加代码,但我做不到。出现了comi总是有错误信息。非常感谢你提供了关于这些代码的全部细节!@ComputerLover:不客气。重点(我想)如果你只使用了诸如“代码> STD:String 或之类的所有便利,那么C++就非常容易使用。当你必须处理流错误状态或类似的事情的复杂性时,这通常是你的代码不使用正确工具的标志。再次感谢你我要告诉你的事情。那是我用名字在键盘上输入值的时候。那时我不能用全名。我的意思是如果我在我的名字之间用空格,那么它就不能正常工作。那么我该怎么处理呢?我的意思是你回答了,但我需要用正常的方式工作。可能吗?@ComputerLover:如果用“正常”你的意思是
operator>
,那么不,这是不可能的,因为空格分隔输入。(有一种方法可以更改空格的定义,但当你不熟悉这种语言时,这不是你想知道或做的事情,即使你不是新的,这也比其他任何事情都更像是一种黑客行为。)对于
getline()
编译代码时出现函数错误:调用“getline(std::istream&,int&)”时没有匹配的函数|
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 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"
    }
}