GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Jan-2006, 14:12
Sabin044 Sabin044 is offline
Junior Member
 
Join Date: Oct 2005
Posts: 68
Sabin044 is on a distinguished road

Sample looping (do/while)


Ok I'm having trouble doing a validation loop for part of a program I am doing using a do/while loop.

The code I have is:

CPP / C++ / C Code:

    do
    {
    cout << "Enter your sales representative number:  ";
    cin >> salesRep;
    cout << "Invalid sales representative number.  Please try again." << endl;
    }
    while (salesRep < 100 || salesRep > 300);


Well, it works because it asks repeatedly the user for a number if it is invalid, but here is what I get:


Quote:


Enter your sales representative number: 500
Invalid sales representative number. Please try again.
Enter your sales representative number: 400
Invalid sales representative number. Please try again.
Enter your sales representative number: 300
Invalid sales representative number. Please try again.
Enter your customer number:


When I enter a valid number (300 or 200 or whatever is valid) it still says "Invalid sales rep number, try again"

and automatically goes to the customer after that.

So, how could I fix this so it goes RIGHT to the customer number without repeating the invalid text when a valid number is entered?

Sorry I am a bit rusty, I know this is very easy =\
  #2  
Old 31-Jan-2006, 14:48
sERAC sERAC is offline
New Member
 
Join Date: Jan 2006
Posts: 20
sERAC is on a distinguished road

Re: Sample looping (do/while)


It's because you said to output "invalid ..." regardless of whatever the number is, valid or not. You have to make it so it processes the first answer and then decides whether to print that out or not.

consider changing it to something like this:

CPP / C++ / C Code:
cout << "Enter your sales representative number:  ";
cin >> salesRep;
if (salesRep < 100 || salesRep > 300) {
  do
    {
    cout << "Invalid sales representative number.  Please try again." << endl;
    cout << "Enter your sales representative number:  ";
    cin >> salesRep;
    }
    while (salesRep < 100 || salesRep > 300);
    }

That'd probably work.
Last edited by LuciWiz : 27-Mar-2006 at 11:43. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #3  
Old 31-Jan-2006, 15:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Sample looping (do/while)


Quote:
Originally Posted by sERAC
It's because you said to output "invalid ..." regardless of whatever the number is, valid or not. You have to make it so it processes the first answer and then decides whether to print that out or not.

consider changing it to something like this:

Code:
cout << "Enter your sales representative number: "; cin >> salesRep; if (salesRep < 100 || salesRep > 300) { do { cout << "Invalid sales representative number. Please try again." << endl; cout << "Enter your sales representative number: "; cin >> salesRep; } while (salesRep < 100 || salesRep > 300); }

That'd probably work.
The problem is correct and your solution will work, sERAC.

A more compact and straight forward version of your code is:
CPP / C++ / C Code:
cout << "Enter your sales representative number:  ";
cin >> salesRep;
while (salesRep < 100 || salesRep > 300)
{
    cout << "Invalid sales representative number.  Please try again." << endl;
    cout << "Enter your sales representative number:  ";
    cin >> salesRep;
}
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #4  
Old 31-Jan-2006, 22:36
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Sample looping (do/while)


Sabin, Welcome back

Kobi
__________________
It's actually a one time thing (it just happens alot).
  #5  
Old 02-Feb-2006, 06:47
Sabin044 Sabin044 is offline
Junior Member
 
Join Date: Oct 2005
Posts: 68
Sabin044 is on a distinguished road

Re: Sample looping (do/while)


CPP / C++ / C Code:

cout << "Enter your sales representative number:  ";
cin >> salesRep;
if (salesRep < 100 || salesRep > 300) {
  do
    {
    cout << "Invalid sales representative number.  Please try again." << endl;
    cout << "Enter your sales representative number:  ";
    cin >> salesRep;
    }
    while (salesRep < 100 || salesRep > 300);
    }



I forgot that you needed to repeat the message inside =P

Same with

CPP / C++ / C Code:

cout << "Enter your sales representative number:  ";
cin >> salesRep;
while (salesRep < 100 || salesRep > 300)
{
    cout << "Invalid sales representative number.  Please try again." << endl;
    cout << "Enter your sales representative number:  ";
    cin >> salesRep;
}




Thanks for your feedback, I am forgetting too much from my C class hehe=]

Hey Kobi thanks for the welcome, nice to see you and everyone else :]
  #6  
Old 03-Feb-2006, 23:32
Sabin044 Sabin044 is offline
Junior Member
 
Join Date: Oct 2005
Posts: 68
Sabin044 is on a distinguished road

Re: Sample looping (do/while)


I came across something else that has stumped me sitting here for hours looking at it. I'm guessing it involves another loop of some sort.

Anyways, I made 2 sales reps with user input (for the 2 of course). Now, the thing that gets me is.. it says "allow the user to enter the data for any number of sales reps and display the 2 reports for that rep before prompting the user for the next sales rep." In which the 2 main reports I have completed.. but "allowing to enter as many as the user wants" is the thing that seems tricky to me.

so lets say (without the validation/input code) my code for the report 1 is..


CPP / C++ / C Code:

 totalSale = quantity * unitPrice;

  cout << "                         Harford Computer Company" << endl << endl;
	
  cout << endl;
	
  cout << "Sales Rep:  " << salesRep;
  cout << "                          Region:  "  << region << endl;
  cout << "Customer Number:  " << customerNumber << endl;
  cout << endl;
  cout << "Item #        Quantity         Unit Price        Total Sale" << endl;
  cout << endl;
  cout << left << setw( 10 ) << itemNumber << right << setw( 10 ) << quantity << right << setw( 20 ) << unitPrice << right << setw( 19 ) << totalSale;
  cout << endl;
  cout << endl;
  cout << "Customer " << customerNumber << right << setw( 15 ) << "Total   Sale: " << customerSale;


I do not understand how the user can enter amounts of an "item number" and "quantity" and have it calculate the "Total Sale" (at the bottom of report) all at once.


(This will show u I already have inputs)


So for example my output for report1 is this:

CPP / C++ / C Code:

Enter your sales representative number:  200
Enter your customer number:  300
Enter your region (A - D):  A
Enter your item number:  40          // My Input Validations..  Easy.. Done..
Enter your quantity:  50
Enter your unit price:  6.75


                         Harford Computer Company


Sales Rep:  200                          Region:  A
Customer Number:  300

Item #        Quantity         Unit Price        Total Sale

40                50             6.75000            337.500

Customer 300   Total Sale: 0.000000  // I cant get this without more than 1 rep entry....since it is the "Total of all purchases"

Press any key to continue



The sample output my teacher gave me to test the validation/input was this:

CPP / C++ / C Code:

SalesRep   Region Cust # Item #     Quantity     Unit Price

900          C      60        465       10        80.50

                             440      800       600.20

                             680       10       1,000.00


Now, notice the testing input gave 3 different item numbers, quantitys, and prices. I need the user to enter exactly more than 1 of each of those...


I'm basically thinking a loop is needed here, but I have not done any kinda loop like this before (i'm pretty sure i havent) yet my book does not explain loops in great detail...

My 2nd report looks like this:

CPP / C++ / C Code:

PauseScreen();
ClearScreen();
    
commission = commissionRate * customerSale;


cout << "                         Harford Computer Company" << endl << endl;

cout << endl;
	
cout << "Sales Rep:  " << salesRep;
cout << "                          Region:  "  << region << endl;
cout << "Commission Rate: 6%" << endl;
cout << endl;
cout << "Customer #     Total Sales     Commission" << endl;
cout << endl;
cout << customerNumber << right << setw( 20 ) << customerSale << right << setw( 17 )
<< commission << right << setw( 10 ) << endl;



Again, I cannot get the "Total Sales" because I cannot figure out how to get the sample input shown above.

Output for report2;

CPP / C++ / C Code:

                         Harford Computer Company


Sales Rep:  200                          Region:  A
Commission Rate: 6%

Customer #     Total Sales     Commission

30            0.000000         0.000000


This prob isnt hard for most of you but I have ALWAYS sucked deeply at looping for some odd reason.

Thanks again, will be looking forward to comments.
  #7  
Old 04-Feb-2006, 03:54
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: Sample looping (do/while)


Quote:
Originally Posted by Sabin044
This prob isnt hard for most of you but I have ALWAYS sucked deeply at looping for some odd reason.

Yeah, it happens ...
It took me lots of time to understand the concept of a ... pointer
and I still make silly mistakes, sometimes.

Sabin, there is an excellent explanation about control flow at K&R "The C Programming Langauge" - and before anyone kills me for offering you to read a "C book" in a "C++" thread, let me say : It's the principal that matters.
Once you understand for, if-else and while loops with one language, you will have no trouble will another (same level) language.

See ya,
Kobi
__________________
It's actually a one time thing (it just happens alot).
  #8  
Old 04-Feb-2006, 11:00
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Sample looping (do/while)


Quote:
Originally Posted by Sabin044
I do not understand how the user can enter amounts of an "item number" and "quantity" and have it calculate the "Total Sale" (at the bottom of report) all at once.

I'm basically thinking a loop is needed here, but I have not done any kinda loop like this before (i'm pretty sure i havent) yet my book does not explain loops in great detail...
Yes you have, when you accepted input for the names above. It's the same thing.

What you probably want to do is after you enter a name, enter your Item#, Quantity, and Unit Prices for that sales rep. Load the data into a matrix (2d array).

Then as you create the report, all your data is ready to go.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #9  
Old 04-Feb-2006, 11:39
Sabin044 Sabin044 is offline
Junior Member
 
Join Date: Oct 2005
Posts: 68
Sabin044 is on a distinguished road

Re: Sample looping (do/while)


Yes, that can make the user input multiple times, instead of showing the "Rep" multiple times.

I was thinking about using functions, but I do not really see a use.

I'm going to be working on this again after i get off work tonight,, gonna be a long day

Thanks kobi/walt + everyone
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ask looping likeit C Programming Language 4 26-Jul-2005 19:49
Signed Key Sample thatsalok C++ Forum 0 06-Dec-2004 22:13
Simple FLTK dialog box sample cable_guy_67 FLTK Forum 4 02-Nov-2004 08:46
for looping prob Rosmayati C Programming Language 1 30-Jun-2004 11:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:52.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.