Total newbie in class and having an issue with several different HW problems. All seem to have similar issues with "left over" input junk.
When I run them the program runs correctly the 1st try but as soon as I try to loop back for added runs after the 1st "cin" it automatically sails through the 2nd "cin" essentially putting in a "+1" from what ever the (old input) was?
So reading here there is a lot about using
1 2 3
std::cout << " That is not a valid input. ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
BUT using MS Vis Studio Community 2015 gives me an error at the "MAX()" looking for some sort of input. So I cant even debug it and when I try to run without debugging it fails for same thing.
most of the code is at the end for full section but having similar issues in 3 or 4 different programs and NOT figuring this out on my own.
I have also tried the simpler versions of
1 2 3 4
cin.ignore();
cin.clear(); //clear out the User Inputs
Which I have tried at both the END of the program prior to the loop and at the top right after the re-starting loop Beginning point.
int main()
{
// Start of prog..
cout << "This program calculates if a coordinate point is withing a circle, prepare to enter the coordinate points. ";
cout << '\n'; // spacer for on screen.
BEGINNING: // Start place for program loop.
cout << " Enter the the pair of X & Y seperated by a space then hit enter key. "; // Prompts & gets user data
cout << '\n'; // spacer for on screen.
cin >> x1, __y1;
This code above works the 1st time but recycles the 2nd try and starts right the 3rd time.
// while ((std::cout << " Enter the the pair of X & Y separated by a space then hit enter // key. ") && !(std::cin >> x1, __y1));
// {
// std::cout << " That is not a valid input. ";
// std::cin.clear();
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// }
EDIT IN the 9 lines of code above does not work
cout << '\n'; // spacer for on screen.
// Start formulas below
double calc1 = (pow((_x2 - x1), 2) + (pow((_y2 - __y1), 2)));
double side1 = (pow(calc1, 0.5));
cout << " The distance of 1st side is " << setprecision(16) << side1 << endl;
//tells user what the length is for 1st side.
if ( side1 < side2 )
{
cout << '\n'; // spacer for on screen.
cout << " The point is INSIDE the Circle. " << endl;
cout << '\n'; // spacer for on screen.
}
if ( side2 == side1 )
{
cout << '\n'; // spacer for on screen.
cout << " The point is ON the Circle. " << endl;
cout << '\n'; // spacer for on screen.
}
if ( side1 > side2 )
{
cout << '\n'; // spacer for on screen.
cout << " The point is OUTSIDE OF the Circle. " << endl;
cout << '\n'; // spacer for on screen.
}
cout << '\n'; // spacer for on screen.
cin.ignore();
cin.clear(); //clear out the User Inputs
cout << " Did this work? Check the math and press any key to exit! " << std::endl;
system("PAUSE"); // wait for user input to confirm this ran correctly vs what was wanted for display.
goto BEGINNING; // loops back to start over
return 0;
}
while ((std::cout << " Enter the the pair of X & Y separated by a space then hit enter key. ") && !(std::cin >> x1, __y1));
WTF. Where did you get this?
With added bonus: std::cin >> x1, __y1
No, seriously, WTF? This really doesn't do what yopu think it does, and even so it's got no business pairing up as part of a loop condition. Who taught you this? They really shouldn't have.
This is just plain wrong. You've also got a semi-colon on the end of it, so the while loop has zero contents.
And there's a goto at the end of this. You're able to use a while loop, but then you also choose to use a goto? And what's going on with these variable names Underscores and double underscores? WTF? You're outright not permitted to start your variable names with two underscores. And in many cases (including this one) with a single underscore.
This is just plain bad (and wrong) code.
Start again. Fetch the input variables one at a time, and NOT in the loop condition. That alone will massively improve the code. Just write that bit first. Fetching two numbers from the user.
Please don't edit your original post. It renders following posts in the thread nonsensical and makes it impossible for anyone else reading the thread to have any idea what's going on.
PNOID: thanks that seems to be it for the simple CIN not looping right.
Moschops: will do! thanks.
Any ideas on how to use the
1 2 3 4 5
std::cout << " That is not a valid input. ";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
in MS Visual Studio as my basic understanding seems this would be a better way of getting accurate inputs of the correct type? Right Clicking on the red underlined code (the MAX()) section, error gives other code/ but I still dont understand it.