2016-03-29 5 views
-2

私はベクトルに格納する名前のユーザー入力を取得しようとしています。 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++){} 

} 
+2

コードに誤りがあります。 –

+0

[最小完全な検証可能な例](http://stackoverflow.com/help/mcve)を入力してください。あなたのコードを投稿してください。 –

+0

自分のコードをデバッガで実行しましたが、問題は115行目のステートメントです。デバッガを実行して正しいことを確認してください。 –

答えて

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); 
     } 
    } 
} 

名を渡す:コンパイルし、このコードを使用することが期待される出力生成「ボブ・ディラン」を、「ジェリーMcQuire」、および「いくつかの男が」出力の結果

Dylan, Bob 
McQuire, Jerry 
Guy, Some 

ことが可能ですVectorの最初の要素が常に空の場合は、コードのどこかにエラーがあることを示します。エラーは、おそらくsaveNames()またはreadNames()関数にあります。しかし、文字列はここで正しく入力され、変換されます。事前に質問に関連するすべてのコードを投稿すると有益です。 )(メインからの最初の選択はに置かれませんので、これは新しい行にCINバッファをクリアします

cin.clear(); 
cin.ignore(10000, '\n'); 

によりgetNamesの最上部()でこれを含めてください予期せずベクトル。

+0

ありがとう、ええ、私はちょうど中括弧が正しく私はちょうどここに正しくコピーしていない。私の投稿をフルコード –

+0

で編集します。ベクターを印刷するときは、0要素を印刷しないでください。空の値は、ユーザがオプション1-5の間で選択したときにmain()のcin文から来ます。 – Winter

+0

ありがとうございました!なぜcinの値はベクトルに格納されますか? –

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)

+0

ありがとう、 "最初の入力は終了文字..."とはどういう意味ですか? –

+0

@ Daniel Evans行の終わりに区切り文字を指定しないと、 "enter"(改行)と見なされるので、最後にcinを押すと最初の入力となる可能性があります。 –

関連する問題