如何在程序中显示cin答案
How do I display cin answer in my program?
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++中获取内容的方法
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++中的cin.ignore()函数不适用于整个流
- 程序崩溃并显示"std::out_of_range"错误
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- Problems with std::cin.fail()
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 使用 CIN COUT 显示垃圾值的文件对象?
- 执行时间以C++显示:cin/scanf/getchar_unlocked()
- CIN函数不显示本地回声
- 如何在程序中显示cin答案
- 如何使用cin读取字符数据以存储在数组中并显示
- while(cin)循环双显示
- 为什么int8_t和用户通过cin输入显示奇怪的结果