Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

question in C problem

  • 11-03-2012 6:31pm
    #1
    Registered Users Posts: 300 ✭✭


    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);
    }
    }


Comments

  • Registered Users Posts: 586 ✭✭✭Aswerty


    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.


  • Registered Users Posts: 300 ✭✭Tomas_S


    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!


  • Closed Accounts Posts: 2,497 ✭✭✭omahaid


    Tomas_S wrote: »
    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.


  • Registered Users Posts: 300 ✭✭Tomas_S


    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);
    }
    }


  • Registered Users Posts: 2,426 ✭✭✭ressem


    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.


  • Advertisement
  • Registered Users Posts: 300 ✭✭Tomas_S


    ressem wrote: »
    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


  • Registered Users Posts: 586 ✭✭✭Aswerty


    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.


  • Registered Users Posts: 300 ✭✭Tomas_S


    Aswerty wrote: »
    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 :)


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    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.


  • Registered Users Posts: 300 ✭✭Tomas_S


    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);
    }
    }


  • Advertisement
  • Registered Users Posts: 40,055 ✭✭✭✭Sparks


    Cork24 wrote: »
    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.


  • Closed Accounts Posts: 2,497 ✭✭✭omahaid


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


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    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.


  • Registered Users Posts: 40,055 ✭✭✭✭Sparks


    Tomas_S wrote: »
    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:
    #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.


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    you have not closed your main() with a {


  • Registered Users Posts: 300 ✭✭Tomas_S


    thanks guys for quick replies. I've no experience in this at all :)


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Why do you have a Int Menu (void)

    if you are setting up Menus..

    within your program you dont need to use a function to return the nunmber press.


  • Registered Users Posts: 300 ✭✭Tomas_S


    Cork24 wrote: »
    Why do you have a Int Menu (void)

    if you are setting up Menus..

    within your program you dont need to use a function to return the nunmber press.

    I suppose you're right.

    Just a novice here so following a similar example.


  • Closed Accounts Posts: 2,207 ✭✭✭longhalloween


    Just out of curiosity, what was the question you were given?


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Tomas_S wrote: »
    Cork24 wrote: »
    Why do you have a Int Menu (void)

    if you are setting up Menus..

    within your program you dont need to use a function to return the nunmber press.

    I suppose you're right.

    Just a novice here so following a similar example.

    I will post a following style menu later when I get a chance I find it better to debug then case functions


  • Advertisement
  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


      
        bool Exit = false;
        
        while (!Exit)
        {
            int choice;
            
            cout << endl;
            cout <<  << endl;
            cout << "                                       " << endl;
            cout << " WELCOME TO File RECORD             " << endl;
         
            cout << "Select Action: " << endl;
            cout << "1) Display Student Deails" << endl;
            cout << "2) Sort Using Insertion Sort" << endl;
            cout << "3) Sort Using Merge Sort" << endl;
            cout << "4) Search Sorted Array Using Binary Search" << endl;
            cout << "5) Add the Array to a Stack" << endl;
            cout << "6) Display the Array" << endl;
            cout << "7) Exit" <<endl;
            cout <<  << endl;
            cout << "Enter choice: ";
            cin >> choice;
            
            
    
            
            
            if (choice == 7) // Quits Program..
                
                
            {
                cout<< "Exit Program" << endl;
                Exit = true; 
            }
            
            
    
    else if (choice ==6)
    {
    
    
    }
    else if (choice ==5)
    {
    
    }
    
    
    etc etc
    
    else if (choice ==1)
    
    {
    
    
    }
    
    } // Close While Statement !! 
    
    Return 0;
    } // Close Main!!
    
    
    
    
    

    This is a Fair better when it comes to running down Errors


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    That isn't C and will only confuse the op.


  • Registered Users Posts: 300 ✭✭Tomas_S


    yeah I was wondering what language was it?


  • Registered Users Posts: 300 ✭✭Tomas_S


    Just out of curiosity, what was the question you were given?

    Its an easy question just a lack of time and experience makes it more difficult for me. I'll put up the question when I submit it.


  • Registered Users Posts: 40,055 ✭✭✭✭Sparks


    Tomas_S wrote: »
    yeah I was wondering what language was it?
    C++ (and not very idiomatic or efficient C++ at that - even in C++, switch() is better than a chain of if/else statements for something like this, and in C++, you'd expect a more object-based design; this is instead what we tend to call C++-- )


  • Registered Users Posts: 300 ✭✭Tomas_S


    Sparks wrote: »
    C++ (and not very idiomatic or efficient C++ at that - even in C++, switch() is better than a chain of if/else statements for something like this, and in C++, you'd expect a more object-based design; this is instead what we tend to call C++-- )
    I was thinking it was C++...


Advertisement