let me start off by saying that i know almost nothing about c++. im having trouble getting this code to run it just repeats the last cout statement over and over till i close the window. any help is appreciated.
#include <iostream>
usingnamespace std;
int main ()
{
double powerLevel = 0;
int force;
int choice;
cout <<"What movie did the term 'The Force' come from?"<<endl;
cout <<"1. The Goonies"<<endl;
cout <<"2. Star Wars"<<endl;
cout <<"3. SuperTroopers"<<endl;
cout <<"4. BatMan"<<endl;
cin >> choice;
while (choice == 2 )
cout <<"Congratulations your Power Level is 1"<<endl;
powerLevel == powerLevel + 1;
while ( choice != 2 )
cout <<"Wrong, the phrase 'The Force' Came from Star Wars. "
<<"Your power level is 0."<<endl;
powerLevel == 0;
system("cls");
switch (force)
{
case'0':
cout << "You have no power. You will die shortly.";
break;
case'1':
cout << "Your power is low, you are in danger.";
break;
case'2':
cout << "Your power is so, so. The force is leaving you.";
break;
case'3':
cout << "Your power is pretty good, but it could be better.";
break;
case'4':
cout << "Great Power. The force is with you.";
break;
}
}
void getpowerLevel (int force)
{
int power = 0;
char size;
int number;
if (force < 1)
power = -1;
else
{
cout << "Is a megabyte bigger than a gigabyte?";
cin >> size;
if (size == 'y' || size == 'Y' )
power = power + 1;
cout << "Who is worst C++ teacher at TVCC? Hainze or Loper?";
string worst;
cin >>worst;
if (worst == "Hainze")
power = power - 1;
cout << "Who is best C++ teacher at TVCC? Hainze or Loper?";
string best;
cin >>best;
if (best == "Hainze")
power = power + 1;
cout << "How many bytes are in kilobyte";
cin >>number;
if (number == 1024)
power = power + 1;
} return;
}
If you are setting something equal to something else you need to use the = operator. "==" checks for equality, while "=" assigns values. You use the incorrect operator twice on lines 17 and 21.
Also, the first while loop will repeat infinitely if the user chooses 2, and the second if the user does not, as I think you have noticed. This is because you are using a while loop. In your case, I wouldn't even use while loops; just you and if-else statement:
1 2 3 4 5 6 7 8
if (choice == 2 ) {
cout <<"Congratulations your Power Level is 1"<<endl;
powerLevel += powerLevel;
} else {
cout <<"Wrong, the phrase 'The Force' Came from Star Wars. "
<<"Your power level is 0."<<endl;
powerLevel == 0;
}
Also, your getpowerlevel function changes power but since it is a function scope variable it is deleted when the function returns; I don't think this is what you want.
Hi ...
If I am not wrong...
1. U need to call the function "getpowerLevel", which I can't see from where is being called.
2. U have used the variable "force" in the switch statement, which is a integer variable, where as in case u have provided char '0'-'4'.
3. It is always a good practice to provide a "default" case when u are using a switch statement.
The main problem is being pointed out by zhuge.
On these points u focus, u can get ur problem fixed.