944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 11981
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 20th, 2007
0

Help with Creating a Command Line Menu

Expand Post »
I have a homework assignment that deals with creating a command line menu, and then selecting items from that menu that do something. the items need to be selected by using either a lowercase or uppercase letter that represents that specific menu item. for example, if i want to print a sentence, then i need to type either lowercase 'p' or uppercase 'P'. right now i dont want help getting each menu item to work, but i need help with configuring my program so that when i type p or P, it will do that function, or if i select e or E, it will perform that function...etc. this is the code I have right now, I was wondering if someone could tell me if I am on the right track. right now i was thinking of declaring each valid character for input as a character, and then inputing that character. i then made a simple if/else statement to test if it works. thanks for any help you can give me.
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. int main()
  8. {
  9. char x [1];
  10. char c [1];
  11. char C [1];
  12. char p [1];
  13. char P [1];
  14. char e [1];
  15. char E [1];
  16. char d [1];
  17. char D [1];
  18. char q [1];
  19. char Q [1];
  20.  
  21. cout << "You can perform the following tasks: \n";
  22. cout << "(p) Print a Sentence \n";
  23. cout << "(c) Capitalize the Sentence \n";
  24. cout << "(e) Encode the Sentence \n";
  25. cout << "(d) Decode the Sentence \n";
  26. cout << "(q) Quit \n";
  27. cout << "Please Select one... \n";
  28.  
  29. cin.getline (x, 1);
  30.  
  31. if (x = c || x = C) //if you type either lowercase c or uppercase C, test was successful
  32. cout << "Test Successful /n";
  33.  
  34. else {
  35. cout << "Test Failed /n";
  36. }
  37.  
  38. return 0;
  39. }
Last edited by WaltP; Jan 20th, 2007 at 8:43 pm. Reason: Added Code Tags -- aren't the words clear enough in the input text box?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
raydogg57 is offline Offline
41 posts
since Jan 2007
Jan 20th, 2007
0

Re: Help with Creating a Command Line Menu

Click to Expand / Collapse  Quote originally posted by raydogg57 ...
I have a homework assignment that deals with creating a command line menu, and then selecting items from that menu that do something. the items need to be selected by using either a lowercase or uppercase letter that represents that specific menu item. for example, if i want to print a sentence, then i need to type either lowercase 'p' or uppercase 'P'. right now i dont want help getting each menu item to work, but i need help with configuring my program so that when i type p or P, it will do that function, or if i select e or E, it will perform that function...etc. this is the code I have right now, I was wondering if someone could tell me if I am on the right track. right now i was thinking of declaring each valid character for input as a character, and then inputing that character. i then made a simple if/else statement to test if it works. thanks for any help you can give me.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
char x [1];
char c [1];
char C [1];
char p [1];
char P [1];
char e [1];
char E [1];
char d [1];
char D [1];
char q [1];
char Q [1];

cout << "You can perform the following tasks: \n";
cout << "(p) Print a Sentence \n";
cout << "(c) Capitalize the Sentence \n";
cout << "(e) Encode the Sentence \n";
cout << "(d) Decode the Sentence \n";
cout << "(q) Quit \n";
cout << "Please Select one... \n";

cin.getline (x, 1);

if (x = c || x = C) //if you type either lowercase c or uppercase C, test was successful
cout << "Test Successful /n";

else {
cout << "Test Failed /n";
}

return 0;
}
Yes it looks ok apart from a few syntax errors.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jan 20th, 2007
0

Re: Help with Creating a Command Line Menu

when i build the program it gives me the following errors:

warning C4800: 'char *' : forcing value to bool 'true' or 'false' (performance warning)
error C2106: '=' : left operand must be l-value
error C2440: '=' : cannot convert from 'bool' to 'char [1]'
There are no conversions to array types, although there are conversions to references or pointers to arrays

i was wondering what was wrong with my code above that gives me these errors. i tried fiddling around with some things a bit but i cant fix it. what syntax errors do you see that I am missing?
Reputation Points: 10
Solved Threads: 0
Light Poster
raydogg57 is offline Offline
41 posts
since Jan 2007
Jan 20th, 2007
0

Re: Help with Creating a Command Line Menu

i changed the following if statement so it has double = now:

if (x == c || x == C) //if you type either lowercase c or uppercase C, test was successful
cout << "Test Successful /n";

it gives me no errors but when i run the program and type either c or C it says test failed. why would this happen?
Reputation Points: 10
Solved Threads: 0
Light Poster
raydogg57 is offline Offline
41 posts
since Jan 2007
Jan 20th, 2007
0

Re: Help with Creating a Command Line Menu

Hi!

Okay, your premise is sound, but the execution could use some work. If you don't mind me turning into an evil code reviewer, I'll point out some ways to make your code better.
C++ Syntax (Toggle Plain Text)
  1. using std::cout;
  2. using std::cin;
  3. using std::endl;
This is my personal preference. I think it's a good one because I'm good at convincing my coworkers to use it. Any using statements should be at the smallest possible scope. That usually means putting a using namespace std; at the top of a bunch of functions or even in a loop or if body. You shouldn't use it globally, and the highest scope should be at the top of a namespace. Anything global should be prefixed with std:: and that's not a big problem because global things that need a standard name should be kept to a minimum. You can ignore all of this for now though, it's not really important when learning C++. :eek:
C++ Syntax (Toggle Plain Text)
  1. char x [1];
  2. char c [1];
  3. char C [1];
  4. char p [1];
  5. char P [1];
  6. char e [1];
  7. char E [1];
  8. char d [1];
  9. char D [1];
  10. char q [1];
  11. char Q [1];
You can make arrays of 1, but it doesn't really make much sense. An array of 1 char is basically a single char that's just harder to access directly. Instead of just saying x, you have to say x[0], or *x. That's a notational inconvenience.
C++ Syntax (Toggle Plain Text)
  1. cin.getline (x, 1);
This is the big problem. cin.getline() will read n - 1 characters and append the array with '\0'. No matter what you type, x will always be an empty string.

Stream input is really tricky. I hate it. But in this case you can just use cin.get() to read one character and things will work. If you add other input later on, you need to re-evaluate that assumption because it might not be true anymore.
C++ Syntax (Toggle Plain Text)
  1. if (x = c || x = C)
There are three problems here. First, x is an array and the first character must be accessed with x[0] or *x. Otherwise you're just taking the address of the first character, and that's probably not what you were expecting. Second, the = operator is assignment. You want the == operator for comparison. Finally, c and C are treated as variables, not character literals. You need to wrap them in single quotes.

Here's my take on the code.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. using namespace std;
  6.  
  7. char x;
  8.  
  9. cout << "You can perform the following tasks: \n";
  10. cout << "(p) Print a Sentence \n";
  11. cout << "(c) Capitalize the Sentence \n";
  12. cout << "(e) Encode the Sentence \n";
  13. cout << "(d) Decode the Sentence \n";
  14. cout << "(q) Quit \n";
  15. cout << "Please Select one... \n";
  16.  
  17. cin.get( x );
  18.  
  19. if (x == 'c' || x == 'C')
  20. cout << "Test Successful\n";
  21. else
  22. cout << "Test Failed\n";
  23.  
  24. return 0;
  25. }
Ask questions! I love helping.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 20th, 2007
0

Re: Help with Creating a Command Line Menu

Thank you so much for helping me with that! Now that I have that part settled I am working on the specific functions within my command line menu. I have already taken care of printing the sentence, as well as the quit option. My program is supposed to be continuous until the user actually types q or Q to terminate it. I just used a simple while(1) loop and then put in the break after they type q or Q.

My next question deals with making a capitalization function. I know that once I create the function I just have to simply insert it below an if statement and then include the actual function below the program.

What this function needs to do is capitalize the sentence the user inputs before the command line menu pops up. It needs to capitalize every character in that sentence. I was trying to look at it a couple of ways:

something like char capitalize(char), but I think that will only capitalize one specific character.

I was wondering if theres an a keyword or something that will capitalize an entire string. The string in my program I titled as 'sentence'. so i would do something like naming a function char capitalize(char), and then use that keyword on the string, and then return(sentence)? Here is my updated code thus far:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. int main()
  8. {
  9. char x;
  10. char sentence [800];
  11. cout << "Enter a Sentence \n";
  12. cin.getline (sentence, 800);
  13.  
  14. while (1)
  15. {
  16. cout << "You can perform the following tasks: \n";
  17. cout << "(p) Print a Sentence \n";
  18. cout << "(c) Capitalize the Sentence \n";
  19. cout << "(e) Encode the Sentence \n";
  20. cout << "(d) Decode the Sentence \n";
  21. cout << "(q) Quit \n";
  22. cout << "Please Select one... \n";
  23.  
  24. cin.get(x);
  25.  
  26. if (x == 'p' || x == 'P') //if you type either lowercase c or uppercase C, test was successful
  27. {
  28.  
  29. cout << sentence << "\n";
  30.  
  31. }
  32. else if (x == 'q' || x == 'Q')
  33.  
  34. cout << "Thanks for using my program, goodbye! \n";
  35. break;
  36. }
  37.  
  38. return 0;
  39. }
Last edited by ~s.o.s~; Jan 20th, 2007 at 11:09 pm. Reason: Added code tags, learn to use them.
Reputation Points: 10
Solved Threads: 0
Light Poster
raydogg57 is offline Offline
41 posts
since Jan 2007
Jan 20th, 2007
0

Re: Help with Creating a Command Line Menu

Click to Expand / Collapse  Quote originally posted by raydogg57 ...
Thank you so much for helping me with that!
My pleasure.
Click to Expand / Collapse  Quote originally posted by raydogg57 ...
I was wondering if theres an a keyword or something that will capitalize an entire string.
Some compilers give you a function that will do this. For example, Visual Studio supports a strupr() function in the <cstring> header.
C++ Syntax (Toggle Plain Text)
  1. cout << strupr( sentence ) << "\n";
But not all compilers do that, and it's not a standard function. It's up to you to determine if you're alright with the portability hit. If you aren't, C++ does support a function called toupper() in <cctype> that translates one character to its upper case equivalent. If you use that in a loop, you can fake strupr.
C++ Syntax (Toggle Plain Text)
  1. #include <cctype>
  2.  
  3. char *my_strupr( char *s )
  4. {
  5. for ( char *p = s; *p; p++ )
  6. *p = toupper( (unsigned char)*p );
  7.  
  8. return s;
  9. }
  10.  
  11. cout << my_strupr( sentence ) << "\n";
And there are a number of ways to do that. Some people will advocate using the transform() function from <algorithm>, but that's tricky so I don't recommend it right now.
Click to Expand / Collapse  Quote originally posted by raydogg57 ...
Here is my updated code thus far:
I've discovered a problem. In something like a loop or an if statement that gives you the option of using braces, you need to be careful to remember that it's only an option if the block only has one statement. Here's your code, cleaned up a hair and properly indented.
C++ Syntax (Toggle Plain Text)
  1. if (x == 'p' || x == 'P')
  2. {
  3. cout << sentence << "\n";
  4. }
  5. else if (x == 'q' || x == 'Q')
  6. cout << "Thanks for using my program, goodbye! \n";
  7. break;
In the if part the braces are optional and you chose to add them, but in the else if part the braces are not optional if you want to break only if someone types 'q' or 'Q'. A good guideline is to always use braces until you're comfortable with when they can be ignored.
C++ Syntax (Toggle Plain Text)
  1. if (x == 'p' || x == 'P') {
  2. cout << strupr( sentence ) << "\n";
  3. }
  4. else if (x == 'q' || x == 'Q') {
  5. cout << "Thanks for using my program, goodbye! \n";
  6. break;
  7. }
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 20th, 2007
0

Re: Help with Creating a Command Line Menu

Thanks again! I included the function you helped me with but whenever I compile the program it gives me the following two errors:

error C3861: 'capitalize': identifier not found
error C2365: 'capitalize' : redefinition; previous definition was 'formerly unknown identifier'

I feel like something is throwing this off, possibly a syntax error. Here is my code after including my function:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. int main()
  9. {
  10. char x;
  11. char sentence [800];
  12. cout << "Enter a Sentence \n";
  13. cin.getline (sentence, 800);
  14.  
  15. while (1)
  16. {
  17. cout << "You can perform the following tasks: \n";
  18. cout << "(p) Print a Sentence \n";
  19. cout << "(c) Capitalize the Sentence \n";
  20. cout << "(e) Encode the Sentence \n";
  21. cout << "(d) Decode the Sentence \n";
  22. cout << "(q) Quit \n";
  23. cout << "Please Select one... \n";
  24.  
  25. cin.get(x);
  26.  
  27. if (x == 'p' || x == 'P')
  28. {
  29. cout << sentence << "\n";
  30. }
  31. else if (x == 'c' || x == 'C')
  32. {
  33. cout << capitalize( sentence ) << "\n";
  34. }
  35. else if (x == 'q' || x == 'Q')
  36. {
  37. cout << "Thanks for using my program, goodbye! \n";
  38. break;
  39. }
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45. //Function Definitions
  46.  
  47. char *capitalize(char *s)
  48. {
  49. for ( char *c = s; *c; c++ )
  50. *c = toupper( (unsigned char)*c );
  51.  
  52. return s;
  53. }
Is the program having trouble pointing to capitalize in the function? Is that what the program error means? Thanks again for all your help!
Last edited by ~s.o.s~; Jan 21st, 2007 at 11:50 am. Reason: Ignoring code tags next time will fetch you an infraction..!!
Reputation Points: 10
Solved Threads: 0
Light Poster
raydogg57 is offline Offline
41 posts
since Jan 2007
Jan 21st, 2007
0

Re: Help with Creating a Command Line Menu

Please use code tags (info in my signature).

The reason you're getting an error about capitalize() is that you haven't given the function a prototype. A prototype is simply a statement at the top of your program defining the functions so that the compiler knows they exist. For example, a prototype for capitalize() would look like this:
  1. char *capitalize(char *s);

Your program works when this is added, but I noticed that you forgot to clear the input buffer, so you get an extra loop in there. Try cin.clear(), and perhaps cin.ignore() with some large number.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 21st, 2007
0

Re: Help with Creating a Command Line Menu

You are right, i just needed to include it at the top of my code. Thanks! and yes, I noticed that my code loops twice, so i tried inserting cin.clear(); in various places within my while loop but for some reason it didnt work. do I need to include something else in addition to cin.clear()?

I also have another question. My program needs to not alter the sentence string. So although my program above performs the functions in order, if i choose to capitalize the sentence but then just want to print the original sentence after, it will print the newly capitalized sentence. I need my program to capitalize the sentence and then print the original sentence whenever i want.

I was thinking that maybe I can just create another character string, called csentence. And then after the user inputs the sentence at the beginning of the program, it creates a duplicate of that sentence saved as csentence. so that when i output the capitalization function later, it uses the csentence string, instead of outputting the original sentence string. so i was thinking something like char csentence [800]; and then sentence = csentence; but when I try this it doesnt work. am I on the right track?
Reputation Points: 10
Solved Threads: 0
Light Poster
raydogg57 is offline Offline
41 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Importing BMP images into Allegro
Next Thread in C++ Forum Timeline: need a function that woks a little bit like getch





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC