我无法弄清楚为什么命令提示符正在跳过Visual Studio中的简单C ++程序的输入

时间:2018-03-31 20:30:51

标签: c++ visual-studio-2017

这是我遇到问题的程序,或者在Visual Studio中配置错误。

#include <iostream>

using namespace std;

int main()

{

    //define variables
    int name;
    int income;
    float tax;

    //Get the users name
    cou << "Please enter your full name \n";
    cin >> name;

    //Get the users income
    cout << "Please enter your annual income: \n";
    cin >> income;

    //tax conditions
    if (income < 50000)
    {
        cout << name; cout << "'s tax rate is 33% and the total income and amount taxed is: \n";
        tax = income * 0.33; 

        cout << income; cout << "dollars \n";
            cout << tax; cout << "dollars \n";
    }
    else
    {
        cout << name; cout << "'s tax rate is 38% and the total income and amount taxed is: \n";
        tax = income * 0.38;

        cout << income; cout << "dollars \n";
        cout << tax; cout << "dollars \n";
    }
system("pause");

    return 0;




}

它应该计算用户收入的税收。它做得很好,但只要我添加第二个cin&gt;&gt;命令提示符顶部的名称输入接受它作为输入,然后跳过第二个输入并输出一堆随机数字和字符。

这是命令提示符中的输出:

Please enter your full name:
Bob Steve
Please enter your annual income:
-858993460's tax rate is 33% and the total income and amount taxed is:
-858993460dollars
-2.83468e+08dollars
Press any key to continue . . .

第一次输入&#34; Bob Steve&#34;进入其余部分自动显示。我感谢您提供有关此问题的任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您尝试输入带有空格的单词,则会发生这种情况。 例如,如果您输入&#34; Bar Foo&#34;作为名称的输入,将跳过下一个输入。尝试将名称分为两个不同的变量,如&#34;字符串名字; string lastname;&#34;。 无论如何,我还看到你的代码中存在一些语法错误,名称data-type应该至少是一个字符数组或std :: string来存储人名。

03-31 21:12:01.635 1279-1279/? I/Name: ROB
03-31 21:12:01.635 1279-1279/? I/Age: 34
03-31 21:12:01.635 1279-1279/? I/id: 33
03-31 21:12:01.635 1279-1279/? I/house: home
03-31 21:12:01.635 1279-1279/? I/street: street
03-31 21:12:01.635 1279-1279/? I/Name: ROB
03-31 21:12:01.635 1279-1279/? I/Age: 34
03-31 21:12:01.635 1279-1279/? I/id: 33
03-31 21:12:01.635 1279-1279/? I/house: home
03-31 21:12:01.635 1279-1279/? I/street: street

您也可以使用std :: getline()来处理多间距输入。