home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

c++ for loop help

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1962
Discussions: 200,936, Posts: 2,379,215, Members: 246,300
Old November 16th, 2004, 01:08 AM   Digg it!   #1 (permalink)
Member
 
Join Date: Nov 2004
Posts: 56
c++ for loop help

i'm writing a simple for loop for extra credit. it is supposed to print to the screen only the even numbers from 2 - 100. this is what i have and it is infinite looping. i'm using dev c++ version 4.0.

#include <iostream.h>
#include <stdlib.h>

int main()
{
int num;

for(num=2;num<=100;num+2)
{
cout << num <<"\n";
}

system("PAUSE");
return 0;
}
help is appreciated,
thanks
sirhc
boogieman063 is offline   Reply With Quote
Old November 16th, 2004, 01:20 AM     #2 (permalink)
Senior Member
 
TechKnickle's Avatar
 
Join Date: Aug 2004
Location: LA, California
Posts: 812
Send a message via AIM to TechKnickle Send a message via MSN to TechKnickle
Try this:

Code:
#include <iostream.h>
#include <stdlib.h>

int main()
{
int num;

for(num=2;num<=100;num+=2)
{
cout << num <<"\n";
}

system("PAUSE");
return 0;
}
Did you want it to loop?

Remember in c++ you have to put the += together if you are adding numbers together (proper syntax).
__________________
People are like coins, there's always two sides.

Last edited by TechKnickle : November 16th, 2004 at 01:24 AM.
TechKnickle is offline   Reply With Quote
Old November 16th, 2004, 02:21 AM     #3 (permalink)
Member
 
Join Date: Nov 2004
Posts: 56
no it was not supposed to loop, thanks that was perfect. let me ask you though, why does it have to be +=, it works, but how?
boogieman063 is offline   Reply With Quote
Old November 16th, 2004, 02:30 AM     #4 (permalink)
Senior Member
 
TechKnickle's Avatar
 
Join Date: Aug 2004
Location: LA, California
Posts: 812
Send a message via AIM to TechKnickle Send a message via MSN to TechKnickle
Yeah, sure no problem.

In c++, the '+' (unary) by itself effectively does nothing. Same concept with the '-', but this does something, it doesn't subtract though. It negates your statement (the value entered or computed earlier).

+= is an assignment operator, telling the computer "this needs to be done, so do it now".

Basically.
TechKnickle is offline   Reply With Quote
Old November 16th, 2004, 03:15 AM     #5 (permalink)
Ultimate Member
 
lost-and-found's Avatar
 
Join Date: Oct 2001
Location: Illinois
Posts: 2,977
Send a message via AIM to lost-and-found
basically what you had was a temporary statement: add 2 to num and continue. += says: add 2 to num and store the result as num.
__________________
lost-and-found is offline   Reply With Quote
Old November 17th, 2004, 02:20 PM     #6 (permalink)
Ultimate Member
 
elmers's Avatar
 
Join Date: Sep 2003
Location: Philadelphia
Posts: 1,484
"num+=2" means "num=num+2"
elmers is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
My first application, try and suggest.. TechKnickle Webmastering and Programming 24 October 1st, 2004 09:39 AM
Kinda cool web page ooops IMO Community 10 May 11th, 2004 05:27 PM
Excel Macro help jjst34 Applications and Operating Systems 15 October 3rd, 2003 12:10 PM
My lastest web site - opinions please... edwelly Webmastering and Programming 27 June 7th, 2003 08:52 AM
FLASH and audio? aznlurkerz Graphic Design and Digital Photography 7 June 29th, 2002 03:47 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2892)
Wireless Televisions. (8)
California Passes Anti-Flat-HDTV Le.. (39)
Obama the Muslim (14)
Is the PSU I received dead? (10)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
HIS HD5770 graphic card question (15)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
EVGA 9800 gtx help with finding a g.. (8)
New Computer wont recognize XP disc (7)
Recent Discussions
windows 7 problem (3)
Point and Shoot Camera Suggestions. (1)
Wireless Televisions. (8)
CPU fan stops spinning randomly (3)
wireless user (1)
Modern Warfare 2 freeze (12)
World's largest Monopoly Game using G.. (332)
Ideal cheap graph card for PC-Gaming? (17)
BIOS won't read disk when I try to fl.. (0)
Install XP pro and a Vista laptop ?? (11)
Partition Magic caused HDD problem (2)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Regular Build (3)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Have you switched yet? (84)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)


All times are GMT -4. The time now is 10:48 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28