LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-08-2004, 09:10 PM   #1
bru
Member
 
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132

Rep: Reputation: 15
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
 
Old 03-08-2004, 09:17 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
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++.
 
Old 03-08-2004, 09:26 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
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.
 
Old 03-08-2004, 09:30 PM   #4
bru
Member
 
Registered: Sep 2003
Location: South Carolina
Distribution: Ubuntu, CentOS, BT4, Debian
Posts: 132

Original Poster
Rep: Reputation: 15
thanks jtshaw.,

P.S. I use the GCC compiler but never have any problems using #include<iostream.h> but thanks for the tip.
 
Old 03-08-2004, 09:38 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
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.
 
Old 03-09-2004, 04:44 AM   #6
eskimo22
LQ Newbie
 
Registered: Feb 2004
Posts: 11

Rep: Reputation: 0
syntax of goto:

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH goto dkrysak Programming 35 03-21-2013 11:22 AM
can you use goto in bash script sabliny Programming 3 10-07-2005 06:54 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 05:45 AM
Goto command? batfoot Linux - General 4 08-26-2003 08:17 PM
GOTO function in bash? sobchak Programming 1 07-22-2002 06:10 AM


All times are GMT -5. The time now is 11:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration