By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Need help with Homework - checking chars

P: n/a
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

Mar 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
JE
On Mar 22, 6:42 pm, "bd" <dalestubblefi...@gmail.comwrote:
<snip>
while(exit==false);

Lose the semicolon.

<snip>
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.

Mar 23 '07 #2

P: n/a
bd
On Mar 22, 9:02 pm, "JE" <jeric...@pacbell.netwrote:
On Mar 22, 6:42 pm, "bd" <dalestubblefi...@gmail.comwrote:
<snip>
while(exit==false);

Lose the semicolon.
Duh...

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

Thanks!!!

Mar 23 '07 #3

P: n/a
bd wrote:
On Mar 22, 9:02 pm, "JE" <jeric...@pacbell.netwrote:
>On Mar 22, 6:42 pm, "bd" <dalestubblefi...@gmail.comwrote:
<snip>
while(exit==false);

Lose the semicolon.

Duh...

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[i] */
}
Mar 23 '07 #4

P: n/a
bd
On Mar 23, 9:20 am, red floyd <no.s...@here.dudewrote:
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[i] */
}
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

Mar 23 '07 #5

P: n/a
bd
On Mar 23, 9:55 am, "bd" <dalestubblefi...@gmail.comwrote:
On Mar 23, 9:20 am, red floyd <no.s...@here.dudewrote:

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

Mar 23 '07 #6

P: n/a
"bd" <da**************@gmail.comwrote in message
news:11*********************@e1g2000hsg.googlegrou ps.com...
On Mar 22, 9:02 pm, "JE" <jeric...@pacbell.netwrote:
>On Mar 22, 6:42 pm, "bd" <dalestubblefi...@gmail.comwrote:
<snip>
while(exit==false);

Lose the semicolon.

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.
Mar 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.