loops

Hi, quick question for a brand new c++ user. How would i go about making a loop from 1 to a value which will be entered by the user (i will not know the value when programming.) Probably obvious but i've only just started this language. Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

void main()
{
int number;
cout << "Enter a number: " << endl;
cin >> number;
while(number > 0)
{
cout << number << " of Loops left" << endl;
number--;
}

This?
For example, i want a program to run and print the square roots of the numbers. I want the user to enter a number, and run the program from 1 to that number and printing out the numbers from 1 to the user input and beside them the square roots?
Yeah just put the code in the while loop
this is what i have but it's not working

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
#include <iostream>

using namespace std;

int main()
{

	int y;
	cout << "Enter any value :" ;
	cin >> y;
	
	while (y>0)
	{
	int square, powerr, powerrr;

	square = sqrt(y);
	powerr = y^2;
	powerrr = y^3;

	cout << "x" << "\t" << "sqrt" << "\t" << "x^2" << "\t" << "x^3" << "\t"   << "\n" 
		<< "===================" << "\n" 
		<< y << "\t" << square << "\t" << powerr << "\t" << powerrr;
	}

		int hold=0;
		return(0);
		cin>>hold;



}



Last edited on
You must do something to y so it won't be the same value forever. Otherwise, your while condition will always be true. Notice that in Code Apperentice's example he has number--; at the end of his loop. That way, eventually the while condition will be false and the loop will end.
Last edited on
Since you'll know how many times you'll want to repeat the loop based on the number the user enters, you could do this in a for loop, starting at 1 and running up to the user number.

I'd make the variable for square root a double, since you won't be dealing with integers in most cases for square roots. Also ^ is not used in C++ to raise a number to a power. There is a pow function in <cmath> but you could also just multiply the number by itself.

I pulled the header out of the loop so it only prints once.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{

	int usernumber, square, cube;
	double sqrt; //sqrt may not be integer value
	cout << "Enter any value :" ;
	cin >> usernumber;
	
	cout << "x" << "\t" << "sqrt" << "\t" << "x^2" << "\t" << "x^3" << << "\n" 
<< "===================" << "\n";
	for (int i=1;i<=usernumber;i++)
	{
	sqrt = sqrt(i);
	square = i*i; // not y^2;
	cube = i*i*i; //not y^3;
	cout << i << "\t" << sqrt << "\t" << square << "\t" << cube;
	}

	return 0;
}
thanks so much, the return 0 however isnt actually pausing the screen? would it be in with the for statement?
return 0 isn't intended to pause the screen.

Are you trying to keep the console from closing after the program runs? There's a thread stickied at the top of this section that talks about that - http://www.cplusplus.com/forum/beginner/1988/
thanks!
Topic archived. No new replies allowed.