|
|
|
#1 (permalink) | |||||||||||||
|
Web Designer
![]() |
I'm making a program for my C++ class right now and I hit a roadblock.
This is what the program will do: - Gets a number from a user - The number goes into the variable cityAmount - A loop runs for the amount that cityAmount is. (If cityAmount is 5, the loop will run 5 times). I'm thinking I should use a for loop (right?), but I'm stumped as to what I need to type inside the parentheses of the loop. Yes, I know, a very nooby question. Thanks all for the help. After I get this question answered, I'll probably ask a couple more.
__________________
Last edited by ImmortalKenny : 03-29-09 at 06:11 PM |
|||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
:wheee:
![]() |
You want to use a loop that creates an int that's set to 0, have it run as long as the int is less than cityAmount, and then increment the int by one after every pass.
__________________
Woodwind instruments are "all your money are belong to us"ing my wallet.
$2,500+ for a half decent oboe? ![]()
|
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||
|
You what?
![]() |
I'm not too good on c++, but you basically want it to do this:
1) Ask for number 2) Set number to cityAmount 3) If cityamount is not 0 or below, continue running script: 3a) Subtract 1 from cityAmount
__________________
THE Mouse FAQ | 32-bit Resolution Fix | Important Information 64-Bit Driver Signing Fix | The Infraction and Warning System My Anime Progress | The HoN Discussion Thread Please direct all tech related questions to a thread in the respectable forums, and not to my PM inbox. Thank you
|
|||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
:wheee:
![]() |
That would work, but I would personally keep cityAmount intact for further manipulation/use in the program.
__________________
Woodwind instruments are "all your money are belong to us"ing my wallet.
$2,500+ for a half decent oboe? ![]()
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
NIF
![]() |
it's been a while since C++ classes for me, but something along these lines?
![]() Code:
int x;
cout << "enter a number" << endl;
cin >> cityAmount;
for (x = 0; x < cityAmount; x++) {
cout << "loopity =p" << endl;
}
for (this number; while this number is compared to another; then do this) { blah }
Last edited by smoke12291 : 03-29-09 at 06:17 PM |
|||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
:wheee:
![]() |
You shouldn't do other people's homework, you should teach them how to do it.
__________________
Woodwind instruments are "all your money are belong to us"ing my wallet.
$2,500+ for a half decent oboe? ![]()
|
|||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||||
|
66MHz
![]() |
this is the for loop structure:
Code:
for (<command 1>; <command 2>; <command 3>)
{
.....stuff you want looped goes here......
}
<command 2>: This command is executed before each new run through the loop, and if it turns up false it stops running the loop <command 3>: This command is executed at the end of each run through the loop So in your case, you could do this: for (int counter = 1; counter <= cityAmount; counter++) Just remember you don't have to use an int and counter, you can stick any valid C++ commands you want in those spaces, including function calls.
__________________
For Sale: [Conus] CM Storm Scout, TEC, WC, Laptop, A900, Model M, GPU, Audio, and a ton more!!
|
||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||||
|
Web Designer
![]() |
Quote:
Quote:
![]() Now the next step of this program is to have the user enter temperatures for the amount that the user asked. If the user entered 5 cities, the console would ask the user to: "Enter city temp 1: " "Enter city temp 2: " "Enter city temp 3: " "Enter city temp 4: " "Enter city temp 5: " Each temperature that they enter goes into its own variable. Then an average for the city temperatures gets calculated. I have the loop running perfectly, I'm stuck at the part where I need to create separate variables for each city's temperature.
__________________
|
|||||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
PC Gamer
|
In that case, you need to use an array. For reference to C++ topics and basic primers go here: http://www.cplusplus.com/
|
|||||||||||||
|
|
|
|
|
#10 (permalink) | |||||||||||||||
|
Web Designer
![]() |
Quote:
This is the assignment: Quote:
__________________
Last edited by ImmortalKenny : 03-29-09 at 06:59 PM |
|||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|