6 Replies - 562 Views - Last Post: 22 July 2009 - 10:03 PM Rate Topic: -----

#1 gantis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-March 09

Issue with function prototypes

Post icon  Posted 22 July 2009 - 03:18 PM

Why am I having trouble using the function prototypes before the function main? I know the function definitions need to be below the function main.
#include<iostream>
using namespace std;

// Function to find the Average of prices

void findAvg( float (&price)[50])						 
{
	 float sum = 0;
	 float average;
	 for(int i=0; i<50; i++)
	 {
			 sum+=price[i];
	 }						   
	 
	 average = sum/50;
	 
	 cout << "\n\n" << "Average Is:" << average;
	 cout <<"\n" << "*************************************";
	 
}	 

// Function to find the Highest price and the respective cities

void findHighest(char (&city_name)[50][100], float (&price)[50])
{
	 int max = 0;
	 for(int i=0; i<50; i++)
	 {
		if(price[max] < price[i])
		{
					max = i;
		}																															
	 }						   
	 cout << "\n\n" << "Max Is:  " << price[max] <<"\n\n";
	 
	 cout << "City/Cities with highest price is/are:" <<"\n\n";
		  
	 for(int j=0; j<50; j++)
	 {
			 if(price[j] == price[max])
			 {
						 cout << city_name[j] << "\n";
			 }			
	 }
	 
	 cout <<"\n" << "*************************************";
}	 

// Function to find the Lowest price and the respective cities

void findLowest(char (&city_name)[50][100], float (&price)[50])
{
	 int min = 0;
	 for(int i=0; i<50; i++)
	 {
		if(price[min] > price[i])
		{
					min = i;
		}																															
	 }						   
	 cout << "\n\n" << "Min Is:  " << price[min] <<"\n\n";
	 cout << "City/Cities with highest price is/are:" <<"\n\n";
		  
	 for(int j=0; j<50; j++)
	 {
			 if(price[j] == price[min])
			 {
						 cout << city_name[j] << "\n";
			 }			
	 }
	 
	 cout <<"\n" << "*************** THE END ********";
}	 


// Main Function

int main()
{

  char city_name[50][100];   // variable for City Name, visible to all functions
  float price[50];		   // variable for Price, visible to all functions


 cout <<"\n" <<"************  MICHAEL ANTIS   ************" <<"\t" << "*** Gas Price Assignment ***" <<"\n\n";


  FILE *fd = NULL;

  fd = fopen("gasprice.txt", "r");
  if(fd == NULL)
  {	  
	cout << "Unable to open gasprice  file";
	return 0;
  } 


  for(int i=0; i<50; i++)
  {
	 (fscanf(fd,"%s %f\n", city_name[i], &price[i]) != EOF);
  } 
  
 /* cout << "City name" <<"\t" << "Gas price" <<"\n\n";
  for(int j=0; j<50; j++)
  {
		  cout << city_name[j] <<"\t" << price[j] <<"\n";
  }
  */
  
  
  findAvg(price);
  
  findHighest(city_name, price);
  
  findLowest(city_name, price);
  
  cout << endl;
  
system("pause");
return 0;
  
  return 1;
}
 



Is This A Good Question/Topic? 0
  • +

Replies To: Issue with function prototypes

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Issue with function prototypes

Posted 22 July 2009 - 03:49 PM

I don't see anything wrong with it. I compiled it and it ran fine, although i think that you should include gasprices.txt and the errors that you encountered. Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Issue with function prototypes

Posted 22 July 2009 - 04:06 PM

That code has the functions before the main. If you're having trouble with the opposite situation, wouldn't it make more sense to show us THAT code?
Was This Post Helpful? 0
  • +
  • -

#4 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Issue with function prototypes

Posted 22 July 2009 - 04:38 PM

I didnt understand totaly so my way of doing
Here goes the example

#include <iostream>
using namespace std;

void Function(); //This is the prototype

void main()
{
Function();
}

void Function() //This is the body of function
{

}

So if this is not compiling than reinstall simple way

good luck
Was This Post Helpful? 0
  • +
  • -

#5 gantis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-March 09

Re: Issue with function prototypes

Posted 22 July 2009 - 04:46 PM

Use attachment

Okay here is what I changed, program executes but I am not getting the correct output this way.

 #include<iostream>
using namespace std;

// Function to find the Average of prices


void findAvg();
  
 void  findHighest();
  
void   findLowest();

// Main Function

int main()
{

  char city_name[50][100];   // variable for City Name, visible to all functions
  float price[50];		   // variable for Price, visible to all functions


 cout <<"\n" <<"************  MICHAEL ANTIS   ************" <<"\t" << "*** Gas Price Assignment ***" <<"\n\n";


  FILE *fd = NULL;

  fd = fopen("gasprice.txt", "r");
  if(fd == NULL)
  {	  
	cout << "Unable to open gasprice  file";
	return 0;
  } 


  for(int i=0; i<50; i++)
  {
	 (fscanf(fd,"%s %f\n", city_name[i], &price[i]) != EOF);
  } 
  
 /* cout << "City name" <<"\t" << "Gas price" <<"\n\n";
  for(int j=0; j<50; j++)
  {
		  cout << city_name[j] <<"\t" << price[j] <<"\n";
  }
  */
  
  
  
  
  cout << endl;
  
system("pause");
return 0;
  
  return 1;
}
 

void findAvg( float (&price)[50])						 
{
	 float sum = 0;
	 float average;
	 for(int i=0; i<50; i++)
	 {
			 sum+=price[i];
	 }						   
	 
	 average = sum/50;
	 
	 cout << "\n\n" << "Average Is:" << average;
	 cout <<"\n" << "*************************************";
	 
}	 

// Function to find the Highest price and the respective cities

void findHighest(char (&city_name)[50][100], float (&price)[50])
{
	 int max = 0;
	 for(int i=0; i<50; i++)
	 {
		if(price[max] < price[i])
		{
					max = i;
		}																															
	 }						   
	 cout << "\n\n" << "Max Is:  " << price[max] <<"\n\n";
	 
	 cout << "City/Cities with highest price is/are:" <<"\n\n";
		  
	 for(int j=0; j<50; j++)
	 {
			 if(price[j] == price[max])
			 {
						 cout << city_name[j] << "\n";
			 }			
	 }
	 
	 cout <<"\n" << "*************************************";
}	 

// Function to find the Lowest price and the respective cities

void findLowest(char (&city_name)[50][100], float (&price)[50])
{
	 int min = 0;
	 for(int i=0; i<50; i++)
	 {
		if(price[min] > price[i])
		{
					min = i;
		}																															
	 }						   
	 cout << "\n\n" << "Min Is:  " << price[min] <<"\n\n";
	 cout << "City/Cities with highest price is/are:" <<"\n\n";
		  
	 for(int j=0; j<50; j++)
	 {
			 if(price[j] == price[min])
			 {
						 cout << city_name[j] << "\n";
			 }			
	 }
	 
	 cout <<"\n" << "*************** THE END ********";
}	 

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Issue with function prototypes

Posted 22 July 2009 - 04:57 PM

Your problem is that you have to call those functions from main. For example from main() type after the for loops:
findAvg(&price);



Then the average function will work as soon as you change this:
void findAvg( float* price)



That will correct the function. Try the rest of them and ask for help if needed.

This post has been edited by Dogstopper: 22 July 2009 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Issue with function prototypes

Posted 22 July 2009 - 10:03 PM

The prototypes must match the definitions EXACTLY.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1