473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Reading unser input & deciding course of action.

Hi all :)

I'm implementing a menu system for my assignment.

User inputs command & an if/else switch statement executes the command.

I'm just not quite sure the best way to take user input.

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.

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.

Any suggestions would be appreciated,

hopefully I'm somewhat coherent - coding for hours makes my brain mushy ;)

Thanks,
Gemma

May 15 '06 #1
8 1579
Gemma Fletcher wrote:
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.


Look for the spaces that separate (delimit) the input values (tokens).

Look for:
(0+)(token)(1+)(token)(1+)(token)(0+)(eol)

where:

0+ is 0 or more whitespace characters

1+ is 1 or more whitespace characters

token is a sequence of non-whitespace characters

eol is end of line

looking for the leading 0+ and the trailing 0+ is optional.

You can worry about what the tokens represent after you get them all.
May 15 '06 #2
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
May 15 '06 #3
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

May 18 '06 #4
*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


May 18 '06 #5
Please do not top-post.
http://en.wikipedia.org/wiki/Top-posting

Gemma Fletcher <sl*****@iinet.net.au> wrote:
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.
If you have errors, then it would be helpful to provide your input, your
expected output, and the actual output.
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.
Please follow the guidelines given at:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
/*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
There is no curly-brace '{' here, so the tokens.push_back(buf) is the
only statement that is executed in the while loop. Your indentation
below is therefore misleading.
for (strIter = tokens.begin(); strIter != tokens.end(); strIter++)
I do not see a need for this 'for' loop. For example, if tokens.size()
is 3 then you unnecessarily read in the parameters three times.
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");


--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 18 '06 #6

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e4**********@news-int2.gatech.edu...
Please do not top-post.
http://en.wikipedia.org/wiki/Top-posting

Sorry - Many apoligies.
Erm...shall I repost properly? Or just leave it since the damage is done?

Gemma
Gemma Fletcher <sl*****@iinet.net.au> wrote:
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.


If you have errors, then it would be helpful to provide your input, your
expected output, and the actual output.
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.


Please follow the guidelines given at:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
/*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


There is no curly-brace '{' here, so the tokens.push_back(buf) is the
only statement that is executed in the while loop. Your indentation
below is therefore misleading.
for (strIter = tokens.begin(); strIter != tokens.end(); strIter++)


I do not see a need for this 'for' loop. For example, if tokens.size()
is 3 then you unnecessarily read in the parameters three times.
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");


--
Marcus Kwok
Replace 'invalid' with 'net' to reply

May 18 '06 #7
Gemma Fletcher <sl*****@iinet.net.au> wrote:
"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e4**********@news-int2.gatech.edu...
Please do not top-post.
http://en.wikipedia.org/wiki/Top-posting


Sorry - Many apoligies.
Erm...shall I repost properly? Or just leave it since the damage is done?


No need to repost the previous message, but if could post another
message following the guidelines at:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


then you will be more likely to receive more useful responses. In other
words, try to simplify your code to the smallest complete example that
demonstrates your problem. Often, in the process of doing this, you may
be able to figure out the problem on your own.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 18 '06 #8
>> Sorry - Many apoligies.
Erm...shall I repost properly? Or just leave it since the damage is done?


No need to repost the previous message, but if could post another
message following the guidelines at:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


then you will be more likely to receive more useful responses. In other
words, try to simplify your code to the smallest complete example that
demonstrates your problem. Often, in the process of doing this, you may
be able to figure out the problem on your own.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply


Thanks for the tip. I'll definatley be more aware with my posts next time.
May 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same...
5
by: Jan Gregor | last post by:
Hello I want to change attribute action in form. Problem is that in that form is also input with name action. Unfortunately renaming of that input is worst case because many servlets depend on...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
1
by: j7.henry | last post by:
I am trying to pull specific data that is in a comma delimited file into a web page. So if my comma delimited file looks like: Name,Address,Zip Fred,123 Elm,66666 Mike,23 Jump,11111 I would...
10
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
1
by: Sebarry | last post by:
Hi, I'm trying to read the results of a database query into an XML document but it's only read so far and stopping. The XML document is as follows: <?xml version="1.0" encoding="utf-8"?> ...
1
by: JPhilS | last post by:
Hi to all Webmaster! I have discover this problem when I inserted the scores of the students i a centain subject. I am making a school project with regards to saving students' record. first, I...
0
by: Craftkiller | last post by:
=========Program compiles on both windows and linux=========== Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.