English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

All of my programming experience was in highschool on TI-83s and such. I am now learning to write in c++ and am doing pretty good at learning the new terminology.

A major problem I am having is that I always relied heavily on the Lbl and Goto functions. Particularly, I would perform logic tests like:

IF X=1
Goto A
If X=2
Goto B
If X=3
Goto C

How do I do this in C++? If I want my program to jump to another part of my program what do I do?

2007-02-22 14:11:34 · 3 answers · asked by cpine505 3 in Computers & Internet Programming & Design

3 answers

Welcome to structured programming! There is no such thing as a goto in c++!

You have to learn to functionalize your code.

For example:

if (x==1) dothisfunction (withthisvariable);

Then you would have a function:

int dothisfunction(int withthisvariable){
doing something happens here;
return (somevalue);
}

Here is a sample program that uses a function to square or cube a number.

#include
//function prototypes
int square(int);
int cube(int);

//main program
int main (void) {
int number;
char choice;

cout>>"Enter a number: ";
cin< cout>>"Type s to square it or c to cube it: ";
cin<
if (choice=='s') cout<<"Square of "<< number << " is " << square(number)< if (choice=='c') cout<<"Cube of "<< number << " is " << cube(number)<
}

//square function
int square(int number){
return(number*number);
}
//cube function
int cube (int number){
return(number*number*number);
}

2007-02-22 14:16:11 · answer #1 · answered by Amanda H 6 · 0 0

The example you've given is good candidate for a
'switch' statement. Would be far easier and clearer
than all those IF statements. As to Goto ..

.. well, they are considered 'bad programming style'
as they say. But hey, if you want to do them, and be
unstructured, then why not! I am wondering, quite
how you manage to keep track of what your program
is doing though, and for that matter, where its going!
Must be difficult. However, I digress..

Link below gives some examples of what you are
looking for. The site itself seems to be a good clear
resource for other things C++ as well. Worth a
deeper look maybe?

HTH

2007-02-22 15:17:39 · answer #2 · answered by Anonymous · 0 0

660 is extra useful than the 670 in cost/overall performance ratio, yet once you dont care that lots on the subject of the fee, the 670 is extra useful needless to say. My GTX 660 can run each and every sport you point available on the extremely settings (some determination on some distance Cry 3 on severe), around 40-50fps. you will get around 8-14 extra fps with the 670, so its not that lots of a distinction

2016-12-17 16:45:41 · answer #3 · answered by ? 4 · 0 0

fedest.com, questions and answers