|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
[RESOLVED] C++ For loop problem
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
AMD Overclocker
![]() |
Code:
int main ()
{
int Even = 0;
int Odd = -1;
int Breakout = 101;
int OddTotal = Odd;
cout << "This program will add the odd numbers between 0 & 100." << endl;
for Odd < 101
Odd = Odd + 2;
Even = Even + 2;
OddTotal = OddTotal + Odd;
cout << "The current total of the odd numbers is " << OddTotal << "." << endl;
cout << endl;
system("PAUSE");
}
Error 1 error C2061: syntax error : identifier 'Odd' the error is referring to the int Odd that is in the For line. I cant find anyway to get this to go away or to compile right, any help would be nice.
__________________
955BE @ 3.7 \ \ \ Possibly Epic Thread \ \ \ AMD Dragon \ \ \ bow before your master....meticadpa \ \ \ Epic Thread
Last edited by NameUnknown : 10-14-09 at 03:54 PM |
||||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
AMD Overclocker
![]() |
Oh and ive put the Odd < 101 in parenthesis, ive had it use the Breakout integer as well and it still fails, if I have parenthesis on there it gives me errors calling for semi colons before and after also.
__________________
955BE @ 3.7 \ \ \ Possibly Epic Thread \ \ \ AMD Dragon \ \ \ bow before your master....meticadpa \ \ \ Epic Thread
|
||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
2 + 2 = 5
![]()
Join Date: Nov 2006
Location: In a Chair.
Posts: 34,951
Rep: 4174
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Unique Rep: 1909
Trader Rating: 56
|
Curly Brackets.
You can make the code simplier using Odd++ and the modulus function.
__________________
To answer most of your questions: (1) a fridge cannot cool a PC (2) 64-bit OS for over 3.4GB (3) If a PCIe card fits, it should work (4) Resolution, not screen size (5) If you have a question, it is not news (6) Report, not respond to Spam (7) Single-Rail/Non-Modular PSUs are not always better than Multi-Rail/Modular
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
The Dapper Swindler
![]() |
I'm guessing you used to program a lot in python?
"for Odd < 101" is not even close to C++ syntax. What you're looking for is while(Odd<101) { //the other code goes BETWEEN curly braces}
|
|||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||
|
AMD Overclocker
![]() |
no need for a modulus just getting the sum of the odd numbers, which is why i cant really use Odd++, and just personal taste but I was never a fan of ___++ to add a variable to itself, I prefer seeing all the math laid out like it would be if it were written on paper.
__________________
955BE @ 3.7 \ \ \ Possibly Epic Thread \ \ \ AMD Dragon \ \ \ bow before your master....meticadpa \ \ \ Epic Thread
|
||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
Intel Overclocker
![]() |
woa
its def for(int a = 0; a < 10; a++) { } or int a = 0; while(a < 10) { a++; } essentially the same thing
__________________
|
|||||||||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||||
|
AMD Overclocker
![]() |
Quote:
Quote:
__________________
955BE @ 3.7 \ \ \ Possibly Epic Thread \ \ \ AMD Dragon \ \ \ bow before your master....meticadpa \ \ \ Epic Thread
|
||||||||||||||
|
|
|
|
|
#8 (permalink) | ||||||||||||
|
*cough* Stock *cough*
|
if you want to use a for loop you might try something like this
int sum=0; for(int i=1;i<101;i += 2){ sum += i; } this will take i through all the odd numbers from 0-100, your current format would work with a while loop.
__________________
Last edited by Sepiida : 10-14-09 at 01:09 PM |
||||||||||||
|
|
|
|
|
#9 (permalink) | ||||||||||||||
|
2 + 2 = 5
![]()
Join Date: Nov 2006
Location: In a Chair.
Posts: 34,951
Rep: 4174
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Unique Rep: 1909
Trader Rating: 56
|
Quote:
Also, why waste time when there are typing shortcuts? Also, Odd++ runs faster than Odd = Odd + 1. (Unfortunately, I haven't touched C++ in a few years but I understand good programming habits. )
__________________
To answer most of your questions: (1) a fridge cannot cool a PC (2) 64-bit OS for over 3.4GB (3) If a PCIe card fits, it should work (4) Resolution, not screen size (5) If you have a question, it is not news (6) Report, not respond to Spam (7) Single-Rail/Non-Modular PSUs are not always better than Multi-Rail/Modular
|
||||||||||||||
|
|
|
|
#10 (permalink) | ||||||||||||||
|
The Dapper Swindler
![]() |
Quote:
What he was saying is it would be easier. Code:
#include <iostream>
int main*(
{
That does the same thing I believe.
|
||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|