#include <iostream>
using namespace std;

int main ()
{
    bool a = true;
    cout << ("Multiply = 1 or Divide = 2 ?\n");
    cin >> a;
    if( a = "true")
    {
        goto Multiply;
    } else {
        goto Divide;
    }
}

:Multiply
    double x;
    double y;
    cout << ("Please Enter The First Number ");
    cin >> x;
    cout << ("Please Enter The Second Number");
    cin >> y;
    cout << (x*y);
    return 0;

:Divide
    double x;
    double y;
    cout << ("Please Enter The Larger Number");
    cin >> x;
    cout << ("Please Enter The Smaller Number");
    cout << (x/y);
    return 0;

errors everywhere, using code::blocks on Ubuntu using g++ compiler...I think I just need to sort out the goto, all help greatly appreciated

Recommended Answers

All 8 Replies

You have a few problems with your code. First off, the Multiply and Divide blocks must be contained in main().
Secondly you cant redeclare x and y in Divide when theyy are already being used by Multiply.
Lastly, I think the colon should come after the word (ie Multiply or Divide)
I ran the code using these adjustments.

Member Avatar for iamthwee

If there was ever a time to use a goto statement. That code just there. In post # 1.

Yep, that would definitely be an essential use of the goto statement.

Wonderful.

It would make your code much more readable and clean to convert those multiply and divide lines into two shiny functions.

If there was ever a time to use a goto statement. That code just there. In post # 1.

Yep, that would definitely be an essential use of the goto statement.

Wonderful.

It would make your code much more readable and clean to convert those multiply and divide lines into two shiny functions.

I plan on using methods but for simplicity of my first ever program written in any language other than java I consider a "quick fix" rather than full declarations to be more efficient as they relate back to Batches which I was probably writting before you were in your daddy's balls!
you'll find I'm experienced in solaris, unix, linux, windows all the way back to 3.0, and I have even done assembly for the Amiga right up to 64 bit AMD architectures...sorry but flaming from a script kiddie like yourself is quite pittiful, show me something you have done...seeing as I helped secure government machines in networking to protect them from false injection at the age of 12.
but o na positive note...which is everything "iamthwee"(how immature) isnt, neithan brought up the point I plan to do next, thanks

So how did my suggestions work out for you?
If they worked for you as they did for me, your code should be running correctly. That said, please mark the thread as solved if you have no further questions.
Thanks.

Member Avatar for iamthwee

Look chicken, the similarities between your little batch programs and real, manly programming languages stop here.

Gotos are essentially an abhorrent unnecessary evil.

They lead to spaghetti code and there are few cases where it is actually needed.

Now if your familiarity with batch scripting is causing you to use gotos you should be glad I pulled you up on this now...

It appears that you don't know how to use a goto in C++, so that puts you about even up with functions from what you say: prefer learning functions first. Or just inline the code (copy and paste between the curly braces).

if( a = "true")

In the above = is assignment; use == for comparison; the string "true" is not the boolean true . (Of course, an actual boolean would be 0 or 1, not 1 or 2 as prompted. If you want to read an int input, or a string for input, you'll need to make a change or two.

I realized a few problems with the code I suggested, sorry.
As Dave pointed out you need (if a == "true"), however to fit your code I would just create an int value to hold the input of either 1 or 2.

int mult_or_div;
    cout << ("Multiply = 1 or Divide = 2 ?\n");
    cin >> mult_or_div;
    
    if( mult_or_div == 1)
    {
        goto Multiply;
    } 
    else 
    {
        goto Divide;
    }

One more problem. You need a second cin>> in your divide block. You never recieve the input for your second number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.