早些时候我发布了一个关于cin跳过输入的问题,我得到了刷新和使用的结果istringstream,但现在我尝试了所有可能的解决方案,但它们都没有工作.
这是我的代码:
void createNewCustomer () {
string name, address;
cout << "Creating a new customer..." << endl;
cout << "Enter the customer's name: "; getline(cin, name);
cout << "Enter the customer's address: "; getline(cin, address);
Customer c(name, address, 0);
CustomerDB::addCustomer(c);
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
但我仍然得到同样的东西,跳过输入,当它确实需要输入时,它需要它们并且名称中的存储空白,并且在地址中它采用我在名称中写的但是从第二个字母到结尾
我的代码出了什么问题?
我尝试了cin.ignore(),cin.get()和cin.clear()所有的人一起,独自一人,没有一次成功
编辑:
main.cpp中的main方法mainMenu()只调用
void mainMenu () {
char choice;
do {
system("cls");
mainMenuDisplay();
cin >> choice;
system("cls");
switch (choice) {
case '1': …Run Code Online (Sandbox Code Playgroud) 我需要以下程序来获取整行用户输入并将其放入字符串名称:
cout << "Enter the number: ";
int number;
cin >> number;
cout << "Enter names: ";
string names;
getline(cin, names);
Run Code Online (Sandbox Code Playgroud)
然而,使用cin >> number命令之前的getline()命令(我猜这是问题),它不允许我输入名称.为什么?
我听说过一个关于cin.clear()命令的事情,但我不知道这是如何工作的,或者为什么这甚至是必要的.
我试图从控制台获取一些用户输入参数,两个字符串,两个整数和一个double.我试图使用的相关代码是:
#include <string>
#include <iostream>
using namespace std;
// ...
string inputString;
unsigned int inputUInt;
double inputDouble;
// ...
cout << "Title: ";
getline(cin, inputString);
tempDVD.setTitle(inputString);
cout << "Category: ";
getline(cin, inputString);
tempDVD.setCategory(inputString);
cout << "Duration (minutes): ";
cin >> inputUInt;
tempDVD.setDuration(inputUInt);
cout << "Year: ";
cin >> inputUInt;
tempDVD.setYear(inputUInt);
cout << "Price: $";
cin >> inputDouble;
tempDVD.setPrice(inputDouble);
Run Code Online (Sandbox Code Playgroud)
但是,在运行程序时,代码不会等待输入第一个inputString,而是在第二次getline()调用之前代码不会停止.因此控制台输出如下所示:
标题:类别:
光标出现在类别之后.如果我现在输入,程序然后跳转到年份输入,不允许我输入多个字符串.这里发生了什么事?
是否有一个原因,如果在我的程序中我要求用户输入,我这样做:
int number;
string str;
int accountNumber;
cout << "Enter number:";
cin >> number;
cout << "Enter name:";
getline(cin, str);
cout << "Enter account number:";
cin >> accountNumber;
Run Code Online (Sandbox Code Playgroud)
为什么在输入第一个数字后,它输出"输入名称",然后立即输入"输入账号",然后我甚至输入我的"str"为getline(cin,str)线?谢谢!
有什么好的理由可以解释:
std::string input;
std::getline(std::cin, input);
Run Code Online (Sandbox Code Playgroud)
getline调用不会等待用户输入?cin的状态是否以某种方式搞砸了?
如果我使用下面的代码,getline不会接受最后一个输入(对于"for"循环的最后一次迭代,它只是跳过它) -
int main()
{
int n;
map<string, set<string> > lst;
string c,s,c2;
cin>>n;
for(int i=0;i<n;i++)
{
getline(cin,c); // here it skips the input for last iteration
stringstream ss;
ss<<c;
bool f=1;
while(ss>>s)
{
if(f)
{
c2=s;
f=0;
}
else
lst[c2].insert(s);
}
}
for (map<string, set<string> >::const_iterator ci = lst.begin(); ci != lst.end(); ++ci)
{
cout<< (*ci).first <<" "<< (*ci).second.size() <<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
为了摆脱它,我在getline之后放了cin.ignore().现在它采取了所有的投入,但我正面临一个新的问题 -
#include<iostream>
#include<string>
#include<map>
#include<set>
#include<sstream>
#include<algorithm>
using namespace std;
int main()
{
int n;
map<string, …Run Code Online (Sandbox Code Playgroud) 我正在学习structC++,但我不知道如何让它不跳过第一个问题。
如果我在其中使用 charstruct它只会复制我的第一个单词,如果我尝试使用 string 它会跳过第一个问题(忽略它)。
你能指导我吗?
后来编辑:我使用Microsoft Visual C ++ 2010 Express Edition的,并且是有之间的差异std::getline和cin.getline?
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
struct melodie{
char artist[50];
char titlu[50];
int an;
int lungime;
};
void main(){
int repetam;
double rezultat;
cout<<"Cate melodii veti introduce: ";
cin>>repetam;
melodie *pointer = new melodie[repetam];
for(int i = 0; i<repetam; i++){
cout<<endl;
cout<<"Introduceti artistul melodiei: ";
cin.get(pointer[i].artist);
cout<<"Introduceti titlul melodiei: ";
cin.getline(pointer[i].titlu);
cout<<"Introduceti anul melodiei: ";
cin>>pointer[i].an; …Run Code Online (Sandbox Code Playgroud) 为什么我们需要cin.ignore()在字符串中输入之前使用?
反手过程是怎样的?getline如果我们不使用 ,为什么它会跳过字符串中的输入(如果我们调用函数来获取更多变量) cin.ignore()?
他们要我声明 3 个变量,一个是整数,一个是双精度,一个是字符串。然后从标准输入读取 3 行输入。我已经发布了我的解决方案,但它不起作用。我不知道为什么我的字符串变量没有从标准输入读取。当我在 VSCODE 中尝试时,它正在工作。你能告诉我我做错了什么吗?
这是问题所在
样本输入:
12
4.0
is the best place to learn and practice coding!
Run Code Online (Sandbox Code Playgroud)
示例输出:
16
8.0
HackerRank is the best place to learn and practice coding!
Run Code Online (Sandbox Code Playgroud)
这是我用来检查我的 VSCODE 的代码。这有效!但它在 HackerRank 网站上不起作用。
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
int i = 4;
double d = 4.0;
string s = "HackerRank ";
// Declare second integer, double, and String variables.int x;
int x;
double y;
string str;
// …Run Code Online (Sandbox Code Playgroud) 我有一个小问题。我创建了一个程序,要求用户输入四个不同零件的零件名称和零件价格。每个名称和价格都填充一个结构,我有一个包含四个结构的数组。当我执行 for 循环来填充所有名称和价格时,我的 getline 函数无法正常工作,它只是在我输入第一部分的名称后跳过输入部分。你能告诉我为什么吗?这是我的代码:
#include <iostream>
#include <string>
struct part {
std::string name;
double cost;
};
int main() {
const int size = 4;
part apart[size];
for (int i = 0; i < size; i++) {
std::cout << "Enter the name of part ? " << i + 1 << ": ";
getline(std::cin,apart[i].name);
std::cout << "Enter the price of '" << apart[i].name << "': ";
std::cin >> apart[i].cost;
}
}
Run Code Online (Sandbox Code Playgroud)