LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   goto usage (https://www.linuxquestions.org/questions/programming-9/goto-usage-155184/)

bru 03-08-2004 08:10 PM

goto usage
 
Quick program run down -> enter numbers, porgram does the math, porgram asks user if they would like to do it again.
However it doesn't work. I think I'm using goto wrong but have not found much of anything thats helpfull.

Code:


#include <iostream.h>

#include <math.h>

int main ()

{

    float A ;
    float B ;
    float C ;
    int equation ;
    float answer ;
    int Repeat;

//Asking for all required inputs

    cout <<"By inputing 1 you are using the -B '-'..., by inputing 2 you are using/n";
    cout <<"-B'+'... for the equation/n";

    loop:
    cout <<"Is the equation for x1 or x2?";
    cin >> equation;
    cout <<"Enter the value for A ";
    cin >> A;
    cout <<"Enter the Value for B ";
    cin >> B;
    cout <<"Enter the Value for C " ;
    cin >> C;

// doin the math, depending on user input

//I know the math is worng, but I will come to that in time
    if (equation == '1')

        {

                answer = (-B - sqrt((B*B) - 4*A*C)/(2*A)) ;

        }

    if (equation == '2')

        {

                answer = (-B + sqrt((B*B) - 4*A*C)/(2*A)) ;

        }

    cout <<"The root of you input of " << equation <<" is "<< answer << endl;
    cout <<"You input " << A << " for A\n";
    cout <<"You input " << B << " for B\n";
    cout <<"You input " << C << " for C\n";

    cout <<"Would you like to do another equation?";
    cin >> Repeat;

//Repeating upon the users request

// This is  my goto attempt

    if (Repeat == 'Y' || Repeat == 'y')

        {

                goto loop;

        }

    if (Repeat == 'N' || Repeat == 'n')

        {

                return(0);

        }

}

-Thanks in advance

jtshaw 03-08-2004 08:17 PM

Why don't you put the stuff that gets called over and over in a seperate function, have that function return a char (the response to "would you like to continue"). And then have your main loop do something like this:

repeat = loop();
while((repeat == 'y') || (repeat == 'Y")) {
repeat=loop();
}

goto's are consider by most to be terrible programming form in C/C++.

jtshaw 03-08-2004 08:26 PM

In case I didn't make myself clear... this works:

Code:

#include <iostream>

#include <math.h>
char loop();

int main()
{
    char Repeat;
    Repeat = loop();
    while((Repeat == 'y') || (Repeat == 'Y')) Repeat = loop();
    return 0;
}

char loop()
{
    float A;
    float B;
    float C;
    int equation;
    float answer;
    char Repeat;

  //Asking for all required inputs

    std::cout << "By inputing 1 you are using -B '-'...\n";
    std::cout << "By inputing 2 you are using -B '+'...\n for the equation\n";

    std::cout << "Is the equation for x1 or x2?";
    std::cin >> equation;
    std::cout << "Enter the value for A ";
    std::cin >> A;
    std::cout << "Enter the Value for B ";
    std::cin >> B;
    std::cout << "Enter the Value for C ";
    std::cin >> C;

    // doin the math, depending on user input

    //I know the math is worng, but I will come to that in time
    if (equation == '1')
    {
      answer = (-B - sqrt((B * B) - 4 * A * C) / (2 * A));
    }

    if (equation == '2')
    {
      answer = (-B + sqrt((B * B) - 4 * A * C) / (2 * A));
    }

    std::cout << "The root of you input of " << equation << " is " << answer << "\n";
    std::cout << "You input " << A << " for A\n";
    std::cout << "You input " << B << " for B\n";
    std::cout << "You input " << C << " for C\n";

    std::cout << "Would you like to do another equation?";
    std::cin >> Repeat;
    return Repeat;
}

Btw, you might need to change #include <iostream> to #include <iostream.h> and get ride of all the std:: I added. I added these so the code would work with GCC's c++ compiler.

bru 03-08-2004 08:30 PM

thanks jtshaw.,

P.S. I use the GCC compiler but never have any problems using #include<iostream.h> but thanks for the tip.

jtshaw 03-08-2004 08:38 PM

Ya, with newer versions of gcc and the -Wall option you might get a message like this:

warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.

That is why I knocked off the .h. The new way of doing C++ you don't put the extension.
math.h is a C header which is why you keep the .h there.

eskimo22 03-09-2004 03:44 AM

syntax of goto:

{
/* some code */
loop:
/* some more code */
goto loop:
}


All times are GMT -5. The time now is 03:11 PM.