I made a program to calculate powers. The program just stops running after counter is initialized. I have to force quit it through the exit button. Why is doing this? Also, any style suggestions?
First of all, x0=1 for all values of x, not 0.
Second, there's a semicolon immediately after the while condition on line 29, making the while an empty loop.
Much better, There's no need for a "for" loop this is fine.
Nice indentation, good structure.
1 crititsism, is validation. But this is something you can learn to add to future projects. The yes is pretty redundant in the output as it loops anyway... Also user could have typed a letter or some gibberish.
What I mean about the output is you could say:
1 2 3 4 5 6 7 8 9 10
bool again=true;
int i;
while (again) // or do while
{
cout << "\nWould you like to continue? press 1 for Yes: ";
cin >> i;
again = false;
if ( i == 1)
again = true;
}
this way if the user wants to continue it does, otherwise it exits.
your code runs an infinite loop on bad input.
using a do while you can set to false to start with:
1 2 3 4 5 6 7 8 9 10
int i;
do
{
bool again=false; // necessary in loop for reset.
cout << "\nWould you like to continue? press 1 for Yes: ";
cin >> i;
if ( i == 1)
again = true;
} while (again);
you should follow Duoas examples in that for better validation. However it can be complicated for begineers.
edit: personally, I don't think it matters too much. I typically use responses as posted above, as they are much easier to write and still unbreakable.
"Would you like to continue Y/N? " process 'y' and ignore everything else is my motto "TOO BAD! if user makes a typo eh."