Tomas_S Registered User
#1

Hi, could somebody give me an idea?

I have a problem in this little program I'm trying to put together for college.

The question I have is in case 2 i'm trying call a function and compiler's error is too few arguments. What should I do?


#include <stdio.h>
#include <stdlib.h>

#define ERROR "Please input data first\n\n"


int menu(void);
/* Purpose: display the menu with options and return valid option*/

int input_number_of_readings(void);
/* Purpose: Input number of readings*/

int input_reading_number(void);
/* Purpose: Input number of reading*/

int input_times(int number_of_readings, float times[], float readings[]);
/* Purpose: Input times for each reading*/

void input_readings(int number_of_readings, float reading, float readings[]);

void display_results(int number_of_readings, float times[], float readings[]);
/* Purpose: display the results of each reading*/

main()
{
float *times=NULL;
float *readings=NULL;

int number_of_readings=0, reading_number;
int number_of_bytes;
int choice;

float fastest_morning, slowest_morning,
fastest_afternoon, slowest_afternoon,
fastest_evening, slowest_evening,reading;


do
{
choice=menu();
switch (choice)
case 0:
printf("End of program\n\n");
break;
case 1:
number_of_readings=get_number_of_readings();
reading_number=input_reading_number();

if (times!=NULL)
free(times);
if (readings!=NULL)
free(readings);

number_of_bytes=number_of_readings*sizeof(float);
readings=(float*) malloc(number_of_bytes);
number_of_bytes=number_of_readings*sizeof(float);
times=(float*) malloc(number_of_bytes);

if (readings==NULL || times==NULL)
{
printf("Memory allocation error\n");
exit (1);
}
case 2:
input_readings(int number_of_readings, float reading, float readings[]);
input_times(int number_of_readings, float times[], float readings[]);
}






int menu()
{
int menu_choice;
int max_choice=4;

printf("\n\n********************Menu********************\n");
printf("1. Input the number of readings *\n");
printf("2. Input readings and times *\n");
printf("3. Display the wind speeds *\n");
printf("4. Display the fastest and the slowest *\n");
printf("5. Exit\n\n *\n\n");
printf("6. Choose option 0-%d:",max_choice);

do
{
scanf("%d",&menu_choice);
}
while (menu_choice<0 || menu_choice>max_choice);

return (menu_choice);
}

/*_____________________________________________*/

int input_number_of_readings()
{
int number_of_readings;

do
{
printf("Enter number of readings (3 minimum):");
scanf("%d",&number_of_readings);
}
while (number_of_readings<3);

return number_of_readings;
}

/*_____________________________________________*/

int input_reading_number()
{
int reading_number;

printf("Enter reading number:");
do
scanf("%d",&reading_number);
while (reading_number<1);

return (reading_number);
}

/*____________________________________________*/

int input_times(int number_of_readings, float times[], float readings[])
{
int i;

printf(" Enter recorded times in the format 24.00\n\n");

for (i=0;i<number_of_readings;i++)
printf("Time for reading number%f:",readings);
scanf("%.2f",&times);
}

/*_____________________________________________*/

void input_readings(int number_of_readings, float reading, float readings[])
{
int i;

printf("Enter readings:");

for(i=0;i<number_of_readings;i++)
printf("Reading for number%f:",readings);
scanf("%f",&readings);
}

/*____________________________________________*/

void display_results(int number_of_readings, float times[], float readings[])
{

int i;

for(i=0;i<number_of_readings;i++)
{
printf("Reading number: %f",readings);
if(readings==0)
printf("No wind");
else
printf("%f",times);
}
}

Aswerty Registered User
#2

You have a number of errors throughout your code. No while clause at the end of the do while loop surrounding the switch statement. Switch statement not encased in brackets. No break in the case 1 and case 2. Re declaration of variables in the parameters of the functions in case 2 (e.g. variables from line 29 and 31 declared again, the readings variable type was also changed from a pointer of type int to an array of type float). Also in case 2 parameters being passed aren't being used in the functions being called.

Tomas_S Registered User
#3

I know I'm just half way through this little program and I'm not surprised about all the errors. Will try and fix them thanks for your input!

#4

Tomas_S said:

case 2:
input_readings(int number_of_readings, float reading, float readings[]);
input_times(int number_of_readings, float times[], float readings[]);
}


You dont need int or float when calling the two functions.

Tomas_S Registered User
#5

one more question,

at the very end compiler says:expected declaration or statement at the end of input. What do I have to do about this?


#include <stdio.h>
#include <stdlib.h>

#define ERROR "Please input data first\n\n"


int menu(void);
/* Purpose: display the menu with options and return valid option*/

int input_number_of_readings(void);
/* Purpose: Input number of readings*/

int input_reading_number(void);
/* Purpose: Input number of reading*/

int input_times(int number_of_readings, float times[], float readings[]);
/* Purpose: Input times for each reading*/

void input_readings(int number_of_readings, float reading, float readings[]);

void display_results(int number_of_readings, float times[], float readings[]);
/* Purpose: display the results of each reading*/

main()
{
float *times=NULL;
float *readings=NULL;

int number_of_readings=0, reading_number;
int number_of_bytes;
int choice;

float fastest_morning, slowest_morning,
fastest_afternoon, slowest_afternoon,
fastest_evening, slowest_evening,reading;


do
{
choice=menu();
switch (choice) {
case 0:
printf("End of program\n\n");
break;
case 1:
number_of_readings=get_number_of_readings();
reading_number=input_reading_number();

if (times!=NULL)
free(times);
if (readings!=NULL)
free(readings);

number_of_bytes=number_of_readings*sizeof(float);
readings=(float*) malloc(number_of_bytes);
number_of_bytes=number_of_readings*sizeof(float);
times=(float*) malloc(number_of_bytes);

if (readings==NULL || times==NULL)
{
printf("Memory allocation error\n");
exit (1);
}
break;
case 2:
input_readings(number_of_readings, reading, readings);
input_times( number_of_readings, times, readings);
break;

case 3:

display_results( number_of_readings, float times, readings);
break;

}






int menu()
{
int menu_choice;
int max_choice=4;

printf("\n\n********************Menu********************\n");
printf("1. Input the number of readings *\n");
printf("2. Input readings and times *\n");
printf("3. Display the wind speeds *\n");
printf("4. Display the fastest and the slowest *\n");
printf("5. Exit\n\n *\n\n");
printf("6. Choose option 0-%d:",max_choice);

do
{
scanf("%d",&menu_choice);
}
while (menu_choice<0 || menu_choice>max_choice);

return (menu_choice);
}

/*_____________________________________________*/

int input_number_of_readings()
{
int number_of_readings;

do
{
printf("Enter number of readings (3 minimum):");
scanf("%d",&number_of_readings);
}
while (number_of_readings<3);

return number_of_readings;
}

/*_____________________________________________*/

int input_reading_number()
{
int reading_number;

printf("Enter reading number:");
do
scanf("%d",&reading_number);
while (reading_number<1);

return (reading_number);
}

/*____________________________________________*/

int input_times(int number_of_readings, float times[], float readings[])
{
int i;

printf(" Enter recorded times in the format 24.00\n\n");

for (i=0;i<number_of_readings;i++)
printf("Time for reading number%f:",readings);
scanf("%.2f",&times);
}

/*_____________________________________________*/

void input_readings(int number_of_readings, float reading, float readings[])
{
int i;

printf("Enter readings:");

for(i=0;i<number_of_readings;i++)
printf("Reading for number%f:",readings);
scanf("%f",&readings);
}

/*____________________________________________*/

void display_results(int number_of_readings, float times[], float readings[])
{

int i;

for(i=0;i<number_of_readings;i++)
{
printf("Reading number: %f",readings);
if(readings==0)
printf("No wind");
else
printf("%f",times);
}
}

ressem Registered User
#6

You still are missing code.

You have a 'do' that does not end in a 'while' statement.
Where is the get_number_of_readings() function?
And another small issue which your compiler will display when you fix the other 2.

Tomas_S Registered User
#7

ressem said:
You still are missing code.

You have a 'do' that does not end in a 'while' statement.
Where is the get_number_of_readings() function?
And another small issue which your compiler will display when you fix the other 2.


I got rid of do statement for now and I've mixed up words supposed to be:
input_number_of_readings() function.

Code is not finished but I just want to get it working for the moment.
Still getting the same after fixing them 2 little things

Aswerty Registered User
#8

I'd hazard a guess that a missing bracket, }, at the end of the switch statement in your main function is causing that compilation error.

Tomas_S Registered User
#9

Aswerty said:
I'd hazard a guess that a missing bracket, }, at the end of the switch statement in your main function is causing that compilation error.

That was it! At least I can get it on the screen now

#10

Where is your

break; ? after every case:

Sorry just had a quick look over and found them

i dont like using case why dont you use

in your switch()

else

if

they are easy to debug.

Tomas_S Registered User
#11

Could somebody have a quick look again for the same thing?

expected declaration or statement at the end of input

at the end of program. I'm probably blind now.


#include <stdio.h>
#include <stdlib.h>


int menu(void);
/* Purpose: display the menu with options and return valid option*/

int input_number_of_readings(void);
/* Purpose: Input number of readings*/

void assign_reading_number(void);
/* Purpose: Assign number of reading*/

int input_times(int number_of_readings, float times[], float readings[]);
/* Purpose: Input times for each reading*/

void input_readings(int number_of_readings, float reading, float readings[]);

void display_results(int number_of_readings, float times[], float readings[]);
/* Purpose: display the results of each reading*/

main()
{
float *times=NULL;
float *readings=NULL;

int number_of_readings=0, reading_number;
int number_of_bytes;
int choice;

float fastest_morning, slowest_morning,
fastest_afternoon, slowest_afternoon,
fastest_evening, slowest_evening;


do
{
choice=menu();
switch (choice)
{
case 0:
printf("End of program\n\n");
break;
case 1:
number_of_readings=input_number_of_readings();

if (times!=NULL)
free(times);
if (readings!=NULL)
free(readings);

number_of_bytes=number_of_readings*sizeof(float);
readings=(float*) malloc(number_of_bytes);
number_of_bytes=number_of_readings*sizeof(float);
times=(float*) malloc(number_of_bytes);

if (readings==NULL || times==NULL)
{
printf("Memory allocation error\n");
exit (1);
}
reading_number=assign_reading_number;
break;
case 2:
input_readings(number_of_readings,reading_number, readings);
input_times( number_of_readings, times, readings);
break;

case 3:

display_results( number_of_readings, times, readings);
break;

}

}
while (choice!=0);


int menu()
{
int menu_choice;
int max_choice=4;

printf("\n\n********************Menu********************\n");
printf("1. Input the number of readings *\n");
printf("2. Input readings and times *\n");
printf("3. Display the wind speeds *\n");
printf("4. Display the fastest and the slowest *\n");
printf("5. Exit\n\n *\n\n");
printf("6. Choose option 0-%d:",max_choice);

do
{
scanf("%d",&menu_choice);
}
while (menu_choice<0 || menu_choice>max_choice);

return (menu_choice);
}

/*_____________________________________________*/

int input_number_of_readings()
{
int number_of_readings;

do
{
printf("Enter number of readings (3 minimum):");
scanf("%d",&number_of_readings);
}
while (number_of_readings<3);

return number_of_readings;
}

/*_____________________________________________*/

void assign_reading_number(int number_of_readings,int reading_number,float readings[])
{
int i;
for (i=0; i<number_of_readings;i++)
readings=reading_number+1;
}

/*____________________________________________*/

int input_times(int number_of_readings, float times[], float readings[])
{
int i;

printf(" Enter recorded times in the format 24.00\n\n");

for (i=0;i<number_of_readings;i++)
printf("Time for reading number%f:",readings);
scanf("%.2f",&times);
}

/*_____________________________________________*/

void input_readings(int number_of_readings, float reading, float readings[])
{
int i;

printf("Enter readings:");

for(i=0;i<number_of_readings;i++)
printf("Reading for number%f:",readings);
scanf("%f",&readings);
}

/*____________________________________________*/

void display_results(int number_of_readings, float times[], float readings[])
{

int i;

for(i=0;i<number_of_readings;i++)
{
printf("Reading number: %f",readings);
if(readings==0)
printf("No wind");
else
printf("%f",times);
}
}

Sparks Moderator
#12

Cork24 said:
i dont like using case why dont you use
in your switch()
else
if
they are easy to debug.


switch() is always more efficient than if..else chains*.
It's also a lot easier to read a switch statement, assuming that you use proper indentation (hell, just run the code through indent or astyle) and that you have your code broken into functions when it gets too large to read, both of which are just good practice.


*nb. I said chains. switch() isn't necessarily more efficient than a single if statement.

#13

I'd say you're better off putting in the exact compiler output (line number and all).....

srsly78 Registered User
#14

Put code tags around it or all the formatting is lost when you post it to forum. Most likely you have left out a semicolon or bracket or something.

Sparks Moderator
#15

Want to share your thoughts?

Login here to discuss!