Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org goto usage
 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

 03-08-2004, 08:10 PM #1 bru Member   Registered: Sep 2003 Location: South Carolina Distribution: Ubuntu, CentOS, BT4, Debian Posts: 132 Rep: 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 #include 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
 03-08-2004, 08: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: 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++.
 03-08-2004, 08: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: In case I didn't make myself clear... this works: Code: #include #include 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 to #include and get ride of all the std:: I added. I added these so the code would work with GCC's c++ compiler.
 03-08-2004, 08:30 PM #4 bru Member   Registered: Sep 2003 Location: South Carolina Distribution: Ubuntu, CentOS, BT4, Debian Posts: 132 Original Poster Rep: thanks jtshaw., P.S. I use the GCC compiler but never have any problems using #include but thanks for the tip.
 03-08-2004, 08: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: 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 header for the header for C++ includes, or instead of the deprecated header . 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.
 03-09-2004, 03:44 AM #6 eskimo22 LQ Newbie   Registered: Feb 2004 Posts: 11 Rep: syntax of goto: { /* some code */ loop: /* some more code */ goto loop: }