我正在尝试将用户输入的名称存储在矢量中。 每次我运行for循环时,它总是在第一次循环时在临时字符串变量中输入空白。 我运行这个程序时得到的结果如下:
,
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++){}
}
答案 0 :(得分:0)
为什么使用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 (string)
答案 1 :(得分:0)
通过修复花括号,你的getNames()函数可以按预期工作。也许这是发布你的代码的疏忽,但你缺少一个大括号来结束你的for循环和函数调用GetNames()。编译和使用此代码会产生预期的输出:
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”和“Some Guy”导致输出
Dylan, Bob
McQuire, Jerry
Guy, Some
如果Vector中的第一个元素始终为空,则代码中的其他位置可能存在错误。该错误可能在saveNames()或readNames()函数中。但是这里输入并正确转换字符串。提前发布与您的问题相关的所有代码是有益的。
请将此信息包含在getNames()的顶部:
cin.clear();
cin.ignore(10000, '\n');
这会将cin缓冲区清除为新行,因此main()的初始选择不会意外地放入向量中。