第一次通过循环输入空字符串?(c++)
Blank string input first time through loop? (c++)
我正在尝试让用户输入的名称存储在向量中。每次我运行for循环时,它总是在第一次运行循环时在temp字符串变量中输入空白。当我运行这个程序时,我得到的是:
,
Dylan, Bob
Brown, Mark
Rogers, Mr
感谢的帮助
抱歉第一个帖子=p这是代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
void getNames(vector<string> &n, int&);
void displayNames(vector<string> &n, int&);
void saveNames(vector<string> &n, int&);
void readNames(vector<string> &n, int&);
void sortNames(vector<string> &n, int&);
int main(){
vector<string> names;
int b = 0;
int choice;
while(choice != 5){
cout << "1 - Enter a Character" << endl;
cout << "2 - Display List of Characters" << endl;
cout << "3 - Save List of Characters to File" << endl;
cout << "4 - Read List of Characters from File" << endl;
cout << "5 - Exit the program" << endl << endl;
cin >> choice;
switch(choice){
case 1: getNames(names, b); break;
case 2: displayNames(names, b); break;
case 3: saveNames(names, b); break;
case 4: readNames(names, b); break;
}
}
return 0;
}
void getNames(vector<string> &n, int &b){
string temp;
cout << "Enter Names. Press 'quit' to stop." << endl;
for(b; temp != "quit"; b++){
getline(cin, temp);
if(temp == "quit"){
break;
}else{
int space = temp.find(' ', 0);
string inPut = temp.substr((space+1), temp.length());
inPut += ", ";
inPut += temp.substr(0, space);
n.push_back(inPut);
}
}
}
void displayNames(vector<string> &n, int &b){
for(int c=0; c < b; c++){
cout << n[c] << endl;
}
}
void saveNames(vector<string> &n, int &b){
ofstream outFile;
outFile.open("names.txt", ios::app);
if(!outFile.fail()){
for(int i=0; i < b; i++){
outFile << n[i] << endl;
}
cout << "Names written to File" << endl;
}else{
cout << "ERROR: File could not be opened" << endl;
}
outFile.close();
}
void readNames(vector<string> &n, int&){
string line;
ifstream inFile("names.txt");
if(inFile.is_open()){
while(getline(inFile, line)){
cout << line << endl;
}
inFile.close();
}else{
cout << "Cannot open File" << endl;
}
}
void sortNames(vector<string> &n, int&){
//for(int i=0; i < b; i++){}
}
通过修复大括号,getNames()函数可以按预期工作。也许这是发布代码时的疏忽,但您缺少一个大括号来结束对GetNames()的for循环和函数调用。编译并使用此代码会产生预期的输出:
void getNames(vector<string> &n, int &b){
string temp;
cout << "Enter Names. Press 'quit' to stop." << endl;
for(b; temp != "quit"; b++){
getline(cin, temp);
if(temp == "quit"){
break;
}else{
int space = temp.find(' ', 0);
string inPut = temp.substr((space+1), temp.length());
inPut += ", ";
inPut += temp.substr(0, space);
cout << inPut << endl; //Testing string transformation before insertion.
n.push_back(inPut);
}
}
}
传递名称:"Bob Dylan"、"Jerry McQuire"answers"Some Guy"导致输出
Dylan, Bob
McQuire, Jerry
Guy, Some
如果Vector中的第一个元素始终为空,那么代码中的其他地方可能存在错误。错误可能在saveNames()或readNames(()函数中。但是字符串在这里被正确地输入和转换。提前发布与您的问题相关的所有代码是有益的。
请将其包含在getNames()的顶部:
cin.clear();
cin.ignore(10000, 'n');
这将清除cin缓冲区到一个新行,这样main()中的初始选择就不会意外地放入向量中。
为什么要使用for循环?最好使用这样的while循环:
void getNames(vector<string> &n, int &b)
{
string temp;
cout << "Enter Names. Press 'quit' to stop." << endl;
getline(cin, temp);
while(temp != "quit")
{
int space = temp.find(' ', 0);
string inPut = temp.substr((space+1), temp.length());
inPut += ", ";
inPut += temp.substr(0, space);
n.push_back(inPut);
getline(cin, temp);
}
...//the rest of your code...
}
我没有回答这个问题,对不起。。。第一个输入是最后一个cin的终止字符。。。std::getline(字符串)