What's the problem here? (Im still noob, yes) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What's the problem here? (Im still noob, yes)

I wanted to use some code by guys that helped me on another code so I can see if I learned it but I guess I missed something, instead of again asking for input it just outputs it over again. #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { bool quit = false; while(!quit) { float meters; cout << endl << "Enter a value in meters: " << endl << endl; while (meters < 1) { cin >> meters; if (cin.fail() || meters < 1) { cin.clear(); cin.ignore(512, '\n'); meters = 0; cout << endl << "Invalid input, please try again." << endl << endl; } } float feet = meters * 3.2808399; cout << endl << meters << " meters " << "is " << feet << " feet." << endl; int choice; cout << endl << "Do you want to try again? Type 1 to continue or 0 to quit." << endl << endl; cin >> choice; if(choice == 0) { quit = true; } } }

12th Aug 2017, 9:51 AM
Djordje Zivkovic
Djordje Zivkovic - avatar
12 Answers
+ 7
Not really sure what you are asking but from what I can see you did not initialize meters before your while(meters < 1). Its going to have a garbage value before entering the loop Try meters = 0 at the start.
12th Aug 2017, 10:01 AM
Karl T.
Karl T. - avatar
+ 7
Not stupid its a common beginner mistake to forget initializing variables.
12th Aug 2017, 10:28 AM
Karl T.
Karl T. - avatar
+ 6
Also this wont work on SL code playground because you have to enter all inputs and then it stops executing.
12th Aug 2017, 10:08 AM
Karl T.
Karl T. - avatar
+ 3
You need to initialise meters to zero before the while loop that checks if meters is less than one, otherwise when it loops around it will always check the same value
12th Aug 2017, 10:19 AM
Shane
+ 3
Dude I am platinum and I am equally "stupid" everyone, in that sense, is "stupid", we just have ro find a way to get less "stupid".
12th Aug 2017, 10:33 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 1
Am I tried to set up a loop so when it calculates the meter-->feet, it asks if i would like to calculate again, but instead outputting the "enter value in meters" and wait for input, it outuputs the old input again and just loop like that.. If i explained well idk
12th Aug 2017, 10:09 AM
Djordje Zivkovic
Djordje Zivkovic - avatar
+ 1
Yeah Im compiling it in Code::Blocks and still doesnt work..
12th Aug 2017, 10:11 AM
Djordje Zivkovic
Djordje Zivkovic - avatar
+ 1
I mean it kinda works but not as intended to.. It should ask if i want to calculate something again and then wait for my input to calculate, but it already calculates the old input..
12th Aug 2017, 10:12 AM
Djordje Zivkovic
Djordje Zivkovic - avatar
+ 1
Yep I am stupid thanks to both of you I am just plain stupid or blind.
12th Aug 2017, 10:26 AM
Djordje Zivkovic
Djordje Zivkovic - avatar
+ 1
I dunno am I stupid but its still the same when I do it. I dont know what answer to check, you both said the same but I didn't understand at first lol.
12th Aug 2017, 10:27 AM
Djordje Zivkovic
Djordje Zivkovic - avatar
+ 1
Well I started the course 2 days ago but still I feel stupid lol.. Thanks for help I hope I'll do it right next time.
12th Aug 2017, 10:30 AM
Djordje Zivkovic
Djordje Zivkovic - avatar
0
Well yeah I saw that on stackoverflow lol. Now Im gonna try to make to choose if you want meter to feet or feet to meter( yeah probably gonna post more questions haha ) Should I use if for that or booleans?
12th Aug 2017, 10:35 AM
Djordje Zivkovic
Djordje Zivkovic - avatar