Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 03-29-09   #1 (permalink)
Web Designer
 
ImmortalKenny's Avatar
 
amd ati

Join Date: Mar 2006
Location: Arizona
Posts: 6,146

Rep: 524 ImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famous
Unique Rep: 391
Hardware Reviews: 6
Trader Rating: 21
Default Basic C++ loop help

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.

System: ImmortalPuter
CPU
AMD Phenom II X2 550
Motherboard
DFI LP JR 790GX-M3H5
Memory
OCZ Gold 4GB (2x2) DDR3-1600
Graphics Card
XFX 4890 XXX
Hard Drive
Seagate 300GB + 320GB External
Sound Card
Creative XtremeMusic
Power Supply
OCZ GameXStream 700w
Case
Antec Mini P180 (Black)
CPU cooling
Noctua NH-U12P, 2x Ultra Kaze 3k's
GPU cooling
Stock
OS
Windows 7 Pro x64
Monitor
Acer H233H 1080p

Last edited by ImmortalKenny : 03-29-09 at 06:11 PM
ImmortalKenny is online now Overclocked Account ImmortalKenny's Gallery   Reply With Quote
Old 03-29-09   #2 (permalink)
:wheee:
 
mrtn400's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: Carmichael, CA
Posts: 4,363
Blog Entries: 2

Rep: 279 mrtn400 is a proven membermrtn400 is a proven membermrtn400 is a proven member
Unique Rep: 237
Hardware Reviews: 1
Trader Rating: 1
Default

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?


System: Evolution (Alt Rig)
CPU
2x 3.06Ghz Xeon Prestonia (SL6VP) w/ HT off
Motherboard
MSI E7505 Master-LS2
Memory
4x 512MB Registered DDR-266
Graphics Card
BFG 7300GT 512MB @ 590Mhz Core/380Mhz Memory
Hard Drive
Hitachi 120GB
Sound Card
ASUS Xonar DX w/ shielding
Power Supply
450w
Case
Stock IBM
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP Professional SP3
Monitor
1x Acer 19" @ 1400x900, 1x Sceptre 19" @ 1280x1024
mrtn400 is offline Overclocked Account mrtn400's Gallery   Reply With Quote
Old 03-29-09   #3 (permalink)
You what?
 
gonX's Avatar
 
intel

Join Date: May 2006
Location: Tampere, Finland
Posts: 20,967
Blog Entries: 10

FAQs Submitted: 1
Hardware Reviews: 15
Trader Rating: 13
Default

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

System: Netbook - Fujisu Siemens Lifebook P7120
CPU
Pentium M ULV 1.2 GHz
Motherboard
Fujitsu Siemens
Memory
2x512MB Generic
Graphics Card
Intel 915GM
Hard Drive
60GB 5400RPM
Sound Card
E-MU Tracker|pre and Onboard
OS
Arch Linux i686
Monitor
11" LED-backlit LCD
gonX is offline Overclocked Account gonX's Gallery   Reply With Quote
Old 03-29-09   #4 (permalink)
:wheee:
 
mrtn400's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: Carmichael, CA
Posts: 4,363
Blog Entries: 2

Rep: 279 mrtn400 is a proven membermrtn400 is a proven membermrtn400 is a proven member
Unique Rep: 237
Hardware Reviews: 1
Trader Rating: 1
Default

Quote:
Originally Posted by gonX View Post
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
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?


System: Evolution (Alt Rig)
CPU
2x 3.06Ghz Xeon Prestonia (SL6VP) w/ HT off
Motherboard
MSI E7505 Master-LS2
Memory
4x 512MB Registered DDR-266
Graphics Card
BFG 7300GT 512MB @ 590Mhz Core/380Mhz Memory
Hard Drive
Hitachi 120GB
Sound Card
ASUS Xonar DX w/ shielding
Power Supply
450w
Case
Stock IBM
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP Professional SP3
Monitor
1x Acer 19" @ 1400x900, 1x Sceptre 19" @ 1280x1024
mrtn400 is offline Overclocked Account mrtn400's Gallery   Reply With Quote
Old 03-29-09   #5 (permalink)
NIF
 
smoke12291's Avatar
 
intel nvidia

Join Date: Apr 2008
Location: California
Posts: 3,486
Blog Entries: 3

Rep: 259 smoke12291 is a proven membersmoke12291 is a proven membersmoke12291 is a proven member
Unique Rep: 217
Folding Team Rank: 135
Trader Rating: 7
Default

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 a "for" loop think

for (this number; while this number is compared to another; then do this) {
blah
}
__________________
Computer Engineering Student!
learn how to fold here!
Quote:
Originally Posted by ENTERPRISE View Post
Fold For The Cause ! Fold For The People .
steam: [OCN]smoke12291

System: Main Baby
CPU
Intel Q6600 G0 3.2Ghz
Motherboard
Asus 750i P5N-D
Memory
G.Skill 2x2gb PC-8500 DDR2
Graphics Card
evga 9800gtx 512mb
Hard Drive
320gb 16mb cache
Sound Card
Realtek 7.1 (onboard)
Power Supply
Apevia Warlock 750 watt
Case
CoolerMaster 690
CPU cooling
Lapped Thermalright Ultra-120 Extreme
GPU cooling
Accelero S1 w/two 120mm fans
OS
Windows 7 Ultimate x64 / Ubuntu 9.04
Monitor
Samsung SyncMaster 932BW 19"
Overclock.net - 2009 Chimp Challenge Champions 1 Million+ Folding at Home points

Last edited by smoke12291 : 03-29-09 at 06:17 PM
smoke12291 is online now I fold for Overclock.net Overclocked Account   Reply With Quote
Old 03-29-09   #6 (permalink)
:wheee:
 
mrtn400's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: Carmichael, CA
Posts: 4,363
Blog Entries: 2

Rep: 279 mrtn400 is a proven membermrtn400 is a proven membermrtn400 is a proven member
Unique Rep: 237
Hardware Reviews: 1
Trader Rating: 1
Default

Quote:
Originally Posted by smoke12291 View Post
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;
}
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?


System: Evolution (Alt Rig)
CPU
2x 3.06Ghz Xeon Prestonia (SL6VP) w/ HT off
Motherboard
MSI E7505 Master-LS2
Memory
4x 512MB Registered DDR-266
Graphics Card
BFG 7300GT 512MB @ 590Mhz Core/380Mhz Memory
Hard Drive
Hitachi 120GB
Sound Card
ASUS Xonar DX w/ shielding
Power Supply
450w
Case
Stock IBM
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP Professional SP3
Monitor
1x Acer 19" @ 1400x900, 1x Sceptre 19" @ 1280x1024
mrtn400 is offline Overclocked Account mrtn400's Gallery   Reply With Quote
Old 03-29-09   #7 (permalink)
66MHz
 
Manyak's Avatar
 
intel ati

Join Date: Mar 2008
Posts: 7,068

Rep: 654 Manyak is becoming famousManyak is becoming famousManyak is becoming famousManyak is becoming famousManyak is becoming famousManyak is becoming famous
Unique Rep: 430
Folding Team Rank: 427
Trader Rating: 31
Default

this is the for loop structure:

Code:
for (<command 1>; <command 2>; <command 3>)
{
          .....stuff you want looped goes here......
}
<command 1>: This command is executed only once at the start of the loop, before any others
<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.

System: Obsidian Phoenix
CPU
Ci7 920 D0
Motherboard
E760 Classified
Memory
12GB G.Skill Titan DDR3-2000
Graphics Card
Waiting for HD5870x2
Hard Drive
1x Intel G2, 4x Intel G2, 1x Scorpio Black 320GB
Sound Card
Xonar D2X
Power Supply
Corsair 1000HX
Case
Corsair Obsidian 800D
CPU cooling
Heatkiller 3.0 Copper
OS
Windows 7 Ultimate
Monitor
3x Sony GDM-FW900 24" CRT's
Manyak is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 03-29-09   #8 (permalink)
Web Designer
 
ImmortalKenny's Avatar
 
amd ati

Join Date: Mar 2006
Location: Arizona
Posts: 6,146

Rep: 524 ImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famous
Unique Rep: 391
Hardware Reviews: 6
Trader Rating: 21
Default

Quote:
Originally Posted by mrtn400 View Post
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.
Quote:
Originally Posted by Manyak View Post
this is the for loop structure:

Code:
for ( <command 1>; <command 2>; <command 3>)
{
          .....stuff you want looped goes here......
}
<command 1>: This command is executed only once at the start of the loop, before any others
<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++)

But generally speaking you don't have to keep it this simple, you can stick any valid C++ commands you want in those spaces, including function calls.
Thanks you two for the help!

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.

System: ImmortalPuter
CPU
AMD Phenom II X2 550
Motherboard
DFI LP JR 790GX-M3H5
Memory
OCZ Gold 4GB (2x2) DDR3-1600
Graphics Card
XFX 4890 XXX
Hard Drive
Seagate 300GB + 320GB External
Sound Card
Creative XtremeMusic
Power Supply
OCZ GameXStream 700w
Case
Antec Mini P180 (Black)
CPU cooling
Noctua NH-U12P, 2x Ultra Kaze 3k's
GPU cooling
Stock
OS
Windows 7 Pro x64
Monitor
Acer H233H 1080p
ImmortalKenny is online now Overclocked Account ImmortalKenny's Gallery   Reply With Quote
Old 03-29-09   #9 (permalink)
PC Gamer
 
intel nvidia

Join Date: Oct 2008
Posts: 103

Rep: 3 psycho12345 Unknown
Unique Rep: 3
Folding Team Rank: 800
Trader Rating: 0
Default

In that case, you need to use an array. For reference to C++ topics and basic primers go here: http://www.cplusplus.com/

System: Artic Ice House
CPU
Conroe E6550
Motherboard
GIGABYTE GA-EP45-UD3R
Memory
7 GB G. Skill PC 6400
Graphics Card
Evga 8800 GTS 512
Hard Drive
2x 150 GB VelociRaptor, 2x 500 GB WD Caviar
Sound Card
Integrated Azalia Audio
Power Supply
Thermaltake ToughPower 750W
Case
Thermaltake Armor 8003BWS
CPU cooling
TRUE Black (lapped)
GPU cooling
Stock
OS
Windows 7 x64 Professional
Monitor
KDS 24"
psycho12345 is offline I fold for Overclock.net   Reply With Quote
Old 03-29-09   #10 (permalink)
Web Designer
 
ImmortalKenny's Avatar
 
amd ati

Join Date: Mar 2006
Location: Arizona
Posts: 6,146

Rep: 524 ImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famousImmortalKenny is becoming famous
Unique Rep: 391
Hardware Reviews: 6
Trader Rating: 21
Default

Quote:
Originally Posted by psycho12345 View Post
In that case, you need to use an array. For reference to C++ topics and basic primers go here: http://www.cplusplus.com/
Hmm, I knew I could use an array, but the assignment never mentioned anything about it. Is there any way around using arrays?

This is the assignment:
Quote:
Lab Overview – Scenario / Summary: Formulate a C++ program solution that asks the user for how many cities to calculate an average temperature for; then do the calculation and the print out. Techniques required in this Lab include the usage of a loop of your choice and 2 methods. The first method is for returning the temperature the user inputted for a given city, but which does not require any parameters. The other method (which should require parameters) is for calculating the average temperature across all cities but does not return the value back; consequently it does a print out of the value.

System: ImmortalPuter
CPU
AMD Phenom II X2 550
Motherboard
DFI LP JR 790GX-M3H5
Memory
OCZ Gold 4GB (2x2) DDR3-1600
Graphics Card
XFX 4890 XXX
Hard Drive
Seagate 300GB + 320GB External
Sound Card
Creative XtremeMusic
Power Supply
OCZ GameXStream 700w
Case
Antec Mini P180 (Black)
CPU cooling
Noctua NH-U12P, 2x Ultra Kaze 3k's
GPU cooling
Stock
OS
Windows 7 Pro x64
Monitor
Acer H233H 1080p

Last edited by ImmortalKenny : 03-29-09 at 06:59 PM
ImmortalKenny is online now Overclocked Account ImmortalKenny's Gallery   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 10:31 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.20842 seconds with 8 queries