C++ code correction

Pl. find the errors

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

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

#1 qwerty159  Icon User is offline

  • New D.I.C Head

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

C++ code correction

Posted 09 December 2010 - 09:30 AM

Hii everyone i think there are few errors in this code and there is also some modification required pl. help me with it..i did what i cud. Thanks

Here is the question : Write a program that determines which of five geographic regions within a major city (north, south, east, west, and central) had the fewest reported automobile accidents last year. It should have the following two functions, which are called by main.

int getNumAccidents () is passed the name of a region. It asks the user for the number of automobile accidents reported in that region during the last year, validates the input, then returns it. It should be called once for each city region.

void findLowest () is passed the five accident totals. It determines which is the smallest and prints the name of the region, along with its accident figure.

#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 main()
{
	int getNumAccidents(string regionName);
		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);


			

			 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;

	 
	
	 for (int i = 1; i < numAccidents; i++)

	 {
	       
		if (numAccidents > north  )
		{
			regName = north;
		}

		if (numAccidents > south  )
		{
			regName = south;
		}
		
		if (numAccidents > east  )
		{
			regName = east;
		}

		if (numAccidents > west  )
		{
			regName = west;
		}

		if (numAccidents > central  )
		{
			regName = central;
		}
	}	

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

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

	for (int i = 1; i < numAccidents; i++)
	 {
		if (numAccidents < north  )
		{
			regName = north;
		}

		if (numAccidents < south  )
		{
			regName = south;
		}
		
		if (numAccidents < east  )
		{
			regName = east;
		}

		if (numAccidents < west  )
		{
			regName = west;
		}

		if (numAccidents < central  )
		{
			regName = central;
		}
	}

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

	
}	 





Is This A Good Question/Topic? 0
  • +

Replies To: C++ code correction

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4006
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: C++ code correction

Posted 09 December 2010 - 09:36 AM

Quote

Hii everyone i think there are few errors in this code


Could you pleas enlighten us on what you think is wrong?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: C++ code correction

Posted 09 December 2010 - 09:38 AM

move line 11 to before main. You cant have a function prototype in a function.

You will also have to call findHighest in main, and findHighest doesn't work correctly. I should be > not <.
Was This Post Helpful? 0
  • +
  • -

#4 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 09 December 2010 - 09:50 AM

View Postjimblumberg, on 09 December 2010 - 08:36 AM, said:

Quote

Hii everyone i think there are few errors in this code


Could you pleas enlighten us on what you think is wrong?

Jim


After I type the number of accidents for each region the program closes down; there is nothing else display after it.

View Postsimeesta, on 09 December 2010 - 08:38 AM, said:

move line 11 to before main. You cant have a function prototype in a function.

You will also have to call findHighest in main, and findHighest doesn't work correctly. I should be > not <.


same problem..nothing is changed and what do u mean by > and < can u b more specific thnx

This post has been edited by qwerty159: 09 December 2010 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4006
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: C++ code correction

Posted 09 December 2010 - 10:19 AM

Please post the modified code and any error/warning messages you get when compile the code. Please post the entire error messages if you get any.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 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 09 December 2010 - 10:33 AM

View Postjimblumberg, on 09 December 2010 - 09:19 AM, said:

Please post the modified code and any error/warning messages you get when compile the code. Please post the entire error messages if you get any.


Jim


i haven't done any modification. This is the best I could do. These are the errors produced at the bottom of the compiler :

'to try.exe': Loaded 'C:\Documents and Settings\ACCUser\Desktop\CS qwerty\All programs (DONE)\to try\Debug\to try.exe', Symbols loaded.
'to try.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'to try.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'to try.exe': Loaded 'C:\WINDOWS\system32\sysfer.dll', Binary was not built with debug information.
'to try.exe': Loaded 'C:\WINDOWS\system32\netapi32.dll'
'to try.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll'
'to try.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll'
'to try.exe': Loaded 'C:\WINDOWS\system32\secur32.dll'
'to try.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll'
'to try.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcp90d.dll'
'to try.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll'
The program '[5864] to try.exe: Native' has exited with code 0 (0x0).
Was This Post Helpful? 0
  • +
  • -

#7 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: C++ code correction

Posted 09 December 2010 - 10:36 AM

As you never call findHighest and findLowest it's difficult to see whether they're correct or not. I don't think they are, so repost your code where you call these functions in main and I'll have another look at them.
Was This Post Helpful? 0
  • +
  • -

#8 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 09 December 2010 - 10:42 AM

View Postsimeesta, on 09 December 2010 - 09:36 AM, said:

As you never call findHighest and findLowest it's difficult to see whether they're correct or not. I don't think they are, so repost your code where you call these functions in main and I'll have another look at them.

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

int 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);
	int getNumAccidents(string regionName);
		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);


			

			 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;

	 
	
	 for (int i = 1; i < numAccidents; i++)

	 {
	       
		if (numAccidents > north  )
		{
			regName = north;
		}

		if (numAccidents > south  )
		{
			regName = south;
		}
		
		if (numAccidents > east  )
		{
			regName = east;
		}

		if (numAccidents > west  )
		{
			regName = west;
		}

		if (numAccidents > central  )
		{
			regName = central;
		}
	}	

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

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

	for (int i = 1; i < numAccidents; i++)
	 {
		if (numAccidents < north  )
		{
			regName = north;
		}

		if (numAccidents < south  )
		{
			regName = south;
		}
		
		if (numAccidents < east  )
		{
			regName = east;
		}

		if (numAccidents < west  )
		{
			regName = west;
		}

		if (numAccidents < central  )
		{
			regName = central;
		}
	}

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

	
}	 





Was This Post Helpful? 0
  • +
  • -

#9 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: C++ code correction

Posted 09 December 2010 - 11:00 AM

you don't call functions like that.
#include <iostream>
void myfunction();//Prototypes these ARE NOT inside main
void myfunction2();

int main()
{
   myfunction(); //calls the function notice there's no void.
   myfunction2();
   return 0;
}

void myfunction()//function defintion
{
   std::cout << "hello from myfunction\n";
}
void myfunction2()
{
   std::cout << "hello from myfunction2!!!!";
}


Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: C++ code correction

Posted 09 December 2010 - 11:00 AM

Again...you're NOT CALLING THESE FUNCTIONS!!!

You've prototyped them within main, which while technically not illegal is not what you want to do; they should be prototyped BEFORE main. However, you are not making calls to these functions.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4006
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: C++ code correction

Posted 09 December 2010 - 11:01 AM

In the following code you are defining 3 functions inside of a function (main). You can't do that. Move these 3 lines to above the int main().

int 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);
	int getNumAccidents(string regionName);



Jim
Was This Post Helpful? 0
  • +
  • -

#12 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 09 December 2010 - 11:11 AM

View Postjimblumberg, on 09 December 2010 - 10:01 AM, said:

In the following code you are defining 3 functions inside of a function (main). You can't do that. Move these 3 lines to above the int main().

int 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);
	int getNumAccidents(string regionName);



Jim


why do u keep telling me the same thing. It still displays the same and terminates at the same point as before.
Was This Post Helpful? 0
  • +
  • -

#13 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: C++ code correction

Posted 09 December 2010 - 11:18 AM

because its WRONG!!!!!!!!
Was This Post Helpful? 2
  • +
  • -

#14 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 09 December 2010 - 11:21 AM

View Postsimeesta, on 09 December 2010 - 10:18 AM, said:

because its WRONG!!!!!!!!


then tell me how to do it..i dun understand it. Don't u c i dun understand anymore i wrote that when i first posted the question. Even though u told me to put the lines over int main(), it still doesn't work..what does that mean. Give me the correct modification.
Was This Post Helpful? -2
  • +
  • -

#15 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: C++ code correction

Posted 09 December 2010 - 11:41 AM

look at my example. READ the comments. Does my code look like yours??? Do you call the functions the same way? Do I have function prototypes in my main function?????

We tell you how to fix it and you ignore our advice. If you don't understand what we ask you to change then ask question on it - BUT don't just repost the code making the same mistakes.
Was This Post Helpful? 0
  • +
  • -

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