C ++ Help needed please

Permabanned
Joined
28 Jun 2006
Posts
769
Location
Midsomer Norton
Cant get my head round this at all. I have been trying for hours. Basically here is my code so far. I am sure most of u will understan it

#include <iostream>
using namespace std;

int City=0; // This is the variable for the user to choose the city they want to run the program on


void New_York() // The City variable chosen to run the Simulation
{
int week=0; // Runs the program until it gets to week 10
int Mob=0; // Total amount of mobsters in alls gang
int Wanted=50; // This is the number of mobsters all wants in his gang
int Needed=0; // This is the variable for the number of mobsters needed to reach 50
int Recruited=0; // This is the number of mobsters recruited by big ALL
int Knicked=0; // This is the variiable for the he number of mobsters that have been arerested by the Fuzz
int Poridge=0; // This is the variiable for the he number of mobsters that are serving time
int Escaped=0; // This is the variiable for the he number of mobsters that have escaped from jail/goal

while (week<10||Mob>=50)
{
week=week+1;

Mob=(Mob+Recruited)- Knicked;//

cout << "gang size at the start of week: " << week <<" in New York is " << Mob << endl;

Recruited =(Wanted - Mob)*0.25;

Needed = Wanted - Mob;

cout << " All You have recruited : " << Recruited << "this week: "<endl;

Knicked = Mob *0.05;

cout << " Dam it the fuzz caught us : " << Knicked << endl;

Poridge = Poridge + Knicked;

cout << " Well we are screwed now : " << Poridge << " " << endl;

Escaped = Poridge *0.10 ;

cout << "Good Work All Busted us out " << Escaped << endl;

cout << " All u still need : " << Needed << endl;


}

cout << " I have had enough of this city i QUIT " << endl;

}
int main()

{


do
{
cout<<"----------------------------------------------------------------------------"<<endl;
cout<<". . . . . . . . . . . . . . . . . . . ."<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;
cout<<"where should we go today"<<endl;
cout<<"im a gangster going to "<<endl;//displays options to be input
cout<<"1 New York"<<endl;
cout<<"2 Chicago"<<endl;
cout<<"3 Los Angeles"<<endl;
cout<<"4 No where i wanna stay at home and do nothing"<<endl;
cout<<"select 1,2,3 or 4 now and nothin else or im guna blow off your head"<<endl;
cin>>City;//input the location of where going today
cout<<"----------------------------------------------------------------------------"<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;

if (cin.fail())//error checking for characters
{
cin.clear();
char City[100];
cin>>City;
}

if (City==1)//displays couts for new york if 1 has been inputed
{
cout<<"Woooo were going to The Big Apple"<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;
New_York();
}

if (City==2)//displays couts for chicago if 2 has been inputed
{
cout<<"Mmmmm i want pizza, im going to chicago"<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;
//Chicago();
}

if (City==3)//displays couts for L A if 3 has been inputed
{
cout<<"I want to see the hollywood sign"<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;
cout<<"----------------------------------------------------------------------------"<<endl;
//Los_Angeles();
}
if (City = > 3)//if function to end the program
{
cout<<"wow...............ok then...."<<endl;
cout<<"cya"<<endl;
return 0;//ends program
}
else if (City > = 5)//error checking if number inserted is greater than 5 or equal to 5
{
cout<<"What did i say?,was that a option, no get it right this time"<<endl;
}

}
while (City!=1||2||3 );
{
cout<<"what did i say,was that a option, no get it right this time"<<endl;
}

}

I keep this error on line 30

Error 2 error C2679: binary '<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) c:\users\scoobz\documents\visual studio 2005\projects\the mob final\the mob final\the mob.cpp 30
 
Typo:

cout << " All You have recruited : " << Recruited << "this week: "<endl;

should be:

cout << " All You have recruited : " << Recruited << "this week: "<<endl;
 
Please use code tags, you had various issues with parts of the code:

-while (City!=1||2||3 ); - should have no semi colon on the end
-else if (City > = 5) - no space between >=
-if (City = > 3) - => is invalid operator, should be >=
-cout << " All You have recruited : " << Recruited << "this week: "<endl; - missing < before endl, should be <<

Also in the top your function, your multiplying integers by floating point values which will give incorrect results, use floats instead.

The code below compiles, but still needs bits fixing, as i said use correct formatting and read through everything carefully and you will spot the syntax errors.


Code:
#include <iostream>
using namespace std;

int City=0; // This is the variable for the user to choose the city they want to run the program on


void New_York() // The City variable chosen to run the Simulation
{
	int week=0; // Runs the program until it gets to week 10
	int Mob=0; // Total amount of mobsters in alls gang
	int Wanted=50; // This is the number of mobsters all wants in his gang
 	int Needed=0; // This is the variable for the number of mobsters needed to reach 50
	int Recruited=0; // This is the number of mobsters recruited by big ALL
	int Knicked=0; // This is the variiable for the he number of mobsters that have been arerested by the Fuzz
	int Poridge=0; // This is the variiable for the he number of mobsters that are serving time
	int Escaped=0; // This is the variiable for the he number of mobsters that have escaped from jail/goal
	
	while (week<10||Mob>=50)
	{
		week=week+1;
		
		Mob=(Mob+Recruited)- Knicked;//
		
		cout << "gang size at the start of week: " << week <<" in New York is " << Mob << endl;
		
		Recruited =(Wanted - Mob)*0.25;
		
		Needed = Wanted - Mob;
		
		cout << " All You have recruited : " << Recruited << "this week: "<<endl;
		
		Knicked = Mob *0.05;
		
		cout << " Dam it the fuzz caught us : " << Knicked << endl;
		
		Poridge = Poridge + Knicked;
		
		cout << " Well we are screwed now : " << Poridge << " " << endl;
		
		Escaped = Poridge *0.10 ;
		
		cout << "Good Work All Busted us out " << Escaped << endl;
		
		cout << " All u still need : " << Needed << endl;
	}
	
	cout << " I have had enough of this city i QUIT " << endl;

}

int main()
{


	do
	{
		cout<<"----------------------------------------------------------------------------"<<endl;
		cout<<". . . . . . . . . . . . . . . . . . . ."<<endl;
		cout<<"----------------------------------------------------------------------------"<<endl;
		cout<<"where should we go today"<<endl;
		cout<<"im a gangster going to "<<endl;//displays options to be input
		cout<<"1 New York"<<endl;
		cout<<"2 Chicago"<<endl;
		cout<<"3 Los Angeles"<<endl;
		cout<<"4 No where i wanna stay at home and do nothing"<<endl;
		cout<<"select 1,2,3 or 4 now and nothin else or im guna blow off your head"<<endl;
		cin>>City;//input the location of where going today
		cout<<"----------------------------------------------------------------------------"<<endl;
		cout<<"----------------------------------------------------------------------------"<<endl;
	
		if (cin.fail())//error checking for characters
		{
			cin.clear();
			char City[100];
			cin>>City;
		}
		
		if (City==1)//displays couts for new york if 1 has been inputed
		{
			cout<<"Woooo were going to The Big Apple"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			New_York();
		}
		
		if (City==2)//displays couts for chicago if 2 has been inputed
		{
			cout<<"Mmmmm i want pizza, im going to chicago"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			//Chicago();
		}
		
		if (City==3)//displays couts for L A if 3 has been inputed
		{
			cout<<"I want to see the hollywood sign"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			//Los_Angeles();
		}

		if (City >= 3)//if function to end the program
		{
			cout<<"wow...............ok then...."<<endl;
			cout<<"cya"<<endl;
			return 0;//ends program
		}

		else if (City >= 5)//error checking if number inserted is greater than 5 or equal to 5
		{
			cout<<"What did i say?,was that a option, no get it right this time"<<endl;
		}
		
	}

	while (City != 1 || 2 || 3 )
	{
		cout<<"what did i say,was that a option, no get it right this time"<<endl;
	}
	
}
 
well spotted mate. thanks.

Thanks. It was very tricky. First I had to read the error message, which suggested that there was a problem on line 30. Then I had to work out what it meant when it said there was a problem with the single '<'. AFter that the solution kinda fell into place.

I Also get conversion problems from int to a double ias there any way around this. I think its because im jusing decimals and storing as whole numbers ?

Thats because on lines like:

Recruited =(Wanted - Mob)*0.25;

You're taking an int, multiplying it by a double, and then assigning it to an int, losing info.

For example if you did:

int i = 5 * 0.25 ;
int j = i * 4 ;

Then naively you might expect i to equal j, as you're dividing by 4 then multiplying by 4. In fact the result of 5 * 0.25 (=1.25) gets truncated to 1 when assigned to i, so j actually equals 4.

Might be an issue, might not, but its generally a good idea to use a static-cast in such situations to make your intentions absolutely clear (and to ensure your code compiles 'clean' with no warnings).
 
Please use code tags, you had various issues with parts of the code:

-while (City!=1||2||3 ); - should have no semi colon on the end
-else if (City > = 5) - no space between >=
-if (City = > 3) - => is invalid operator, should be >=
-cout << " All You have recruited : " << Recruited << "this week: "<endl; - missing < before endl, should be <<

Also in the top your function, your multiplying integers by floating point values which will give incorrect results, use floats instead.

The code below compiles, but still needs bits fixing, as i said use correct formatting and read through everything carefully and you will spot the syntax errors.


Code:
#include <iostream>
using namespace std;

int City=0; // This is the variable for the user to choose the city they want to run the program on


void New_York() // The City variable chosen to run the Simulation
{
	int week=0; // Runs the program until it gets to week 10
	int Mob=0; // Total amount of mobsters in alls gang
	int Wanted=50; // This is the number of mobsters all wants in his gang
 	int Needed=0; // This is the variable for the number of mobsters needed to reach 50
	int Recruited=0; // This is the number of mobsters recruited by big ALL
	int Knicked=0; // This is the variiable for the he number of mobsters that have been arerested by the Fuzz
	int Poridge=0; // This is the variiable for the he number of mobsters that are serving time
	int Escaped=0; // This is the variiable for the he number of mobsters that have escaped from jail/goal
	
	while (week<10||Mob>=50)
	{
		week=week+1;
		
		Mob=(Mob+Recruited)- Knicked;//
		
		cout << "gang size at the start of week: " << week <<" in New York is " << Mob << endl;
		
		Recruited =(Wanted - Mob)*0.25;
		
		Needed = Wanted - Mob;
		
		cout << " All You have recruited : " << Recruited << "this week: "<<endl;
		
		Knicked = Mob *0.05;
		
		cout << " Dam it the fuzz caught us : " << Knicked << endl;
		
		Poridge = Poridge + Knicked;
		
		cout << " Well we are screwed now : " << Poridge << " " << endl;
		
		Escaped = Poridge *0.10 ;
		
		cout << "Good Work All Busted us out " << Escaped << endl;
		
		cout << " All u still need : " << Needed << endl;
	}
	
	cout << " I have had enough of this city i QUIT " << endl;

}

int main()
{


	do
	{
		cout<<"----------------------------------------------------------------------------"<<endl;
		cout<<". . . . . . . . . . . . . . . . . . . ."<<endl;
		cout<<"----------------------------------------------------------------------------"<<endl;
		cout<<"where should we go today"<<endl;
		cout<<"im a gangster going to "<<endl;//displays options to be input
		cout<<"1 New York"<<endl;
		cout<<"2 Chicago"<<endl;
		cout<<"3 Los Angeles"<<endl;
		cout<<"4 No where i wanna stay at home and do nothing"<<endl;
		cout<<"select 1,2,3 or 4 now and nothin else or im guna blow off your head"<<endl;
		cin>>City;//input the location of where going today
		cout<<"----------------------------------------------------------------------------"<<endl;
		cout<<"----------------------------------------------------------------------------"<<endl;
	
		if (cin.fail())//error checking for characters
		{
			cin.clear();
			char City[100];
			cin>>City;
		}
		
		if (City==1)//displays couts for new york if 1 has been inputed
		{
			cout<<"Woooo were going to The Big Apple"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			New_York();
		}
		
		if (City==2)//displays couts for chicago if 2 has been inputed
		{
			cout<<"Mmmmm i want pizza, im going to chicago"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			//Chicago();
		}
		
		if (City==3)//displays couts for L A if 3 has been inputed
		{
			cout<<"I want to see the hollywood sign"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			cout<<"----------------------------------------------------------------------------"<<endl;
			//Los_Angeles();
		}

		if (City >= 3)//if function to end the program
		{
			cout<<"wow...............ok then...."<<endl;
			cout<<"cya"<<endl;
			return 0;//ends program
		}

		else if (City >= 5)//error checking if number inserted is greater than 5 or equal to 5
		{
			cout<<"What did i say?,was that a option, no get it right this time"<<endl;
		}
		
	}

	while (City != 1 || 2 || 3 ) ***
	{
		cout<<"what did i say,was that a option, no get it right this time"<<endl;
	}
	
}

That is very much appreciated. The last final error i get is here *** Error 4 error C2143: syntax error : missing ';' before '{' c:\users\scoobz\documents\visual studio 2005\projects\the mob final\the mob final\the mob.cpp 117
 
That is very much appreciated. The last final error i get is here *** Error 4 error C2143: syntax error : missing ';' before '{' c:\users\scoobz\documents\visual studio 2005\projects\the mob final\the mob final\the mob.cpp 117

You have a do…while loop with no while condition at the end.

Also, (City != 1 || 2 || 3 ) needs to be (City != 1 && City != 2 && City != 3 ); && and || separate boolean expressions, so each one will be evaluated for truth. Since any non-zero integer is treated as true, 2 and 3 will both always be true, meaning the loop will never terminate.
 
Back
Top Bottom