i am very new to c++ and have grasped the concepts pretty well but im still struggling with a simple while loop. if someone could quickly proofread my work im sure it wont take longer than a moment. thanks.
// while loop inside of while loop
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main ()
{
unsignedlongint f;
cout << "Give me a number up to 4294967295 \n";
cin >> f;
while (f!= -1) {
while (f>0) {
cout << f << ","; // shows number and adds a comma
--f; // subtracts one
}
cout << "again?\n";
cin >> f;
}
}
return 0;
}
thanks so much. i couldnt get much farther in c++ without learning this
@Duncan - there doesn't seem to be a point, it's just a while-loop-exercise that seems to be supposed to count backwards from the entered number to 1. The first while-statement seems to be the way to exit the program; if user enters "-1" instead of a new number, the program exits.
Because f is an unsigned integer, it can't hold a negative value. However your first loop won't end until f hold the value of -1. This screws things up. Change the condition of your first while loop.
ok, so because its unsigned ive changed (f!=-1) to (f!=1) because that will still act the same way but im still getting errors.
what im really trying to do is have a loop that will count down but once it has reached zero the program will start over again without the need to restart it.
as dennisDOTcpp had said this is just a simple exercise and serves no purpose other than helping me to understand and get comfortable with loops.
sorry. my specific goal is to have a program that will count down an input variable by one until it reaches 0. once it reaches 0 i want it to quit out of the while loop. but instead of having the program finished i would like it to still continue so that i can put in a new variable and have it count down that variable and then repeat. essentially i want to have the whole program to be a huge loop but the program in the huge loop is also nothing but a simple loop. is that helpful at all? sorry for the confusion
#include <iostream>
usingnamespace std;
int main ()
{
unsignedlongint f;
cout << "Give me a number up to 4294967295 \n";
cin >> f;
while (f>0) {
cout << f << ",";
cout << "again?\n";
cin >> f;
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
unsigned long int f;
cout << "Give me a number up to 4294967295 \n";
while(f==0){
cin >> f;
while (f>0){
f--;
cout <<f << "\n";}
cout << "again? \n";}
return 0;
}
I got it! novablows' skipped the loop because then my entry had to be zero but by making it while (f>=0) for the first loop it works great! thanks guys. i wouldnt be able to much farther in programming without loops haha. heres my final code:
// Testing.cpp : while loop in a while loop
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main ()
{
unsignedlongint f;
cout << "\ngive me a number up to 4294967295 \n";
cin >> f;
while (f >= 0){
while (f>0) {
cout << f << ",";
--f;
}
cout << "again?";
cin >> f;
}
int wait;
cin >> wait;
return 0;
}
Awesome that you got it! I have a few other pieces of advice, however. On line 14, because the max int size can vary from computer to computer, I would recommend using the bitwise complement to figure out the maximum int size. Make sure it is unsigned. Changing the line to this:
cout << "\ngive me a number up to " << ~0U << endl;
will automatically adjust the number so that it shows the maximum int size on the specific computer. You could also use INT_MAX from climits to achieve the same effect: http://www.cplusplus.com/reference/clibrary/climits/
Also, you can combine lines 19 and lines 20 to make your code more concise like so:
cout << f-- << ",";
Lastly, instead of declaring the wait variable and using cin to read into it, you could just use getchar(), which will wait for the user to enter a character before exiting. Just my advice :)
ill get right on those things. any advice on not only getting my program to work but also on how to make it more technically correct is greatly appreciated. thanks!