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]
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]
works for me
Code:char c; cout << "Enter product code: "; cin >> c; c = toupper(c); cout << c;
How is c declared?
if c is a character string try:
Code:for (int i = 0; c[i]; i++) { toupper(c[i]); }
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]
Code should not normally be put in .h files, only in .c files. .h files should be reserved for headers, declarations and definitions.
Well, that pretty much eliminates toupper() as the 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;
}
Please use [code][/code]Tags
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:
because all it takes is one true in a series of OR comparisons for the result to be true.Code:bool valid = false; while(!valid) { if(c == 'A' || c == 'B') valid = true; else cout << "invalid input " << endl; //etc. }
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]