Thread: i'm trying to fix an error with my error messaging on my program

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    55

    Question i'm trying to fix an error with my error messaging on my program

    sounds ironic i guess, but my problem is that in my program i have a menu, and when you type in a letter that is not a menu option it prints an error saying you entered an invlid choice. but if you enter multiple characters.. something like tsdf and press enter it will repeat the error message 4 times. i want it to print one error message for no matter how many characters you type in. My menu choice is of type char. so i know why it's doing, it just runs the loop and enters the next character automatically.. but how can i get it do not do it? or to maybe fit multiple characters into that one char variable. People tell me to use an array(whiche i haven't learned about in class). do something like char choice[a big number]; and that will do it.. but i try it and well when the program runs no matter what i type in, even if it's a correct menu option, it prints the error message!!.. only once... but what's the use if i can't access the rest of the program that way.. anyone have a clue? my menu is set up with a bunch of if and else if statements. i know switch is better. but it's too late. it's for a class and it must be able to do this. How can i correct this?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You could start by posting some code.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    well right now i'm in my schools computer lab and don't have the code with me.. but when i get home i will... which will be in about 2 hours or so..

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    here's my code.. its set up so that if you type anything in that's not a menu option it will display that error message.. but if you type in more then on character for choice it will print the error based on how many characters you type in for choice.




    Code:
    #include <iostream.h>
    #include <math.h>
    #include <iomanip.h>
    
    void menu(void); // displays menu
    void sinusoid(double& freq, double& amp, double& phase); // option A
    void calcsignal(double freq, double amp, double phase); // option b
    void complex(void); // option C
    double arctan(double y, double x);  // subroutine of complex()
    void samplecalc(double sampfreq,double samptime,double freq,double amp,double phase); // subroutine of calcsignal
    
    const double pi = 3.1415926535; // global variable
    
    
    
    
    
    
    
    
    int main(void)
    {
          char choice;             // menu choice
          double freq, amp, phase; // 3 variables of sinusoid
    
          freq = 'a';       // used for error if user doesn't do option A
          amp = 'b';        //  before option B
          phase = 'c';      //
    
          do {
          menu();
          cin >> choice;
    
          if (choice == 'a' || choice == 'A')
          {
          sinusoid(freq, amp, phase);
          }
    
    
          else if (choice == 'b' || choice == 'B')
          {
          calcsignal(freq,amp,phase);
          }
    
    
          else if (choice == 'c' || choice == 'C')
          {
          complex();
          }
    
    
          else if (choice == 'q' || choice == 'Q') break;
    
    
          else
          {
          cout << endl;
          cout << "ERROR: Incorrect Choice!";
          cout << endl;
          }
    
             }while (1);
          return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    if you want the rest of the program let me know.. but i figured it's not nessesary since my problem is in main

  6. #6
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Make your IF..ELSE a switch and sorry but I do not use cin, so...

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    if i use switch instead of if - else.. it's the same result.. it will display error messages based on how many chracters i enter. all i want it to do is display the error message and menu once for anything that is typed in as a choice.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'd try changing the input to strings rather individual char input to see what happens. For some reason it sounds like the call to menu() is being ignored or isn't waiting for user input because there are still char in the input buffer after the first char is removed by the first call to >>. Therefore the program assumes that the user put in the second char at the second time the menu is called, eventhough they haven't, it's still there from the first input. Another way you could check this is to call the ignore() function of cin to be sure as you can that the input buffer is cleared after the initial input so there is nothing there the next time around.

    Do you have something in menu() to allow the user to stop the infinite loop created by do....while(1)?

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    in the menu. q or Q as an input breaks the loop and closes the program.
    then i have choice a,b, and c. they just do some signal sampling calculations, and after they are done it all goes back to the menu for a new input. and the user can choose to do another option or quit the program.

    if it is posible i would like to make it read only the first character and ignore everything, so if you type in any old garbage like dfgh since d is not one of the menu choice it will display the error once and then display the menu. can the ignore function do that? if so. how? here's the menu in case you want to see it.
    Code:
    void menu(void)
    {
         cout << endl;
         cout << "CGS2425 Sinusoids and Complex Numbers" << endl;
         cout << endl;
         cout << "A) Input a sinusoid" << endl;
         cout << "B) Sample the sinusoid" << endl;
         cout << "C) Calculate the magnitude and phase of a complex number" << endl;
         cout << "Q) Quit" << endl;
         cout << endl;
         cout << "Enter choice: ";
    }

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    55
    actually i should change what i said.. if you type in anything that's not a menu choice even if it's something like asdf i would still want it to display the error. A is an option but i want it to display the error anyway because of the other characters. it must be posible since the program i have to mimic does that. i'm not exactly sure if our professor really cares about that part of the program.. i think he's really looking for everything else at least that's what other people have told me. but i want to be sure it's exactly like his, and it would be good to know this for the future cause i'm sure i'll have to do something like that soon.

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    Try using recording the choice in a string, then check for the length of the string as an error. that way it will detect when more then one character has been entered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anybody fix this program for me ASAP
    By smallslugbug in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2006, 07:25 AM
  2. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM