如何让用户在 c++ 中选择一个数字

How do i get a user to pick a number in c++

本文关键字:数字 一个 选择 用户 c++      更新时间:2023-10-16

我试图让用户选择一个数字,导致C ++中的另一个代码,我该怎么做?

cout << "Choose a text: "
getline(????)
1:
code number one
2.
text number 2

选择一个数字? 我假设您正在谈论输入一个整数。 Getline主要用于每个单词或单词之间的字符串和空格。 例如,如果输入类似于"快乐感谢给予" 代码如下所示:

#include <iostream>
#include <string> //to use getline(cin, variable_name)
using namespace std;
int main()
{
string sentenceWithSpace;
cout << "get input" << endl;
getline(cin,sentenceWithSpace);
cout << sentenceWithSpace << endl;
system("pause"); // include this line if you use VS
return 0;
}

如果用户只是输入一个值,如 1,2,3,4,5,6

#include <iostream>
using namespace std;
int main()
{
int thisIsJustAInteger;
cout << "get input" << endl;
cin >> thisIsJustAInteger;
cout << thisIsJustAInteger << endl;
system("pause"); // include this line if you use VS
return 0;
}
相关文章: