1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
// Usage for choice1: '1 FirstName LastName BirthYear ZipCode'
string input, str1, str2, str3, str4;
str1.clear(); str2.clear(); str3.clear(); str4.clear(); // To clear from previous iterations of
int choice = 0; // a while loop
getline(cin, input);
stringstream convert(input);
convert >> choice; // Puts only first character of "input" into an int (choice)
// Split input into individual strings
int numarg = 1; // Minimum number. I assume that there's no input error
int lastspace = 0; // by the user - which, for my purposes, is safe
int space = 0;
for (unsigned int i = 0; i < input.length(); i++) {
if (input[i] == ' ') {
numarg++;
if (!lastspace && !space) {
lastspace = space = i; // If it hasn't iterated at all, they'll both set to the first space
} // at input[1] - it's possible that this is a small problem
else {
space = i; // If lastspace is already set, only move space forward
} // for use below
if (numarg == 2) {
str1.assign(input, lastspace + 1, space - (lastspace + 1));
}
else if (numarg == 3) {
str2.assign(input, lastspace + 1, space - (lastspace + 1));
}
else if (numarg == 4) {
str3.assign(input, lastspace + 1, space - (lastspace + 1));
}
else if (numarg == 5) {
str4.assign(input, lastspace + 1, space - (lastspace + 1));
}
lastspace = space;
}
}
if (choice == 1) { // Add user
if (numarg != 5) { // It never gets caught here
cout << "\tError: Incorrect number of arguments." << endl;
}
else {
string name = string(str1) + string(str2); // Creates name in single string
int year, zip; // Temporary
stringstream birthyear(input);
birthyear >> year;
stringstream zipcode(input);
zipcode >> zip;
Network.add_user(name, year, zip);
}
}
|