*sigh* I thought I was so clever....I've now been shot down terribly by my
evil assignment.
On to business - I hashed together the start of my main menu, using the
previous idea's and alot of google stuff.
Now I thought it was working <never trust an error free compile> however
have realised that converting my string to an integer is giving me the wrong
integer.
I'm assuming that is because of how I've read, tokenised and converted the
integer.
So if anyone has a quick minute to check out my code below and perhaps
suggest a less convuluted method, or point out an error - I would be very
grateful.
Just a note - the code below is only the snippet for the main menu - so it's
not going to compile. If the working code is wanted just let me know and
I'll post it.
/*Main Menu
Assumptions : Either 1, 2 or 3 paramaters will be used only. First
parameter will always be a char, 2nd always an int, 3rd always a string
*/
string buf;
string param1, temp, param3;
int param2;
string answer;
do{
cout << "Welcome to my Transport Database. \n";
cout << "Please enter a command (For help - type 'H'): ";
cin >> answer;
stringstream ss(answer); //Insert string into stream
vector<string> tokens; //Create vector to hold words
vector<string>::iterator strIter; //Creates an iterator for the
vector
while (ss >> buf)
tokens.push_back(buf); //When hits whitespace, put word into vector
for (strIter = tokens.begin(); strIter != tokens.end(); strIter++)
if (int (tokens.size()) == 1) { //If 1 paramater assign to
param1
param1 = tokens.at(0);
}
else if (int (tokens.size()) == 2) { //If 2 paramaters assign to
param1 & 2
param1 = tokens.at(0);
temp = tokens.at(1);
const char *ch = temp.c_str(); //Convert string to c-style
string
param2 = atoi(ch); //Convert c_string to int
}
else if (int (tokens.size()) == 3) { //If 3 paramaters assign to
param1, 2 &3
param1 = tokens.at(0);
temp = tokens.at(1);
const char *ch = temp.c_str();
param2 = atoi(ch);
param3 = tokens.at(2);
}
if (param1 == "H")
getHelp();
if (param1 == "A")
car->addById(param2); //Add new Vehicle with the given
parameter as it's Id
}while(param1 != "Q");
"Gemma Fletcher" <sl*****@iinet.net.au> wrote in message
news:44***********************@per-qv1-newsreader-01.iinet.net.au...
Thanks guys!!
You were both very helpful :)
"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e4**********@news-int.gatech.edu... Gemma Fletcher <sl*****@iinet.net.au> wrote: For eg:
L <vehicleId>
or
A <vehicleId> <Type>
So the user inputing L 123 is asking to list the vehicle who's Id is
123. Or
A 123 Ship, is asking to add a vehicle with an id of 123 and type ship.
However since all the menu commands are a mixture of just the letter (H
for
help) or a command with 1 or 2 pararmeters, I can't really declare 3
input
variables and go: cin >> letter >> Id >> type, as when i run the program
I
have to enter something for each variable field.
I thought maybe instead i would read in a string and then search the
string
for the variables instead. However I'm not quite sure how to go about
it.
You can use std::getline() (found in <string>) to read in a whole line.
I though maybe using the string class I would use the find() method,
however
I'm not sure how to tell the program that once it finds (for example)
the
start of a number, to read the rest of the number and save it in a temp
variable to be passed to my vehicleId variable.
If you know where the number starts and ends, you can use string's
substr() function to get a substring. Then, you can convert this
substring into an actual number.
To convert a string to a number, see
http://www.parashift.com/c++-faq-lit....html#faq-39.2
and the FAQ following it too.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply