I'm trying to get user input of names to store in a vector. Every time I run through the for loop, it always takes blank input in the temp string variable the first time through the loop. What I get when I run this program is something like:
,
Dylan, Bob
Brown, Mark
Rogers, Mr
Thanks for any help
Sorry first post =P Heres the code:
#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++){}
}
 
     
    