learning C++
Moderators: Public Relations Department, Players Department
- fishfin
- Posts: 490
- Joined: Mon Mar 20, 2006 12:38 pm
- Location: Nanning, China
-
- Posts: 97
- Joined: Tue Jun 29, 2004 4:08 pm
- fishfin
- Posts: 490
- Joined: Mon Mar 20, 2006 12:38 pm
- Location: Nanning, China
- N-Aldwitch
- Posts: 1771
- Joined: Thu Mar 16, 2006 1:48 am
- Contact:
fishfin wrote:I tryed this:
if ( x > 4 ) || ( x < 1 ) {
cout<<"Sorry, '"<<x <<"' is not a recognized command. Please try again.";
}
and I got these erors:
expected primary extension before '||' tolken
expected ';' before '{' tolken
I'll put this into English for you.
expected ';' before '{' tolken
It expected to see a ';'before the '{' on the same line. And tolken is the describing word.
So your line of code should obviously read
if ( x > 4 ) || ( x < 1 ); {
rather than
if ( x > 4 ) || ( x < 1 ) {
Nakranoth's "evil" character says:
"Thief! That's terrible! *shakes his head* That would hurt people's feeling if I did that."
http://www.sylorn.com - Free MMORPG in development.. need help.
"Thief! That's terrible! *shakes his head* That would hurt people's feeling if I did that."
http://www.sylorn.com - Free MMORPG in development.. need help.
- fishfin
- Posts: 490
- Joined: Mon Mar 20, 2006 12:38 pm
- Location: Nanning, China
- Birdsall007
- Posts: 118
- Joined: Tue Nov 30, 2004 12:40 pm
- Location: Northampton, England
fishfin wrote:I tryed this:
if ( x > 4 ) || ( x < 1 ) {
cout<<"Sorry, '"<<x <<"' is not a recognized command. Please try again.";
}
...snip...
There's at least one other problem here, whenever you have an if statement in C++, the whole test must be enclosed in parenthesis (brackets). The correct way to code this would be...
Code: Select all
if (x > 4 || x < 1)
{
cout << "Sorry, '" << x << "' is not a recognized command. Please try again.";
}
If you want each element in the test to be bracketed, then you would use this syntax...
Code: Select all
if ( (x > 4) || (x < 1) )
This isn't really necessary unless you have some specific case that you want to test that doesn't follow the standard order of precendence, ie, which is tested first. Again, another example...
Code: Select all
if ( x > 4 || x < 1 && y == 2)
... will be true if x is not 1 through 4, and y is 2. If you wanted the y=2 test to only be carried out when x is less than 1, the syntax is...
Code: Select all
if ( x > 4 || (x < 1 && y == 2) )
I hope this helps
On the question of whether the Microsoft compiler is free, yes it is. At least, it will be up until about September. I'm assuming that you are running your computer with some version of MS Windows?
Even if the voices aren't real...They have some pretty good ideas!
- fishfin
- Posts: 490
- Joined: Mon Mar 20, 2006 12:38 pm
- Location: Nanning, China
Thanks, that works now, but I still haven't figured out how to run the same line of code over and over again...
and another thing, is it possible to change a variable to say 6 the first time you run the program and have the program remember 6 for the second time and if the second time you change the variable from 6 to 7 for the program to remember 7 the third time you run it?

and another thing, is it possible to change a variable to say 6 the first time you run the program and have the program remember 6 for the second time and if the second time you change the variable from 6 to 7 for the program to remember 7 the third time you run it?
The following statement is not true.
The previous statement is not true.
The previous statement is not true.
-
- Posts: 97
- Joined: Tue Jun 29, 2004 4:08 pm
One way to have something repeat:
This will do the "...codecodecode..." part and repeat it as long as you don't input 0 in the end.
I assume you mean you shut down the program between. In that case you have to save it to a file and retrieve from it the next time.
Code: Select all
bool done = false;
while (!done) {
...codecodecode...
int input;
cin >> input;
if (input == 0)
done = true;
}
This will do the "...codecodecode..." part and repeat it as long as you don't input 0 in the end.
and another thing, is it possible to change a variable to say 6 the first time you run the program and have the program remember 6 for the second time and if the second time you change the variable from 6 to 7 for the program to remember 7 the third time you run it?
I assume you mean you shut down the program between. In that case you have to save it to a file and retrieve from it the next time.
- Peanut
- Posts: 1155
- Joined: Mon Oct 18, 2004 3:01 pm
&& and || (and and or)
Denote operation between different brackets.
So they require a bracket for themselves.
Ie ((x == 1) || (x == 2))
Notice the extra () encapsuling the other brackets?
I'd suggest finding a good basic c++ (e)book with perhaps a standard compiler.
Though it will probably cost as most freeware compilers require additional knowledge.
Ie like devcpp requiring manual library linking.
Denote operation between different brackets.
So they require a bracket for themselves.
Ie ((x == 1) || (x == 2))
Notice the extra () encapsuling the other brackets?
I'd suggest finding a good basic c++ (e)book with perhaps a standard compiler.
Though it will probably cost as most freeware compilers require additional knowledge.
Ie like devcpp requiring manual library linking.
- Birdsall007
- Posts: 118
- Joined: Tue Nov 30, 2004 12:40 pm
- Location: Northampton, England
Peanut wrote:&& and || (and and or)
Denote operation between different brackets.
So they require a bracket for themselves.
Ie ((x == 1) || (x == 2))
Notice the extra () encapsuling the other brackets?
It will work fine like this, but it is not necessary. Mind you, how the code looks is all down to the individual developer.

Code: Select all
(x == 1 || x == 2)
I just think it looks neater.
Even if the voices aren't real...They have some pretty good ideas!
- Peanut
- Posts: 1155
- Joined: Mon Oct 18, 2004 3:01 pm
fishfin wrote:Thanks, that works now, but I still haven't figured out how to run the same line of code over and over again...
and another thing, is it possible to change a variable to say 6 the first time you run the program and have the program remember 6 for the second time and if the second time you change the variable from 6 to 7 for the program to remember 7 the third time you run it?
http://www.cprogramming.com/tutorial/lesson10.html
- fishfin
- Posts: 490
- Joined: Mon Mar 20, 2006 12:38 pm
- Location: Nanning, China
That was just what I was looking for, thanks.
I probably didn't explane very well what I wanted to do, I want to be able to go back and start running code from line 20 when I get line 100 kind of like it does lots of things (lines 1-99) but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99...
And how do you make 'x' something other than a number?
I probably didn't explane very well what I wanted to do, I want to be able to go back and start running code from line 20 when I get line 100 kind of like it does lots of things (lines 1-99) but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99...
And how do you make 'x' something other than a number?
The following statement is not true.
The previous statement is not true.
The previous statement is not true.
-
- Posts: 208
- Joined: Tue Mar 14, 2006 9:02 am
fishfin wrote:I probably didn't explane very well what I wanted to do, I want to be able to go back and start running code from line 20 when I get line 100 kind of like it does lots of things (lines 1-99) but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99...
There is a instruction that you could use for that. It's "goto".
Code: Select all
Somelabel:
instructioninstruction...
if (something) {
goto Somelabel;
}
BUT: it is strongly discouraged to use this instruction because it is VERY hard to write easy to comprehend piece of code with goto instructions. If you need to use goto, you should restrict it to forward jump only.
fishfin wrote:And how do you make 'x' something other than a number?
You can declare various types of variables - like int, double (real number), characters, arrays of numbers, arrays of characters..
Try this link:
http://cplus.about.com/od/beginnerctutorial/l/aa021202a.htm[/i]
- Peanut
- Posts: 1155
- Joined: Mon Oct 18, 2004 3:01 pm
fishfin wrote:That was just what I was looking for, thanks.
I probably didn't explane very well what I wanted to do, I want to be able to go back and start running code from line 20 when I get line 100 kind of like it does lots of things (lines 1-99) but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99 but on line 100 there is some code telling it to go back to line 20 so it runs code line 20-99...
And how do you make 'x' something other than a number?
http://www.cprogramming.com/tutorial/lesson3.html
http://www.cprogramming.com/tutorial/lesson4.html
- Birdsall007
- Posts: 118
- Joined: Tue Nov 30, 2004 12:40 pm
- Location: Northampton, England
Gender wrote:BUT: it is strongly discouraged to use this instruction because it is VERY hard to write easy to comprehend piece of code with goto instructions. If you need to use goto, you should restrict it to forward jump only.
I totally agree with this statement. If I ever find a goto in any of the code that I maintain, the second thing that I do is remove it. The first is taking out a contract on the developer who wrote it in the first place!


Goto is worse than evil!
If you are using a goto to always skip some code, the code that is skipped needs to be removed. Even if it's only commented out, it's enough.
If you are using it as the result of an if test, test for the opposite of what you want to do, eg...
Code: Select all
if (a == 1)
goto ....
becomes
Code: Select all
if (a != 1)
{
....
}
If you use goto, it will come back and bite you.
Even if the voices aren't real...They have some pretty good ideas!
Return to “Non-Cantr-Related Discussion”
Who is online
Users browsing this forum: No registered users and 1 guest