please help

Pages: 12
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// while loop inside of while loop
//

#include "stdafx.h"

#include <iostream>

using namespace std;
int main ()
{
	

	unsigned long int 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
Last edited on
You can knock off the firstwhile(f!=-1), as it is better portreyed in the second loop.

I also don't see the point of decrementing f.
closed account (iwTf92yv)
@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.
Then make that an if statement. You are using it as one anyways.

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.
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.


-1 is not the same thing as 1. Where did you get your math education from?
i mean that it will do the same job. im not going to be counting down from 1 so instead i was hoping to use it now as my exit.
Okay, take a step back and explain to us, as clearly as you can, what you are trying to do, and what errors you are getting.
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
Yes. How is it that you want the program to end then? The way you explained it it sounds like you would just have to kill the process.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;
int main ()
{
	

	unsigned long int f;
		
	cout << "Give me a number up to 4294967295 \n";
	cin >> f;			
	
	while (f>0) {
		cout << f << ",";	
		cout << "again?\n";
		cin >> f;
	}
	return 0;
}


this?

#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;
}

chipp - i was hoping to have the program count down such as put in 5 and the program will say
5,4,3,2,1, again?
and thenhave it so i can put in a new number and have it count down that number.

novablows- ive tried something similar to that and it only show the number once and then says
again?

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Testing.cpp : while loop in a while loop
//

#include "stdafx.h"

#include <iostream>

using namespace std;
int main ()
{
	

	unsigned long int 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!
alright so when i tried ~OU it didnt work. do i have to #define that? it did work for INT_MAX though.
use numeric_limits<unsigned int>::max() instead ( limits header )
1
2
unsigned int f;
while( f>= 0 ) // tautology 
Thats odd, the following program runs perfectly for me:
1
2
3
4
5
6
7
#include <iostream>

int main(void) {
    std::cout << ~0U << std::endl;
    return 0;
}

Pages: 12