Having problems with a menu type program.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am in a beginning Programming class, and we are supposed to write a program that does the following.

1. Displays the following on the Console:<o:p></o:p>
3. Hello out there in C# land, I’m your happy menu:-)<o:p></o:p>
4. ------------------------------------------------------<o:p></o:p>
5. I)nteger Values<o:p></o:p>
6. D)ouble Values<o:p></o:p>
7. C)haracter Values<o:p></o:p>
8. S)tring Values<o:p></o:p>
9. Q)uit Program<o:p></o:p>
2. Gets the users input, in a do-while loop and verifies that it is valid and if it is NOT, notifies the user, then clears the Console, and waits for another response. If it is valid it then saves the value in the appropriate variable. NOTE, you cannot allow the user to enter a value that will throw an Exception!<o:p></o:p>
3. Asks the user to enter in a second integer, double, char or string value.<o:p></o:p>
4. Gets the users second input, in a do-while loop and verifies that it is valid and if it is NOT, notifies the user, then clears the Console, and waits for another response. If it is valid it then saves the value in the second appropriate variable. NOTE, you cannot allow the user to enter a value that will throw an Exception!<o:p></o:p>
5. If the variables entered were of same type int or double, then it proceeds to add the two values together and outputs the result, showing four digits if it were an integer and three digits after the decimal if it were a double. If it the user selected “C” or “S” concatenate the values and output them.<o:p></o:p>
6. Asks the user if they want to enter two more values.<o:p></o:p>
7. If the user responds "yes" go back to step #1.<o:p></o:p>
8. Otherwise print a "Goodbye" message and end the program.<o:p></o:p>
Enter your choice: You MUST NOT use string literals in Console.Write(…) or Console.WriteLine(…) statements. You need to declare them as string <identifier> and then use this string identifier in the Console.Write( … ) or Console.WriteLine(…).
<o:p></o:p>
<o:p>Here is the code I have so far. The integer option works perfectly, so does Quit. I keep getting errors, though in the Double, Char, and String sections. I know its because I am doing something stupid, I just cant figure out what it is supposed to look like.</o:p>
<o:p>
</o:p>
I HAVE MARKED THE LINES WHERE I GET COMPILE ERRORS WITH ALL CAPS IN PSEUDO CODE OFF TO THE SIDE.
I know this code is a mess, but any help I can get will be much appreciated.

using System;static class Program{ const int COL = 20; const int ROW_1 = 6; const int ROW_2 = 8; const string MainMenu = "Hello out there in C# Land, Im your happy menu:-) ntI) Integer Values ntD)Double Values"+
"ntC)Character Values ntS)String Values ntQ)Quit nEnter your choice: ";
const string ErrorMsg_1 = "You just pressed Enter. Input a valid selection!"; const string ERROR_INT = "You entered an invalid Integer, please re-enter"; const string ERROR_DBL = "You entered an invalid Double Value, please re-enter"; const string ERROR_CHAR = "You entered an invalid Character, please re-enter"; const string ERROR_STR = "You entered an invalid String, please re-enter"; const string ERROR_MENU = "Invalid Menu selection, please re-enter"; const string QUIT_MSG = "Quit selected. Press Enter to continue..."; const string EINT = "Enter an Integer "; const string INT_2 = "Enter a Second Integer "; const string EDBL = "Enter a Double "; const string DBL_2 = "Enter a Second Double "; const string ECHAR = "Enter a Character"; const string CHAR_2 = "Enter a Second Character "; const string ESTR = "Enter a string"; const string STR_2 = "Enter a Second String "; const string BLANK_LINE = " "; const string INT_OUT = "Integer = {0:D4}"; const string DBL_OUT = "Double = {0:F3}"; const string GOODBYE = "Have a good day! Bye-Bye!"; const string REPEAT = "Here we go again!"; const string ERROR = "Invalid selection. Try again!"; static void Main() { Menu(); } static void Menu() { // use a do-while loop do { // this logic gets done every time the program executes the loop Console.Clear(); Console.WriteLine(MainMenu); Console.SetCursorPosition(COL, ROW_1); string inputStg = Console.ReadLine(); if (inputStg == "") { Console.WriteLine(ErrorMsg_1); Console.ReadLine(); continue; } switch (inputStg[0]) { case I: case i: int tempInt = 0; int tempInt2 = 0; do //--------------------------------------- Integer Loop ---------------------------------- { Console.SetCursorPosition(0, ROW_2); //position cursor Console.Write(EINT); // write enter integer message Console.SetCursorPosition(COL, ROW_2); // position curson Console.Write(BLANK_LINE); // clear input Console.SetCursorPosition(COL, ROW_2); inputStg = Console.ReadLine(); if (int.TryParse(inputStg, out tempInt) == true) { Console.Write(INT_2); tempInt2 = int.Parse(Console.ReadLine()); int Solution_INT = tempInt + tempInt2; Console.WriteLine(INT_OUT, Solution_INT); Console.ReadLine(); break; } else { Console.Write(ERROR_INT); int col = Console.CursorLeft; // get column position int row = Console.CursorTop; // get row position Console.ReadLine(); Console.SetCursorPosition(0, row); // position cursor Console.WriteLine(BLANK_LINE); // clear error message continue; } } while (true); //--------------------------------------- End int Loop -------------------------------- break; case D: case d: double tempDbl = 0.0; double tempDbl2 = 0.0; do //--------------------------------------- Double Loop ----------------------------------- { Console.SetCursorPosition(0, ROW_2); //position cursor Console.Write(EDBL); // write enter Double message Console.SetCursorPosition(COL, ROW_2); // position curson Console.Write(BLANK_LINE); // clear input Console.SetCursorPosition(COL, ROW_2); inputStg = Console.ReadLine(); if (double.Parse(inputStg, out tempDbl) == true) // ERROR HERE! NOT SURE HOW TO FIX IT { Console.Write(DBL_2); tempDbl2 = double.Parse(Console.ReadLine()); double Solution_DBL = tempDbl + tempDbl2; Console.WriteLine(DBL_OUT, Solution_DBL); Console.ReadLine(); break; } else { Console.Write(ERROR_DBL); int col = Console.CursorLeft; // get column position int row = Console.CursorTop; // get row position Console.ReadLine(); Console.SetCursorPosition(0, row); // position cursor Console.WriteLine(BLANK_LINE); // clear error message continue; } } while (true); //--------------------------------------- End double Loop ------------------------------ break; case C: case c: char tempChar = ; // ERRORS HERE I KNOW THESE ARE BECAUSE I HAVENT GIVEN A BASE VALUE BUT IM NOT SURE HOW TO char tempChar2 = ; // ERRORS HERE DO THAT WITH CHAR. STRING IS "", AND DOUBLE/INTEGER IS 0.0 AND 0 RESPECTIVELY do //--------------------------------------- Char Loop ------------------------------------ { Console.SetCursorPosition(0, ROW_2); //position cursor Console.Write(ECHAR); // write enter Char message Console.SetCursorPosition(COL, ROW_2); // position curson Console.Write(BLANK_LINE); // clear input Console.SetCursorPosition(COL, ROW_2); inputStg = Console.ReadKey(); //ERRORS HERE if (Char.Parse(inputStg, out tempChar) == true) //ERRORS HERE { Console.Write(CHAR_2); tempDbl2 = (Console.ReadKey()); // ERRORS HERE char Solution_DBL = tempChar + tempChar2; // ERRORS HERE Console.WriteLine(DBL_OUT, Solution_DBL); Console.ReadKey(); break; } else { Console.Write(ERROR_CHAR); int col = Console.CursorLeft; // get column position int row = Console.CursorTop; // get row position Console.ReadLine(); Console.SetCursorPosition(0, row); // position cursor Console.WriteLine(BLANK_LINE); // clear error message continue; } } while (true); break; //-------------------------------------- End Char Loop ------------------------------- case S: case s: string tempString = ""; string tempString2 = ""; do //------------------------------------- String Loop ----------------------------------- { Console.SetCursorPosition(0, ROW_2); //position cursor Console.Write(ESTR); // write enter String message Console.SetCursorPosition(COL, ROW_2); // position curson Console.Write(BLANK_LINE); // clear input Console.SetCursorPosition(COL, ROW_2); inputStg = Console.ReadLine(); if (string.Compare(inputStg, out tempChar) == true) // ERROR HERE { Console.Write(STR_2); tempDbl2 = double.Parse(Console.ReadLine()); string Solution_DBL = tempString + tempString2; Console.WriteLine(DBL_OUT, Solution_DBL); Console.ReadKey(); break; } else { Console.Write(ERROR_STR); int col = Console.CursorLeft; // get column position int row = Console.CursorTop; // get row position Console.ReadLine(); Console.SetCursorPosition(0, row); // position cursor Console.WriteLine(BLANK_LINE); // clear error message continue; } } while (true);
break; //-------------------------------- End String Loop ---------------------------------------------- } // Now see if the user wants to do this loop again Console.Write("Do you want to do this again (yes or no)? "); string userResponse = Console.ReadLine(); if (userResponse == "") { Console.WriteLine(ErrorMsg_1); Console.ReadLine(); continue; } switch (userResponse[0]) { case N: case n: Console.WriteLine(GOODBYE); Console.ReadLine(); return; case Y: case y: Console.WriteLine(REPEAT); Console.ReadLine(); break; default: Console.WriteLine(ERROR); Console.ReadLine(); break; } } while (true); Console.ReadLine(); }//End Main()}//End class Program



<o:p>
</o:p>


View the full article
 
Back
Top