hk3008

Distinguished
Jan 29, 2012
43
0
18,580
#include<cmath>
#include<iostream>

using namespace std;



int x1 = 1;
int x2 = 1;
int x3 = x1 + x2;

while true{
cout << x3 << " ";

then
x1 = x2;
x2 = x3;
x3 = x1+ x2;

cout << x1 << " " << x2 << " ";
}
return 0;

so I am trying to make a program increase the ingteger by one with each run on paper it seemed more functional but now I am confused

these are the reports the debugger from Visual 2010
1>c:\users\hk3008\documents\visual studio 2010\projects\increment\increment\increment.cpp(12): error C2059: syntax error : 'while'
1>c:\users\hk3008\documents\visual studio 2010\projects\increment\increment\increment.cpp(12): error C2143: syntax error : missing ';' before '{'
1>c:\users\hk3008\documents\visual studio 2010\projects\increment\increment\increment.cpp(12): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\hk3008\documents\visual studio 2010\projects\increment\increment\increment.cpp(22): error C2059: syntax error : 'return'

as well the only thing underlined in my code is the

while

so any and all help would be greatly appreciated thank you
 

hk3008

Distinguished
Jan 29, 2012
43
0
18,580
ok so I have

#include<cmath>
#include<iostream>

using namespace std;


int main ()
{

int x1 = 1;
int x2 = 1;
int x3 = x1 + x2;


while (true)


cout << x3 << " ";

x1 = x2;
x2 = x3;
x3 = x1+ x2;

cout << x1 << " " << x2 << " ";


return 0;
}

but I just get x2 repeating over and over again but I want it to increase by 1 with each entry
 

randomizer

Distinguished
You didn't put braces around the statements inside the while loop. You need to combine your first and latest solutions.

Code:
while (true)
{
    DoSomething();
    DoSomethingElse();
}

In the above example, the two method calls inside the loop will (for all intents and purposes) be repeated called forever.

In the following:

Code:
while (true)
    DoSomething();

NeverGoingToDoThis();

Line 4 will never be reached. If you don't add braces, the loop only applies to the statement directly after it. This is the same with all control structures, including for and foreach loops as well as if-else blocks. I don't recommend leaving out braces even if there is only one statement that you want to execute inside the loop because it can lead to misunderstanding of the code. At the very least, make sure that statements inside a loop or if-else block are indented and on a new line. There's nothing worse than seeing this sort of thing:

Code:
if (some_condition) DoSomething(); DoSomethingElse();

I had to reformat some really old code like that in a commercial application (one that Just Works... but nobody knows how it still does). It required carefully prying each line apart to make sure that I didn't alter the logic.
 

hk3008

Distinguished
Jan 29, 2012
43
0
18,580
Ok so I put the brackets in but the logic still seems to be off as well I added the if then maybe that was not the right move any ideas?

#include<cmath>
#include<iostream>

using namespace std;


int main ()
{

int x1 = 1;
int x2 = 1;
int x3 = x1 + x2;
while (true)
{
cout << x3 << " ";
if
int x1 = x2;
int x2 = x3;
then
int x3 = x1+ x2;

cout << x1 << " " << x2 << " ";
}

return 0;
}
 

randomizer

Distinguished
I think you need to go back and re-read the first few chapters of your textbook. Many of your mistakes (across multiple threads) are syntactic, and simply giving you the right answer for your assignments isn't going to help you learn the syntax. Pay close attention to compiler messages as they usually tell you what is wrong (admittedly they are sometimes cryptic but you eventually learn what the compiler means).

Once you have the syntax nailed you can focus on correcting logic problems, eg. unexpected output from a program that compiles and runs without giving errors.
 

theDanijel

Distinguished
May 4, 2011
74
0
18,590
I've seen these kind of mistakes beafore, especialy from people going from pascal or basic to C/C++. I have to agree with randomizer, you are missing a lot on basic C/C++ syntax...
 

hk3008

Distinguished
Jan 29, 2012
43
0
18,580
Ok it was actually something we did on paper in class as well made us do a chart on paper to show the increase about a month ago I was just trying to work it out in the computer and when he put it in visual studio in class he had the almost exact same code almost I was just writing it down on paper and missed a few items I wanted to get it to work for my self but cant seem to get the chucks I missed or maybe the algorythm right plus it was so long ago I cant remember if he even used if then statements I think i was just trying to get it to work