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!
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?
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.
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.
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/