In: Computer Science
This program does compile. But it does not give the expected output. It is supposed to raise the value of the input to its power all the way from 0 to 10. The only output seen is the number raised to the power 0 over and over again. what did I do wrong? help please.
// This program raises the user's number to the powers
// of 0 through 10.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int value;
char choice;
cout << "Enter a number: ";
cin >> value;
cout << "This program will raise " << value;
cout << " to the powers of 0 through 10.\n";
for (int count = 0; count <= 10; count)
{
cout << value << " raised to the power of ";
cout << count << " is " << pow(value,
count);
cout << "\nEnter Q to quit or any other key ";
cout << "to continue. ";
cin >> choice;
if (choice == 'Q' || choice == 'q')
break;
}
return 0;
}
Answer:
You forgot to increment "count" value in the for loop.
Source code:
// This program raises the user's number to the
powers
// of 0 through 10.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int value;
char choice;
cout << "Enter a number: ";
cin >> value;
cout << "This program will raise " << value;
cout << " to the powers of 0 through 10.\n";
for (int count = 0; count <= 10; count++) //here forgot to
increment the count
{
cout << value << " raised to the power of ";
cout << count << " is " << pow(value,
count);
cout << "\nEnter Q to quit or any other key ";
cout << "to continue. ";
cin >> choice;
if (choice == 'Q' || choice == 'q')
break;
}
return 0;
}