Thread: Newbie problem with function coding..

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    19

    Question Newbie problem with function coding..

    I posted a thread before, but I have recoded since. All my function prototypes are now in my header file, and their definitions are in my *.cpp file. The function I am having trouble with seems to freeze the program right after it outputs the "product code" letter I enter. I enter a letter, it outputs the upper case version of that letter, then nothing seems to happen. Here is the code again:

    Code:
    char accept_item_code ()
    {
       char c;
       
       cout << endl << "--------------------" << endl;
       cout << "Enter product code: ";
       cin >> c;
       c = toupper(c);
       cout << c;
       cout << endl;
       cout << "BEFORE WHILE LOOP";           // DEBUGGING
       while (c != 'A' && c !='B' && c !='C' && c !='D' 
            && c !='E' && c !='F'&& c !='G' && c !='H'
    		&& c !='I' && c !='X')
       {
          cout << "INSIDE WHILE LOOP";                 // DEBUGGING
          cout << "!!! Invalid product code" << endl << endl
               << "Enter product code: ";
          cin >> c;
          c = toupper(c);
          cout << endl;
       }
       cout << "RETURNING ITEM CODE FROM FUNCTION";   // DEBUGGING
    
       return c;
    }
    The prompt is printed, I enter the letter (like "a"), the toupper(c) changes it to "A", the "A" is printed to the screen, and then nothing more happens. What could be the problem? Any help is greatly appreciated since this program is due tonight!


    Josh Stevanus
    [email protected]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well typing in 'A' means that the first test in your loop (c != 'A') is already false, which makes the whole boolean expression false, which means your while loop never executes.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Logical or ( || ) may be the thing you are looking for.
    Playing around with the boolean operators for a short while will enable you to get the hang of them.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    ...then nothing seems to happen...
    What is supposed to happen? What's the program/ function supposed to do? Is 'A' supposed to be valid, or invalid???


    1- You enter an 'a', which gets changed to a 'A' by toupper()

    2- 'A' is displayed

    3- The while condition is not met, so the while-loop is skipped. 'A' is valid (not invalid if you prefer).

    4- The function returns 'A'

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by jstevanus
    The prompt is printed, I enter the letter (like "a"), the toupper(c) changes it to "A", the "A" is printed to the screen, and then nothing more happens. What could be the problem? Any help is greatly appreciated since this program is due tonight!


    Josh Stevanus
    [email protected]
    Sorry if you missed the deadline. For future reference: does it make any difference if you flush the output stream? Something like:

    Code:
       cout << "BEFORE WHILE LOOP" << flush;
    or

    Code:
       cout << "RETURNING ITEM CODE FROM FUNCTION" << endl;
    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Newbie: having a problem with my string function.
    By dsharp in forum C Programming
    Replies: 3
    Last Post: 10-20-2003, 10:11 AM
  5. Problem with strncat() function (newbie)
    By Kettch in forum C Programming
    Replies: 2
    Last Post: 12-08-2001, 02:54 AM