Sid
Sid

Reputation: 23

Can someone help me understand how labels and goto functions work?

I am totally new to C++ programming so this code may actually have many errors in it, I'm not sure. But the problem I'm having is that I have a label (START:) at the beginning of my code that I reference in a goto later on. The goto function itself seems to have no problems, but where I first use the START label I am getting an error that says "this declaration has no storage class or type specifier". Not sure if I just don't understand how labels/goto works, or if the functions being void is causing the issue, or what. Like I said, this is all completely new to me.

#include <iostream>
#include <string>
using namespace std;

double weight;
string planet;
double newWeight;

START:
void getUserInput() {
    cout << "Enter your weight and a planet: ";
    cin >> weight >> planet;
}

void convertInputToPlanetType() {
    if (planet == "Mercury") {
        newWeight = weight * 0.4155;
    }
    else if (planet == "Venus") {
        newWeight = weight * 0.8975;
    }
    else if (planet == "Earth") {
        newWeight = weight;
    }
    else if (planet == "Moon") {
        newWeight = weight * 0.166;
    }
    else if (planet == "Mars") {
        newWeight = weight * 0.3507;
    }
    else if (planet == "Jupiter") {
        newWeight = weight * 2.5374;
    }
    else if (planet == "Saturn") {
        newWeight = weight * 1.0677;
    }
    else if (planet == "Uranus") {
        newWeight = weight * 0.8947;
    }
    else if (planet == "Neptune") {
        newWeight = weight * 1.1794;
    }
    else if (planet == "Pluto") {
        newWeight = weight * 0.0899;
    }
    else {
        cout << "Error: Please enter a valid planet name, starting with a capital letter (ie. 'Earth')";
        goto START;
    }
}
void outputWeight() {
    cout << "On " << planet << " you would weigh " << newWeight << " pounds!" << endl;
}

Upvotes: 0

Views: 208

Answers (1)

Nate Eldredge
Nate Eldredge

Reputation: 58072

You can only goto a label within the same function. You cannot use it to jump between functions as you seem to be trying to do.

In fact, goto in general is a bad way to design programs. As a beginner you should probably just forget that it exists. There are certain special cases where some people feel it is better than the alternatives, but one needs considerable expertise to make that judgment well, and anyway yours is definitely not one of those cases.

Think about how to redesign your program to get the desired effect using while or do/while instead. It might be helpful to have convertInputToPlanetType return a value indicating whether the conversion was successful, so that getUserInput can tell whether to ask the user again.

As a side note, also think about how you could redesign your program to not use global variables, but pass data via function arguments instead. Global variables are another language feature that, as a beginner, you probably ought to ignore.

Upvotes: 4

Related Questions