473,320 Members | 1,979 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,320 software developers and data experts.

Question about looping with multiple variables

1
Basically, I am teaching myself how to program in C++ (after having done lots of work in Visual and qBasic) and am currently learning about loops. One of the programming questions I have deals with prompting the user to enter a value for each variable (variable1, variable2, variable3 etc.) and then, if the value entered is less than 0 or greater than 100, prompt them to re-enter it. So it should look like this:

Enter Q1: 1
Enter Q2: 3
Enter Q3: 200
Re-enter amount
Enter Q3: 100

And so on. I am suppose to do this using a while statement. How do I do this?
Jan 29 '08 #1
2 1328
Ganon11
3,652 Expert 2GB
Well, you know you have to use a loop to do this. Whenever you are creating a loop, there are three things you must consider:

1) What do you want to do inside the loop? What action (might) need to be repeated multiple times?
2) When do you want your loop to keep going? Or, when do you want your loop to stop?
3) What are you going to do inside the loop to ensure that the end condition will change?
Jan 29 '08 #2
whodgson
542 512MB
A while loop with a body which includes elements like the following might work.
Expand|Select|Wrap|Line Numbers
  1.  {   cout<<"\nVariable: "<<count<<" ";//increments the variable name
  2.     cin>>n;
  3.     if(n<0||n>200)
  4.     {cout<<"Variable must be > 0 and < 200\n";
  5.      continue;}
  6.      count++;}
  7.  
continue causes the loop to cycle without incrementing count++ in the event that n is outside the 0 - 200 bounds.
Feb 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not...
4
by: Paul M | last post by:
Larry, i've got an xml file which has multiple cities as the top level item. When i read it in using XMLReader, and define the variables, as below, it finds the correct number of cities in the...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
10
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of...
7
by: Shapper | last post by:
Hello, I have a "Select Case MyVar" in which I define the values of an Array according to the value of MyVar. I need to use the Array Values in a Loop after End Select. It seems the Array is...
5
by: Crirus | last post by:
What do you think about this approaches, wich one is the best? For x As Integer = u.GetViewBounds.X To u.GetViewBounds.X + u.GetViewBounds.Width For y As Integer = u.GetViewBounds.Y To...
14
by: Coleen | last post by:
Hi All :-) We have an APSX application using VB.net as the code behind, which uses one or two session variables per page. These Session variables are passed to the final page and calculations...
16
by: akantrowitz | last post by:
In csharp, what is the correct locking around reading and writing into a hashtable. Note that the reader is not looping through the keys, simply reading an item out with a specific key: If i...
8
by: Perl Beginner | last post by:
I am using Win32. I have created an excel spreadsheet, formatted the columns and rows, and would like to write to the cells…all of this using Spreadsheet::WriteExcel. My issue is, my script is very...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.