ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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);
}
}
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:
#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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.