[Question] question in C problem - boards.ie
Boards.ie uses cookies. By continuing to browse this site you are agreeing to our use of cookies. Click here to find out more x
Post Reply  
 
 
Thread Tools Search this Thread
11-03-2012, 18:31   #1
Tomas_S
Registered User
 
Tomas_S's Avatar
 
Join Date: Jan 2009
Posts: 285
question in C problem

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[i]);
scanf("%.2f",&times[i]);
}

/*_____________________________________________*/

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[i]);
scanf("%f",&readings[i]);
}

/*____________________________________________*/

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[i]);
if(readings[i]==0)
printf("No wind");
else
printf("%f",times[i]);
}
}
Tomas_S is offline  
Advertisement
11-03-2012, 23:34   #2
Aswerty
Registered User
 
Join Date: Sep 2010
Location: Sligo
Posts: 379
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.
Aswerty is offline  
12-03-2012, 09:56   #3
Tomas_S
Registered User
 
Tomas_S's Avatar
 
Join Date: Jan 2009
Posts: 285
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!
Tomas_S is offline  
12-03-2012, 19:59   #4
omahaid
Registered User
 
omahaid's Avatar
 
Join Date: May 2002
Location: Cork city
Posts: 2,314
Quote:
Originally Posted by Tomas_S View Post
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.
omahaid is online now  
15-03-2012, 10:56   #5
Tomas_S
Registered User
 
Tomas_S's Avatar
 
Join Date: Jan 2009
Posts: 285
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[i]);
scanf("%.2f",&times[i]);
}

/*_____________________________________________*/

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[i]);
scanf("%f",&readings[i]);
}

/*____________________________________________*/

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[i]);
if(readings[i]==0)
printf("No wind");
else
printf("%f",times[i]);
}
}
Tomas_S is offline  
Advertisement
15-03-2012, 13:04   #6
ressem
Registered User
 
Join Date: Apr 2002
Posts: 1,908
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.
ressem is offline  
15-03-2012, 13:25   #7
Tomas_S
Registered User
 
Tomas_S's Avatar
 
Join Date: Jan 2009
Posts: 285
Quote:
Originally Posted by ressem View Post
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
Tomas_S is offline  
15-03-2012, 13:51   #8
Aswerty
Registered User
 
Join Date: Sep 2010
Location: Sligo
Posts: 379
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.
Aswerty is offline  
15-03-2012, 14:27   #9
Tomas_S
Registered User
 
Tomas_S's Avatar
 
Join Date: Jan 2009
Posts: 285
Quote:
Originally Posted by Aswerty View Post
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
Tomas_S is offline  
Advertisement
17-03-2012, 13:00   #10
Cork24
Banned
 
Join Date: Jul 2009
Posts: 2,623
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.

Last edited by Cork24; 17-03-2012 at 13:03.
Cork24 is offline  
20-03-2012, 11:43   #11
Tomas_S
Registered User
 
Tomas_S's Avatar
 
Join Date: Jan 2009
Posts: 285
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[i]=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[i]);
scanf("%.2f",&times[i]);
}

/*_____________________________________________*/

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[i]);
scanf("%f",&readings[i]);
}

/*____________________________________________*/

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[i]);
if(readings[i]==0)
printf("No wind");
else
printf("%f",times[i]);
}
}
Tomas_S is offline  
20-03-2012, 11:53   #12
Sparks
Category Moderator
 
Sparks's Avatar
 
Join Date: Apr 2003
Posts: 34,312
Quote:
Originally Posted by Cork24 View Post
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.
Sparks is offline  
20-03-2012, 11:53   #13
omahaid
Registered User
 
omahaid's Avatar
 
Join Date: May 2002
Location: Cork city
Posts: 2,314
I'd say you're better off putting in the exact compiler output (line number and all).....
omahaid is online now  
20-03-2012, 11:54   #14
srsly78
Registered User
 
Join Date: Oct 2010
Posts: 5,839
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.
srsly78 is offline  
20-03-2012, 12:02   #15
Sparks
Category Moderator
 
Sparks's Avatar
 
Join Date: Apr 2003
Posts: 34,312
Quote:
Originally Posted by Tomas_S View Post
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.
The problem is more basic than you think. You just can't see it because you're not indenting the code.
I ran it through astyle (making no changes) and got this:
Code:
#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[i]=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[i]);
        scanf("%.2f",&times[i]);
    }

    /*_____________________________________________*/

    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[i]);
        scanf("%f",&readings[i]);
    }

    /*____________________________________________*/

    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[i]);
            if (readings[i]==0)
                printf("No wind");
            else
                printf("%f",times[i]);
        }
    }
You can now clearly see that you didn't have a closing brace on your main() function, so you're trying to define all the other functions inside the main() function. There's nothing strictly illegal about that, but it's not really best practice the way you've done it, and because your definitions were outside main(), it looks like it was just a mistake, especially since you're still missing the closing brace for main() by the time you get to the end of the code.
Sparks is offline  
Post Reply

Quick Reply
Message:
Remove Text Formatting
Bold
Italic
Underline

Insert Image
Wrap [QUOTE] tags around selected text
 
Decrease Size
Increase Size
Please sign up or log in to join the discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Share Tweet