如何在程序中显示cin答案

How do I display cin answer in my program?

本文关键字:显示 cin 答案 程序      更新时间:2023-10-16
cout <<"nnYour name ?:" ;
cin >> name;
getline(cin,name);
cout <<"Hello"<<name;

试试这个代码

//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;
}

输出为-->

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 . . .
请从代码中删除"cin >> name;"。试着在没有它的情况下运行它。感谢

删除getline(),您不需要cin>>名称和getline,然后删除其中一个,最好是getline,因为cin行是在c++中获取内容的方法