Saturday, May 22, 2010

I need help to write this C++ code. urgently. I don't know where to start?

write a program that displays the following menu:





A. Display "Hello"


B. Display "Goodbye"


C. Display "Have a nice day"





Have the program get the input from the user in the form of a letter A, B, or C and display the corresponding greeting.


The program should work with either upper or lowercase letters.


Use a switch statement to check the user input.


PLEASE HELP... THANK YOU.

I need help to write this C++ code. urgently. I don't know where to start?
#include %26lt;iostream%26gt;


#include %26lt;cctype%26gt;


using namespace std;


int main()


{


char choice;





cout %26lt;%26lt; "A. Display \"Hello\"\n


B. Display \"Goodbye\"\n


C. Display \"Have a nice day\"" %26lt;%26lt; endl;





cin %26gt;%26gt; choice;





switch(toupper(choice))


{


case a: cout %26lt;%26lt; "Hello";


break;


case b: cout %26lt;%26lt; "Goodbye";


break;


case c: cout %26lt;%26lt; "Have a nice day";


}





return 0;





}
Reply:use


1) "printf" to show choices


2) "scanf" command for command input


3) make decision using case/switch


4) use printf 's again.


...


5) try to do your homework yourself
Reply:What is the question? We write the program for you? I'm not writing it for you.





you must start with an outline for your program. It should look like a declaration section will be at the top. Then a set of cout statements. Then an input statement, then a switch statement followed by the exit stage of the program.





Then, develop a pseudo code and use that to write the actual code.





For every tw significant lines of new code you add to your program, compile it to see that you are free of errors. If you compile all at a go, you might be overwhelmed with error messages.





Hope this helps.
Reply:Here's a rough outline:





#include %26lt;iostreams%26gt; // this lets you use cout and cin


using namespace std; // this lets you skip putting std in front of everything


...


cout %26lt;%26lt; "A) hello" %26lt;%26lt; endl;


...


char choice;


cin %26gt;%26gt; choice;





switch (choice) {


case 'a':


case 'A':


cout %26lt;%26lt; "hello" %26lt;%26lt; endl;


break;


....


}
Reply:#include %26lt;iostream%26gt;


using namespace std;





int main(void)


{


char input;





cout %26lt;%26lt; "A. Display \"Hello\"" %26lt;%26lt; endl %26lt;%26lt; "B. Display \"Goodbye\"" %26lt;%26lt; endl %26lt;%26lt; "C. Display \"Have a nice day\"" %26lt;%26lt; endl;





cin %26gt;%26gt; input;





switch (input)


{


case 'A': case 'a':


cout %26lt;%26lt; endl %26lt;%26lt; "Hello" %26lt;%26lt; endl;


break;





case 'B': case 'b':


cout %26lt;%26lt; endl %26lt;%26lt; "Goodbye" %26lt;%26lt; endl;


break;





case 'C': case 'c':


cout %26lt;%26lt; endl %26lt;%26lt; "Have a nice day" %26lt;%26lt; endl;


break;





default:


cout %26lt;%26lt; endl %26lt;%26lt; "Invalid selection" %26lt;%26lt; endl;


}





return (0);


}
Reply:#include %26lt;iostream%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;string.h%26gt;





using namespace std;





int main()


{


char input;





cout%26lt;%26lt;"A. Display \"Hello\"\n";


cout%26lt;%26lt;"B. Display \"Goodbye\"\n";


cout%26lt;%26lt;"C. Display \"Have a nice day\"\n";


cout%26lt;%26lt;"Selection: ";


cin%26gt;%26gt; input;


input = toupper(input);


switch ( input ) {


case 'A':


cout%26lt;%26lt;"Hello\n";


break;


case 'B':


cout%26lt;%26lt;"Goodbye";


break;


case 'C':


cout%26lt;%26lt;"Have a nice day";


break;


default:


cout%26lt;%26lt;"Error, bad input, quitting\n";


break;


}


getch();


}


No comments:

Post a Comment