私はベクトルに格納する名前のユーザー入力を取得しようとしています。 forループを実行するたびに、最初にループを介して一時変数の変数に空白の入力が常にかかります。 私はこのプログラムを実行するときに私が得ることであるようなもの:任意の助け空の文字列がループを介して最初に入力されましたか? (C++)
,
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++){}
}
コードに誤りがあります。 –
[最小完全な検証可能な例](http://stackoverflow.com/help/mcve)を入力してください。あなたのコードを投稿してください。 –
自分のコードをデバッガで実行しましたが、問題は115行目のステートメントです。デバッガを実行して正しいことを確認してください。 –