learning C++

General chitchat, advertisements for other services, and other non-Cantr-related topics

Moderators: Public Relations Department, Players Department

User avatar
fishfin
Posts: 490
Joined: Mon Mar 20, 2006 12:38 pm
Location: Nanning, China

Postby fishfin » Sun Jun 11, 2006 2:26 pm

My last two questions have not yet been answered (so feel free to answer them if you know the answer)

here's another one, is it possible to run a program in C++ without having the compiler open?
The following statement is not true.

The previous statement is not true.
Lord_Igor
Posts: 97
Joined: Tue Jun 29, 2004 4:08 pm

Postby Lord_Igor » Sun Jun 11, 2006 5:50 pm

(x > 7 || < 52) should give you a compilation error.

(x > 7) || (x < 52) is the way to do it.

Once you have compiled a program you should have a file named a.out or myprogram.exe which you can run without the compiler. Obviously you should replace "myprogram" with whatever you call it.
User avatar
fishfin
Posts: 490
Joined: Mon Mar 20, 2006 12:38 pm
Location: Nanning, China

Postby fishfin » Sun Jun 11, 2006 11:03 pm

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
The following statement is not true.



The previous statement is not true.
User avatar
N-Aldwitch
Posts: 1771
Joined: Thu Mar 16, 2006 1:48 am
Contact:

Postby N-Aldwitch » Mon Jun 12, 2006 1:24 am

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.
User avatar
fishfin
Posts: 490
Joined: Mon Mar 20, 2006 12:38 pm
Location: Nanning, China

Postby fishfin » Mon Jun 12, 2006 1:53 am

How do I say go back to line X and start over from there (or something like that)?

(so I could do stuff like show an always growing amount of 'hello world')
The following statement is not true.



The previous statement is not true.
User avatar
Birdsall007
Posts: 118
Joined: Tue Nov 30, 2004 12:40 pm
Location: Northampton, England

Postby Birdsall007 » Mon Jun 12, 2006 8:46 am

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!
User avatar
fishfin
Posts: 490
Joined: Mon Mar 20, 2006 12:38 pm
Location: Nanning, China

Postby fishfin » Mon Jun 12, 2006 9:04 am

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?
The following statement is not true.



The previous statement is not true.
Lord_Igor
Posts: 97
Joined: Tue Jun 29, 2004 4:08 pm

Postby Lord_Igor » Mon Jun 12, 2006 1:34 pm

One way to have something repeat:

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.
User avatar
Peanut
Posts: 1155
Joined: Mon Oct 18, 2004 3:01 pm

Postby Peanut » Mon Jun 12, 2006 2:12 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.
User avatar
Birdsall007
Posts: 118
Joined: Tue Nov 30, 2004 12:40 pm
Location: Northampton, England

Postby Birdsall007 » Mon Jun 12, 2006 4:39 pm

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. :D Personally I prefer...

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!
User avatar
Peanut
Posts: 1155
Joined: Mon Oct 18, 2004 3:01 pm

Postby Peanut » Mon Jun 12, 2006 4:51 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
User avatar
fishfin
Posts: 490
Joined: Mon Mar 20, 2006 12:38 pm
Location: Nanning, China

Postby fishfin » Mon Jun 12, 2006 11:36 pm

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?
The following statement is not true.



The previous statement is not true.
Gender
Posts: 208
Joined: Tue Mar 14, 2006 9:02 am

Postby Gender » Tue Jun 13, 2006 7:25 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]
User avatar
Peanut
Posts: 1155
Joined: Mon Oct 18, 2004 3:01 pm

Postby Peanut » Tue Jun 13, 2006 7:29 am

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
User avatar
Birdsall007
Posts: 118
Joined: Tue Nov 30, 2004 12:40 pm
Location: Northampton, England

Postby Birdsall007 » Tue Jun 13, 2006 8:46 am

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! ;) :twisted:

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