C++ code correction

Pl. find the errors

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 3746 Views - Last Post: 10 December 2010 - 02:06 PM Rate Topic: -----

#31 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: C++ code correction

Posted 10 December 2010 - 10:38 AM

View PostJackOfAllTrades, on 10 December 2010 - 09:23 AM, said:

void findHighest(int north, int south, int east, int west, int central,int numAccidents);
{


Lose that semi-colon.


Already did that..tell me something else.
Was This Post Helpful? -1
  • +
  • -

#32 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: C++ code correction

Posted 10 December 2010 - 11:18 AM

Have you found your missing brace?

The error that JackofAllTrades informed you about was in the last code you posted.

If you have fixed these two errors:

Please post the modified code:

Does the program compile without error/warning messages?

If not please post the complete error/warning messages.

If it does compile:

Is the program producing any output?

Are the outputs what you expect?

If not what are the outputs?

What are the outputs?

What did you input into the program?

Jim
Was This Post Helpful? 0
  • +
  • -

#33 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: C++ code correction

Posted 10 December 2010 - 11:29 AM

here is the modified code: i think i found the missing braces, but it is showin this error - Run-Time Check Failure #3 - The variable 'numAccidents' is being used without being initialized.


#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

void findLowest(int north, int south, int east, int west, int central,int numAccidents);
void findHighest(int north, int south, int east, int west, int central,int numAccidents);
int getNumAccidents(string regionName);

int main()
{
	
		string regName;
	int north, 
		south, 
		east, 
		west, 
		central,
		numAccidents;
	
 cout << "There will be 5 regions within the major city.\n";     
	              
		 { 
			 regName="north:";
			 north = getNumAccidents(regName);
	         
			 
			 regName="south:";
	         south = getNumAccidents(regName);
	         
			
			 regName="east:";
	         east = getNumAccidents(regName); 
	         
			
			 regName="west:";
	         west = getNumAccidents(regName); 
	         
			 
			 regName="central:";
	         central = getNumAccidents(regName);
		 }
			  findLowest(north, south, east, west, central, numAccidents);
			  
			  findHighest(north, south, east, west, central, numAccidents);
			  {int numAccidents = 0;}

			 return 0;
			
}
	 
int getNumAccidents(string regionName)
	 
{  
	   int numAccidents;

         do

         {

                cout << "Enter the number of accidents for " << regionName;                 
				cin >> numAccidents;

         } 
		  while (numAccidents < 0); 

          return numAccidents;  
}
	 
void findLowest(int north, int south, int east, int west, int central,int numAccidents)
{
	
	 string regName;

	 {   
		if (numAccidents > north) 
		{
		regName = "north";
		numAccidents = north;
		}
		if (numAccidents > south) 
		{
		regName = "south";
		numAccidents = south;
		}
		if (numAccidents > east) 
		{
		regName = "east";
		numAccidents = east;
		}
		if (numAccidents > west) 
		{
		regName = "west";
		numAccidents = west;
		}
		if (numAccidents > central) 
		{
		regName = "central";
		numAccidents = central;
		}
	 }

		cout << "The Lowest Region is " << regName << endl;

	
}
void findHighest(int north, int south, int east, int west, int central,int numAccidents)
{
	
	
	string regName;
	{

		if (numAccidents < north) 
		{
		regName = "north";
		numAccidents = north;
		}
		if (numAccidents < south) 
		{
		regName = "south";
		numAccidents = south;
		}
		if (numAccidents < east) 
		{
		regName = "east";
		numAccidents = east;
		}
		if (numAccidents < west) 
		{
		regName = "west";
		numAccidents = west;
		}
		if (numAccidents < central) 
		{
		regName = "central";
		numAccidents = central;
		}
	}
	cout << "The Highest Region is " << regName << endl;
}


Was This Post Helpful? 0
  • +
  • -

#34 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: C++ code correction

Posted 10 December 2010 - 11:59 AM

Quote

here is the modified code: i think i found the missing braces


Good it seems you are making progress.

Now for the next problem:


Quote

Run-Time Check Failure #3 - The variable 'numAccidents' is being used without being initialized


This error is complaining about numAccidents not being initialized.

Let's look back at Post #22 posted by simeesta

Quote

Re: C++ code correction

Posted Today, 03:07 AM
In your code you declare some variables. Then you call the function findHigest and findLowest. So what are the values of north south east and west???

Then you read the values of n,s,e & w from the user. Wouldn't you want to do this the BEFORE you find the highest and lowest?

Also there is no need for the numAccidents argument in your highest/lowest functions. Your findHigest (and lowest) algorithms are also sightly off. Go over it and see if you find the mistakes.

I'd declare numAccidents inside the functions - not as an argument.


Look at the last 2 paragraphs (especially the last).

Is this not saying you might not need the variable numAccidents for these functions? Are you using this variable in main anywhere else? If not then you can delete numAccidents everywhere in main, and from the function prototype, and function definition.

I have not really studied your findHighest, findLowest functions yet so I don't know what kind of problems these functions might have.
Was This Post Helpful? 0
  • +
  • -

#35 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: C++ code correction

Posted 10 December 2010 - 01:21 PM

View Postjimblumberg, on 10 December 2010 - 10:59 AM, said:

Quote

here is the modified code: i think i found the missing braces


Good it seems you are making progress.

Now for the next problem:


Quote

Run-Time Check Failure #3 - The variable 'numAccidents' is being used without being initialized


This error is complaining about numAccidents not being initialized.

Let's look back at Post #22 posted by simeesta

Quote

Re: C++ code correction

Posted Today, 03:07 AM
In your code you declare some variables. Then you call the function findHigest and findLowest. So what are the values of north south east and west???

Then you read the values of n,s,e & w from the user. Wouldn't you want to do this the BEFORE you find the highest and lowest?

Also there is no need for the numAccidents argument in your highest/lowest functions. Your findHigest (and lowest) algorithms are also sightly off. Go over it and see if you find the mistakes.

I'd declare numAccidents inside the functions - not as an argument.


Look at the last 2 paragraphs (especially the last).

Is this not saying you might not need the variable numAccidents for these functions? Are you using this variable in main anywhere else? If not then you can delete numAccidents everywhere in main, and from the function prototype, and function definition.

I have not really studied your findHighest, findLowest functions yet so I don't know what kind of problems these functions might have.


Here is the final code dude..it works perfect but it doesn't display findLowest. It does display findHighest. Please just tell me now..it is due at 5.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;


int getNumAccidents(string regionName);

int main()
{
	
		string regName;
	int north, 
		south, 
		east, 
		west, 
		central;
		
	
 cout << "There will be 5 regions within the major city.\n";     
	              
		 { 
			 regName="north:";
			 north = getNumAccidents(regName);
	         
			 
			 regName="south:";
	         south = getNumAccidents(regName);
	         
			
			 regName="east:";
	         east = getNumAccidents(regName); 
	         
			
			 regName="west:";
	         west = getNumAccidents(regName); 
	         
			 
			 regName="central:";
	         central = getNumAccidents(regName);
		 }
			  int numAccidents = 0;
			  void findLowest(int north, int south, int east, int west, int central,int numAccidents);
			  void findHighest(int north, int south, int east, int west, int central,int numAccidents);	
			  findLowest(north, south, east, west, central, numAccidents);
			  
			  findHighest(north, south, east, west, central, numAccidents);
			  

			 return 0;
			
}
	 
int getNumAccidents(string regionName)
	 
{  
	   int numAccidents;

         do

         {

                cout << "Enter the number of accidents for " << regionName;                 
				cin >> numAccidents;

         } 
		  while (numAccidents < 0); 

          return numAccidents;  
}
	 
void findLowest(int north, int south, int east, int west, int central,int numAccidents)
{
	
	 string regName;

	 {   
		if (numAccidents > north) 
		{
		regName = "north";
		numAccidents = north;
		}
		if (numAccidents > south) 
		{
		regName = "south";
		numAccidents = south;
		}
		if (numAccidents > east) 
		{
		regName = "east";
		numAccidents = east;
		}
		if (numAccidents > west) 
		{
		regName = "west";
		numAccidents = west;
		}
		if (numAccidents > central) 
		{
		regName = "central";
		numAccidents = central;
		}
	 }

		cout << "The Lowest Region is " << regName << endl;

	
}
void findHighest(int north, int south, int east, int west, int central,int numAccidents)
{
	
	
	string regName;
	{

		if (numAccidents < north) 
		{
		regName = "north";
		numAccidents = north;
		}
		if (numAccidents < south) 
		{
		regName = "south";
		numAccidents = south;
		}
		if (numAccidents < east) 
		{
		regName = "east";
		numAccidents = east;
		}
		if (numAccidents < west) 
		{
		regName = "west";
		numAccidents = west;
		}
		if (numAccidents < central) 
		{
		regName = "central";
		numAccidents = central;
		}
	}
	cout << "The Highest Region is " << regName << endl;
}

Was This Post Helpful? 0
  • +
  • -

#36 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: C++ code correction

Posted 10 December 2010 - 02:06 PM

These line belong above main()

			  void findLowest(int north, int south, int east, int west, int central,int numAccidents);
			  void findHighest(int north, int south, int east, int west, int central,int numAccidents);



In main() to make this program work as is you need to add a line and change the default value of numAccidents.


			  int numAccidents = 0;  // Change this 0 to 100000.

			  findLowest(north, south, east, west, central, numAccidents);
                          numAccidents = 0;   // Add this line.
			  findHighest(north, south, east, west, central, numAccidents);



Jim
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3