1 Replies - 1489 Views - Last Post: 30 April 2008 - 10:28 AM Rate Topic: -----

#1 the SS show  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-April 08

repeating cout statement

Post icon  Posted 30 April 2008 - 10:10 AM

i wil just start off by saying i dont know hardly anything about c++ but im having a problem getting my program to run. the program simply repeats the last cout statement over and over untill i close the program. any help in getting this to stop so that the rest of the program can run is appreciated
#include <iostream>
using namespace 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;
	}


Is This A Good Question/Topic? 0
  • +

Replies To: repeating cout statement

#2 jeronimo0d0a  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 03-March 08

Re: repeating cout statement

Posted 30 April 2008 - 10:28 AM

a while (condition ) must change condition or execute forever
C++ lets you do without {} but it's a bad idea. These will exit because x will eventually pass 10; Since this got you, beware of floating point variables with ==, they sometimes don't EXACTLY equal. see included patch in the code. also note this works as an if statement

while ( x < 10 )
x++ ;
// same as
while ( x < 10 )
{
x++ ;
}
#include <iostream>
using namespace 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;

// patch in code
 if ( choice == 2 )
// while (choice == 2 ) // why is this while anyway
{ // without the brace here only the next line will execute
   // so the cout runs and choice can never change
   cout <<"Congratulations your Power Level is 1"<<endl;
   powerLevel == powerLevel + 1;
   choice = 0; // exit the while loop, which is now an if 
} // and here ***********
else if( choice != 2 )
//	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;
	}

[/quote]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1