473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need help with Homework - checking chars

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
6 1099
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
4
by: JC | last post by:
hi i want to check a char in the char array public void characters(char chars, int start, int length) { if (chars!='/n' || chars((char)'/r')) { System.out.println("String read is " + new...
8
by: whiteboy | last post by:
My assignment is to duplicate the strtok function. My problem is i get "access violation" errors when I try to turn a single char in a char*, as I should. My question is how do I get around this? I...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
6
by: dwight | last post by:
I have a string that can't contain the following characters: ../\|}{[:;+=_)(*&^%$#@!~` What would be easiest way to check the string? Should I use regexp or create a array with these...
31
by: Martin Jørgensen | last post by:
Hi, I've had a introductory C++ course in the spring and haven't programmed in C++ for a couple of months now (but I have been programmed in C since january). So I decided to do my conversion...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
6
by: Jeff | last post by:
Could someone tell me the easiest way to check a string of variable length to see if it consists of all blank characters? ....or perhaps more generally, to see if all of the characters are the...
3
by: alireza6485 | last post by:
I need to write a C# program that asks for a pattern and for each pattern beeps one and goes silence for 1 second. This is my code: for (int i=0;i< pattern; i++) { ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.