Need help with Homework - checking chars

B

bd

I think that I can convey my problem without having to post the entire
code.

I know that I am doing this wrong and that there is a better way, but
I keep coming up short with my various methods....

Here is the code:
//
************************************************************************************************

void getItems( /*inout*/ char& woodLetter,
/*inout*/ int& quantity,
/*inout*/ int& width,
/*inout*/ int& height,
/*inout*/ int& length )

// Postcondition: woodLetter must equal P, F, C, M, O, or T

{
bool exit;
exit = false;
cout << "\nEnter items: ";
cin >> woodLetter >> quantity >> width >> height >> length;
woodLetter = toupper(woodLetter);
if ((woodLetter=='P')||
(woodLetter=='F')||
(woodLetter=='C')||
(woodLetter=='M')||
(woodLetter=='O')||
(woodLetter=='T'))
exit = true;

while(exit==false);
{
cout << "\nWood Type must be one of the following:"
<< "\n(P) - Pine"
<< "\n(F) - Fir"
<< "\n(C) - Cedar"
<< "\n(M) - Maple"
<< "\n(O) - Oak"
<< "\n(T) - Total Price"
<< "\n\nEnter item: ";
cin >> woodLetter >> quantity >> width >> height >> length;
woodLetter = toupper(woodLetter);
if (woodLetter=='P')
exit = true;
else if (woodLetter=='F')
exit = true;
else if (woodLetter=='C')
exit = true;
else if (woodLetter=='M')
exit = true;
else if (woodLetter=='O')
exit = true;
else if (woodLetter=='T')
exit = true;
else exit = false;
}
return;
}

//
************************************************************************************************

This is not working for me. It will compile, but when I input what
should be one of the correct chars in upper or lower case, the program
still goes to the while loop and I get prompted again.

Any ideas?

Thanks,
Dale
 
J

JE

<snip>
while(exit==false);

Lose the semicolon.

This is not working for me. It will compile, but when I input what
should be one of the correct chars in upper or lower case, the program
still goes to the while loop and I get prompted again.

Any ideas?

Check the C++ FAQ for I/O. Watch out for getting cin into an error
state.
 
R

red floyd

bd said:

Been there, done that, got the T-shirt.

My very first piece of code written as a professional (C, not C++),
20-something years ago:

for (i = 0 ; i < N; i++);
{
/* initialize array */
}
 
B

bd

Been there, done that, got the T-shirt.

My very first piece of code written as a professional (C, not C++),
20-something years ago:

for (i = 0 ; i < N; i++);
{
/* initialize array */
}


Red - thanks for the encouragement! This is my first C++ class, but
so far I've made a 100 on every program (not that you can tell by this
one!)

I now have a new problem - When I execute the code below and enter an
incorrect char for 'woodLetter', the "Wood type must be..." part
executes on the screen six times, once for each letter.

I thought I had written it so that it should only display once and
then prompt for another type.

Here is the code:
//
************************************************************************************************
char GetWoodLetter( /*out*/ char& woodLetter )

// Postcondition: woodLetter must equal P, F, C, M, or O

{
bool exit;
exit = false;
cout << "\nEnter items: ";
cin >> woodLetter;
woodLetter = toupper(woodLetter);
if (woodLetter=='P')
exit = true;
else if (woodLetter=='F')
exit = true;
else if (woodLetter=='C')
exit = true;
else if (woodLetter=='M')
exit = true;
else if (woodLetter=='O')
exit = true;
else if (woodLetter=='T')
exit = true;

while(exit!=true)
{
cout << "\nWood Type must be one of the following:"
<< "\n(P) - Pine"
<< "\n(F) - Fir"
<< "\n(C) - Cedar"
<< "\n(M) - Maple"
<< "\n(O) - Oak"
<< "\n(T) - Total Price"
<< "\n\nEnter item: ";
cin >> woodLetter;
woodLetter = toupper(woodLetter);
if (woodLetter=='P')
exit = true;
else if (woodLetter=='F')
exit = true;
else if (woodLetter=='C')
exit = true;
else if (woodLetter=='M')
exit = true;
else if (woodLetter=='O')
exit = true;
else if (woodLetter=='T')
exit = true;
}
return woodLetter;
}

//
************************************************************************************************

Any ideas (again)?

Thanks,
Dale
 
B

bd

<SNIP>
I now have a new problem - When I execute the code below and enter an
incorrect char for 'woodLetter', the "Wood type must be..." part
executes on the screen six times, once for each letter.
<SNIP>

I feel like a big tard. It was executing over and over because of the
way I was inputting things not the way the code was written.

Next time, I will proofread better before asking for advice.

-dale-
 
J

Jim Langston

bd said:
Duh...

I guess I stared at it so long, I didn't notice that.

Thanks!!!

In my early days of programming I had a program with a bug I couldn't figure
out. After about 6 hour finally going through the program character by
character I finally found I had a 1 intead of an I somewhere.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top