如何在我的程序中显示cin答案?

时间:2014-09-10 06:56:59

标签: c++ visual-studio-2012

cout <<"\n\nYour name ?:" ;

cin >> name;

getline(cin,name);

cout <<"Hello"<<name;

3 个答案:

答案 0 :(得分:3)

试试此代码

//declaring header mains
#include <iostream>
#include <string>
using namespace std;

//Method: The main method
//Purpose: To get the users name, age, and money; and return the information
//Parameters: Name as string, age as integer, and money as a float value
//Returns: Users inputted name, age, and money
int main()
{
    //Declaring variables
    string fullName;
    int age;
    float money;

    //Promt the user to enter their age
    cout << "\nPlease enter your age: ";
    cin >> age;

    //Prompt the user to enter their amount of money
    cout << "\nPlease tell me how much money you have: ";
    cin >> money;

    //Prompt the user to enter their full name
    cout << "\nPlease enter your full name: ";
    getline( cin, fullName);

    //Display the name back to the user; and adds another line
    cout << "\n" << fullName << endl;

    //Display the age back to the user
    cout << "\nYou are " << age;  
    cout << " years old; ";

    //Display the amount of money back to the user
    cout << "\nand you have $" << money;
    cout << " in your pocket.";

    //Giving the user an ending message
    cout << "\nGoodbye .....\n";

    //Keeping the window open
    system("PAUSE");

    // and finally, return zero
    return 0;
}

输出是 - &gt;

Please enter your age: 12
Please tell me how much money you have: 10
Please enter your full name: ABC XYZ
ABC XYZ
You are 12 years old;
and you have $10 in your pocket.
Goodbye .....
Press any key to continue . . .

答案 1 :(得分:1)

请从代码中删除"cin >> name;"。 并尝试在没有它的情况下运行它 感谢

答案 2 :(得分:0)

删除getline(),你不需要cin&gt;&gt; name和getline,删除其中一个,更喜欢getline,因为cin行是在c ++中获取东西的方法