Thread: Beginner's problem.. please help

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

    Question Beginner's problem.. please help

    here is the portion of my code that doesn't seem to be working... please help if you can at all:

    cout << "Enter product code: ";
    cin >> c;
    c = toupper(c);
    cout << c;


    Josh Stevanus
    [email protected]

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    works for me
    Code:
      char c;
      cout << "Enter product code: ";
      cin >> c;
      c = toupper(c);
      cout << c;

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How is c declared?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    if c is a character string try:

    Code:
    for (int i = 0; c[i]; i++) {
       toupper(c[i]);
    }

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

    Question update..

    This part of my code is included in a header file. This is the first time I've used a *.h file in my program. I have tried that code in a separate program and it works fine, but in this one it does not. When I run the program, it prints the prompt onto the screen, I enter the code (a, b, c, etc.) and then nothing else happens. I have to force quit the program.


    Josh Stevanus
    [email protected]

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code should not normally be put in .h files, only in .c files. .h files should be reserved for headers, declarations and definitions.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well, that pretty much eliminates toupper() as the problem.

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

    Question new problem..

    Ok, that first code I posted is working now, but the programs seems to lock up right after that. Here is the code for the function I'm using:

    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;
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Please use [code][/code]Tags

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I suspect you want the product code to be A-I or X, and if not reaquire input. The code you have won't do that. For example, what if user input 'a'. The first toupper would change it to 'A'. The while loop would evaluate it and the first comparison would occur. 'A' == 'A' therefore the first comparison is false, but all other comparisons will be true. However, in a series of AND comparisons, all it takes is one false and the whole series will evaluate to false.

    I'd try something like this:
    Code:
    bool valid = false;
    while(!valid)
    {
      if(c == 'A' || c == 'B')
    	valid = true;
     else
    	cout << "invalid input " << endl;
    	//etc.
    }
    because all it takes is one true in a series of OR comparisons for the result to be true.

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

    Smile Thank You!

    Thank you very much, that was exactly the type of answer I was looking for. I see what you mean, and I am changing my code immediately. I wish you were around all the time for my problems!
    Thanks again..

    Josh Stevanus
    [email protected]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Beginner's Problem With First Funtion
    By Ohrange in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2007, 06:59 PM
  5. A beginner's problem
    By NewToC in forum C Programming
    Replies: 5
    Last Post: 11-21-2002, 05:20 PM