在C ++中变量的输入值始终输出为0 [英] Input values of variable always outputting as 0 in C++

查看:54
本文介绍了在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.

这篇关于在C ++中变量的输入值始终输出为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆