Do While Loop Early Ending

Alright so this is my first do while loop, and I'm having some interesting flaws. First of all, let me say that everything runs without error messages, it just doesn't perform they way it should. I'll post the code then give a description.


int main()
{
cout << "Welcome to MathWiz!" << endl;
again=="yes";
do{
cout << "Would you like:" << endl;
cout << "Reference angle finder" << endl << "Radian and degree converter" << endl << "Pythagorean theorum solver" << endl;
cout << "Sine formula solver" << endl << "Cosine formula solver" << endl << "Quadratic Solver" << endl;
string option;
getline (cin,option);
if (option=="reference angle finder"||option=="Reference angle finder"||option=="Reference Angle Finder")
{Refangle();}
else if (option=="radian and degree converter"||option=="Radian and degree converter"||option=="Radian and Degree Converter")
{Converter();}
else if (option=="pythagorean theorum solver"||option=="Pythagorean theorum solver"||option=="Pythagorean Theorum Solver")
{Pythag();}
else if (option=="sine formula solver"||option=="Sine formula solver"||option=="Sine Formula Solver")
{Sinform();}
else if (option=="cosine formula solver"||option=="Cosine formula solver"||option=="Cosine Formula Solver")
{Cosform();}
else if (option=="quadratic solver"||option=="Quadratic solver"||option=="Quadratic Solver")
{Quadsolvediscriminant();}
else
{cout << "Oops! That's not a valid input!";}
cout << endl << "Would you like to use the program again?" << endl;
getline (cin,again);
system("cls");
}
while(again=="yes"||again=="Yes");
cout << "Goodbye!";
Sleep(1500);
return 0;
}


So basically, the main function is supposed to ask which tool the user wants to use. They enter a string (via getline), and this sends it off to a void function.

***Side Note: all of my void functions work, I've tested them being called from main before the do while loop was added).***

Anyway, it completes the function and goes back to main while still in the do loop, and then the it asks the user if they would like to repeat. If the user inputs yes via getline, then it compares it in the while loop and should repeat the do loop. The problem is that every time it asks if you'd like to repeat, it skips to the goodbye output which comes after the while loop. Here's the catch: when it sends to else on the original if statement, it works perfectly, asks to repeat, waits for a response and if yes is put in it will restart the do loop. I was thinking this was because it was the only one to go to void or something, but I'm not sure.

And then I tried changing a void function around. I changed a void function by adding the separated part (note that the name of the void function will not be the same as the if statement in main as it reroutes through a different function.

void Sinside()
{
cout << "What units are you using for length?" << endl;
getline (cin,units);
cout << "Please enter the known side (excluding units)." << endl;
cin >> answerside1;
cout << "Please enter the corresponding angle to this side" << endl;
cin >> answerangle1;
cout << "Please enter the final angle" << endl;
cin >> answerangle2;
cout << "Are these angles in radians or degrees?" << endl;
getline(cin,sineangle);

FROM HERE**
if(sineangle=="radians"||sineangle=="Radians")
(sinfinalangle1 = answerangle1) && (sinfinalangle2 = answerangle2);
else if (sineangle=="degrees"||sineangle=="Degrees")
(sinfinalangle1 = (answerangle1*.01745)) && (sinfinalangle2 = (answerangle2*.01745));
answerside2=sin(sinfinalangle2)*answerside1/sin(sinfinalangle1);
TO HERE**

cout << "Your missing side is " << answerside2 << units;
}

After I did this, it changed to printing "are these angles in radians or degrees" and consequently ignoring the getline right after that. BUT after printing everything it should've and totally ignoring the getline, it rightfully went to the end of the do loop, asked if you wanted to repeat the program and correctly started it over if input was yes.

Whats up??? Why is it ignoring a getline if it goes to a function? Please help!! Sorry for the long post, but I felt it was necessary to fully explain.

Oh, and here are my declarations:
#include <iostream>
#include <string>
#include <conio.h>
#include <cmath>
#include <Windows.h>
using std::cout;
using std::cin;
using std::string;
using std::getline;
using std::endl;
float userangle;
float finalangledegrees;
float finalangleradians;
float finalangleradianspi;
float reducedangle;
float legone;
float legtwo;
float hypotenuse;
float answerangle1;
float answerangle2;
float answerside1;
float answerside2;
float firstterm;
float secondterm;
float thirdterm;
float discriminant;
float quadanswerpositive;
float quadanswernegative;
float sinfinalangle1;
float sinfinalangle2;
int pi=227;
string units;
string again;
string sineangle;
bool repeat;
Last edited on
closed account (zb0S216C)
helios82 wrote:
 
again=="yes";
(sic)

This doesn't do what you think it does. What it actually does is compare the current string contained within again to the string literal "yes". The result of the evaluation returns a Boolean value. Since the resulting Boolean isn't assigned or compared, this statement is effectively useless. I'm guessing this is a typing mistake on your part.

As for your problem, try synchronizing the stream[1].

I can't stress this enough: Always initialize your variables! :) The importance of this is quite high.

References:
[1]http://www.cplusplus.com/reference/iostream/istream/sync/


Wazzak
Last edited on
Thanks so much for the quick reply!!

Yes that was not supposed to be there, thanks for the catch.

I read the article on sync but I have to say I'm very confused on how to use it. Also, for the initialization of variable, what do I do need to do for initialization if it's going to be dictated by cin? Still set it equal to a value that will just be overwritten? Why is this important?
Also, for the initialization of variable, what do I do need to do for initialization if it's going to be dictated by cin? Still set it equal to a value that will just be overwritten? Why is this important?

Yes, do something like this:

1
2
string option("");
getline(cin, option);

or this:

1
2
3
float answerside1 = 0;
cin>>answerside1;
cin.sync();

In the above two examples, the initialization is not important at all. However, it's a good habit to get into. Some seemingly random and confusing bugs occur thanks to uninitialized variables. For instance, the "it works in Debug mode but not Release!", and the "it works with optimizations off but not when they're on!", and the "it works for x86/x64 but not for x64/x86!" bugs may sometimes be traced back to an uninitialized variable.
Last edited on
EDIT: Never mind! I did some more reading on it and everything now runs awesome! Thanks guys! Thanks so much for the help and I will continue to initialize all variables in the future!
Last edited on
Topic archived. No new replies allowed.