Issue with function prototypes

Page 1 of 1

6 Replies - 885 Views - Last Post: 22 July 2009 - 10:03 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=116274&amp;s=b76cb03545ccdd9fbee95a01bf5940a2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 gantis

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

Issue with function prototypes

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

Reputation: 2965
• Posts: 11,222
• 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!

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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?

#4 debjit625

• D.I.C Regular

Reputation: 51
• Posts: 447
• 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

#5 gantis

Reputation: 0
• 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 ********";
}
```

#6 Dogstopper

Reputation: 2965
• Posts: 11,222
• 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

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

Re: Issue with function prototypes

Posted 22 July 2009 - 10:03 PM

The prototypes must match the definitions EXACTLY.