What's wrong with my code?

closed account (DL30RXSz)
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?

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
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

int main()
{
	int base;
	int power;
	int result;

	cout << "What is the base? ";
	cin >> base;
	result = base;

	cout << endl << "What is the power? ";
	cin >> power;
	
	int counter = 1;
	//program doesn't get passed here
	if(counter==power)
	{
		result=base;
	}
	else if(power==0)
	{
		result=0;
	}
	else
	{
		while(counter<power);
		{
			result *= base;
			counter++;
		}
	}
		
	cout << endl << "The result is: ";
	cout << result << endl;

	bool again;
	cout << "Go again? 1=yes/0=no: ";
	cin >> again;
	if(again==1)
		main();
	else
		return 0;
}
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.
Last edited on
closed account (DL30RXSz)
Haha, thanks I had no idea I missed something stupid like that.
also:

You can't call main() like that.

Never ever do that.

Put main in a loop. But don't call it again.
how do you put main in a loop disch????
also:

You can't call main() like that.

Never ever do that.

Put main in a loop. But don't call it again.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  bool repeat = true;
  while(repeat)
  {
    // ...
    // stuff you want to repeat
    // ...

    if( dont_want_to_repeat_anymore )
      repeat = false;
  }
}
closed account (DL30RXSz)
I made a few revisions. How's this?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

int main()
{
	int base;
	int power;
	int Power(int, int);

	bool again=true;
	while(again)
	{
		cout << endl << "What is the base? ";
		cin >> base;

		cout << endl << "What is the power? ";
		cin >> power;

		cout << endl << "Result is: " << Power(base, power) << endl;

		cout << "Go again? 1=yes 0=no ";
		cin >> again;
	}

	return 0;
}

int Power(int base, int power)
{
	int result=base;
	int counter=1;

	if(base==1)
	{
		result=base;
	}
	else if(power==0)
	{
		result=1;
	}
	else
	{
		while(counter<power)
		{
			result *= base;
			counter++;
		}
	}

	return result;
}
i'd say use for loop
How's this?

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);

Last edited on
what about if 0 is raised to 0, it's not equal to 1..
00

or a number is raised to a negative?
N-M
I thought you were referring to user input you didn't specify.
LOL, i mean using for loop for calculating powers..
Anyway, I replied to this post, then read this post : http://cplusplus.com/forum/beginner/18518/

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."
Last edited on
what about if 0 is raised to 0, it's not equal to 1..
Yes, it is.

or a number is raised to a negative?
x^-y = (1/x)^y
Somebody clearly hasn't done differentiation/integration!
Topic archived. No new replies allowed.