I am tring to pass an array to a function but when i compile it says array undeclared first use in function.i thought once i pass it as parameter it becomes known by the function.here is my code:
/*
Write a program that reads quizzes from a file 'q.txt', randomly selects a number of questions,and presents them to the user one-by-one, collecting user answers. After user has answered all questions,program gives user the score.Use functions to structure your program. Extra credit is given if you use dynamic data structure(s).*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 50
#define MAX_LEN 300

void generatequiz(void)
{
    static int seen[MAX];
    static int array[10];
    int i;

    srand(time(NULL));   /* Seed the random number generator. */

    for (i=0; i<10; i++)
    {
        int r;
        do
        {
            r = rand() / (RAND_MAX / MAX + 1);
        }
        while (seen[r]);
        seen[r] = 1;
        array[i] = r + 1;
    }


    int sort(const void *x, const void *y)
    {
        return (*(int*)x - *(int*)y);

    }
    qsort(array, 10, sizeof(int), sort);
    return 0;

}

void readquestions ( struct questions structered_array[])  /*reading questions to user give a score*/
{
    int j,score_count=0;
    char user_answer;

    for (j=0;j<10;j++)
    {
        printf( "Press keys a or b or c or d to give your answer \n");
        printf("%s\n %s\n %s\n s%\n",structured_array[j].question,
               structured_array[j].choice_1,
               structured_array[j].choice_2,
               structured_array[j].choice_3);

        user_answer=getchar();
        if (user_answer= structured_array[j].answer)
            score_count++;
    }
    printf( "Your score is %d out of 10 \n");

    return 0;
}

int main()
{
    generatequiz();

    int array1[10];
    int count = 1;
    char unstructered_array[10];
    char random_array1[10];
    int i=0;
    FILE *fp;

    fp = fopen("q.txt", "r");

    if (fp == NULL)
    {
        puts("Cannot open file for reading");
        exit(EXIT_FAILURE);
    }

    char my_string[MAX_LEN + 1];
    while (fgets(my_string, MAX_LEN + 1, stdin) != NULL)

        if (count==random_array1[i])
        {
            struct questions
            {
                char question [100];
                char choice_1 [50];
                char choice_2 [50];
                char choice_3 [50];
                char answer   ;
            };
            struct questions structered_array[10];


            structered_array[i].question[100] = strtok(my_string, ":");
            structered_array[i].choice_1[50] = strtok(NULL, ":");
            structered_array[i].choice_2[50] = strtok(NULL, ":");
            structered_array[i].choice_3[50] = strtok(NULL, ":");
            structered_array[i]. answer = strtok(NULL, "\n");

            count++;
            i++;
        }

    fclose(fp);
    return structered_array;

    readquestions(structured_array);
    return 0;

}

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

q.txt sample:
How many languages and dialects are spoken by people all over the world?: A.6,000 :B.9,000 :C.4,000 D.1,000 :E.b
Approximately, how many people speak Chinese language?: A.1 billion :B.1 million :C.1 lakh :D.1 thousand :E.a

@fxwebpage
Really thats your advise. If your code is not compiling then there is some error in your code !!!!

@melusim

Is this your code or did you copy paste from some where ?There are so so many errors in it

1. You have declared the sort function in the generatequiz function
2. In the read question function, in the second printf there is a spelling mistake. It should be structered_array[j].question as opposed to structured_array[j].question. You have made this mistake all over the function
3. In the generatequiz function you are trying to return a value when the return type of the function is void.
4. Dont define your struct inside the if block in the main function. Make it global
5. Include string.h
6. You have not used strtok correctly. Google for the correct syntax and apply the correct syntax here


I did not have the patience to investigate further.... There may be many more errors. Make all these changes and post the results

Edited 6 Years Ago by abhimanipal: n/a

thanks, here is an improved version.we are not allowed to use global variables but use return and parameters.As for strtok can i change my structure members to be pointers and pass tokens to them.

/*
Write a program that reads quizzes from a file 'q.txt', randomly selects a number of questions, and
presents them to the user one-by-one, collecting user answers. After user has answered all questions,
program gives user the score.Use functions to structure your program. Extra credit is given if you use dynamic data structure(s).*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX 50
#define MAX_LEN 300

int generatequiz(void)
{
    static int seen[MAX];
    static int random_array[10];
    int i;

    srand(time(NULL));   /* Seed the random number generator. */

    for (i=0; i<10; i++)
    {
        int r;
        do
        {
            r = rand() / (RAND_MAX / MAX + 1);
        }
        while (seen[r]);
        seen[r] = 1;
        random_array[i] = r + 1;
    }


    int sort(const void *x, const void *y)/*sort array*/
    {
        return (*(int*)x - *(int*)y);

    }
    qsort(random_array, 10, sizeof(int), sort);

    return random_array;
}

void readquestions (quiz_array)  /*reading questions to user give a score*/
{
    int j,score_count=0;
    char user_answer;

    for (j=0;j<10;j++)
    {
        printf( "Press keys a or b or c or d to give your answer \n");
        printf("%s\n %s\n %s\n s%\n",quiz_array[j].question,
               quiz_array[j].choice_1,
               quiz_array[j].choice_2,
               quiz_array[j].choice_3);

        user_answer=getchar();
        if (user_answer= quiz_array[j].answer)
            score_count++;
    }
    printf( "Your score is %d out of 10 \n",score_count);


}

int main()
{
    struct questions
    {
        char question [100];
        char choice_1 [50];
        char choice_2 [50];
        char choice_3 [50];
        char answer   ;
    };

    struct questions quiz_array[10];

    generatequiz();

    struct questions getline(int random_array)
    {
        int count = 1;
        int i=0;
        FILE *fp;

        fp = fopen("q.txt", "r");

        if (fp == NULL)
        {
            puts("Cannot open file for reading");
            exit(EXIT_FAILURE);
        }

        char my_string[MAX_LEN + 1];
        while (fgets(my_string, MAX_LEN + 1, stdin) != NULL)

            if (count==random_array[i])
            {
                quiz_array[i].question[100] = strtok(my_string, ":");
                quiz_array[i].choice_1[50] = strtok(NULL, ":");
                quiz_array[i].choice_2[50] = strtok(NULL, ":");
                quiz_array[i].choice_3[50] = strtok(NULL, ":");
                quiz_array[i]. answer = strtok(NULL, "\n");
                count++;
                i++;
            }

        fclose(fp);
        return quiz_array;
    }

    readquestions(quiz_array);


    return 0;

}

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

now only 2 types of errors.
error1: subscripted value is neither array nor pointer-> i have no idea
error2: incompatible types in return (in getline function)-> i returned structured array which is of struct questions type so why is compiler complaining?.

help!!!

Post the latest version of your code man. The code posted above still does not contain the changes I told you to make earlier

Also use CODE TAGS.

i asked questions on how to implement the suggestion you made and my limitations on global variables above my code.fine i forgot to take out declaration of sort function out of the generatequiz function but my main problem now is error1: subscripted value is neither array nor pointer.

/*Melusi Mlotshwa
Student number 0604482
Final Assignment-question 2
Write a program that reads quizzes from a file 'q.txt', randomly selects a number of questions, and
presents them to the user one-by-one, collecting user answers. After user has answered all questions,
program gives user the score.Use functions to structure your program. Extra credit is given if you use dynamic data structure(s).*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX 50
#define MAX_LEN 300

int sort(const void *x, const void *y)/*sort array*/
{
    return (*(int*)x - *(int*)y);

};

int generatequiz(void)
{
    static int seen[MAX];
    static int random_array[10];
    int i;

    srand(time(NULL));   /* Seed the random number generator. */

    for (i=0; i<10; i++)
    {
        int r;
        do
        {
            r = rand() / (RAND_MAX / MAX + 1);
        }
        while (seen[r]);
        seen[r] = 1;
        random_array[i] = r + 1;
    }

    qsort(random_array, 10, sizeof(int), sort);

    return random_array;
}

void readquestions (quiz_array)  /*reading questions to user give a score*/
{
    int j,score_count=0;
    char user_answer;

    for (j=0;j<10;j++)
    {
        printf( "Press keys a or b or c or d to give your answer \n");
        printf("%s\n %s\n %s\n s%\n",quiz_array[j].question,
               quiz_array[j].choice_1,
               quiz_array[j].choice_2,
               quiz_array[j].choice_3);

        user_answer=getchar();
        if (user_answer= quiz_array[j].answer)
            score_count++;
    }
    printf( "Your score is %d out of 10 \n",score_count);


}

void main()
{
    struct questions
    {
        char question ;
        char choice_1 ;
        char choice_2 ;
        char choice_3 ;
        char answer   ;
    };

    struct questions quiz_array[10];

    generatequiz();

    struct questions getline(int random_array)
    {
        int count = 1;
        char *token[5];
        int i, n = 0;
        FILE *fp;

        fp = fopen("q.txt", "r");

        if (fp == NULL)
        {
            puts("Cannot open file for reading");
            exit(EXIT_FAILURE);
        }

        char my_string[MAX_LEN + 1];
        while (fgets(my_string, MAX_LEN + 1, stdin) != NULL)

            if (count==random_array[i])
            {
                token[0] = strtok(my_string, ":");
                if (token[0] != NULL)
                {
                    for (n = 1; n < 5 && (token[n] = strtok(NULL,":")) != NULL; n++);
                }

                quiz_array[i].question = token[0];
                quiz_array[i].choice_1 = token[1];
                quiz_array[i].choice_2= token[2];
                quiz_array[i].choice_3 = token[3];
                quiz_array[i]. answer = token[4];
                count++;
                i++;

                fclose(fp);
                return quiz_array;
            }

        readquestions(quiz_array);
    }

1. In the generate quiz function, the function return type is an int but you are trying to return an array.
2. The rest of the errors are coming because when you define the function readquestions, the compiler does not know that there exits a struct called as question...

Let me see if I can figure out how to solve this problem

The C standard doesn't support nested functions. Therefore, struct questions getline(int random_array) cannot be defined inside main(). Furthermore, that said function returns a struct questions according to the header, and inside the block you return an array of struct questions

Are you not allowed to write the definition of the struct globally or you can write the definition of the struct globally and not make global objects ?
There is significant difference between the 2 statements. If you can write the definition globally, it will become much easier to code

i suppose you can write the definition of the struct globally but i tried it nothing changed.

It will make a world of difference ....
In my posts above I told you there is a problem with the generate quiz function and then Aia pointed out another bug, have you made those 2 changes ?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX 50
#define MAX_LEN 300

int sort(const void *x, const void *y)/*sort array*/
{
    return (*(int*)x - *(int*)y);

};

generatequiz(void)
{
    static int seen[MAX];
    static int random_array[10];
    int i;

    srand(time(NULL));   /* Seed the random number generator. */

    for (i=0; i<10; i++)
    {
        int r;
        do
        {
            r = rand() / (RAND_MAX / MAX + 1);
        }
        while (seen[r]);
        seen[r] = 1;
        random_array[i] = r + 1;
    }

    qsort(random_array, 10, sizeof(int), sort);

    return random_array;
}
breakline(char my_string,int count)
{
    struct questions
    {
        char question ;
        char choice_1 ;
        char choice_2 ;
        char choice_3 ;
        char answer   ;
    };
    struct questions quiz_array[10];
    char *token[5];
    int n = 0;
    token[0] = strtok(my_string, ":");
    if (token[0] != NULL)
    {
        for (n = 1; n < 5 && (token[n] = strtok(NULL,":")) != NULL; n++);
    }

    quiz_array[count-1].question = token[0];
    quiz_array[count-1].choice_1 = token[1];
    quiz_array[count-1].choice_2= token[2];
    quiz_array[count-1].choice_3 = token[3];
    quiz_array[count-1]. answer = token[4];

    return quiz_array;
}
void readquestions ( struct questions quiz_array[])  /*reading questions to user give a score*/
{
    int j,score_count=0;
    char user_answer;

    for (j=0;j<10;j++)
    {
        printf( "Press keys a or b or c or d to give your answer \n");
        printf("%s\n %s\n %s\n s%\n",
               quiz_array[j].question,
               quiz_array[j].choice_1,
               quiz_array[j].choice_2,
               quiz_array[j].choice_3);

        user_answer=getchar();
        if (user_answer= quiz_array[j].answer)
            score_count++;
    }
    printf( "Your score is %d out of 10 \n",score_count);
}

main(int random_array)
{
    generatequiz();

    int count = 1;

    FILE *fp;

    fp = fopen("q.txt", "r");

    if (fp == NULL)
    {
        puts("Cannot open file for reading");
        exit(EXIT_FAILURE);
    }

    char my_string[MAX_LEN + 1];
    while (fgets(my_string, MAX_LEN + 1, stdin) != NULL)

        if (count==random_array[count-1])
        {
            breakline(char my_string);
            count++;
        }
    fclose(fp);

    readquestions(struct questions quiz_array[]);
    return 0;
}

i think i have a problem with writing return types in function definitions when the functions return structures, arrays or structured arrays.are these correct

int generatequiz(void)
//some code
return array;

and

struct my_struct breakline(char my_string)
//some code
return array/*array contains elements of my_struct type*/

i think i have a problem with writing return types in function definitions when the functions return structures, arrays or structured arrays.are these correct

int generatequiz(void)
//some code
return array;

Only if array is defined as an int, which I doubt with that name.

struct my_struct breakline(char my_string)
//some code
return array/*array contains elements of my_struct type*/

Only if array is of type struct my_struct, which it is doubtful with that identifier

Edited 6 Years Ago by Aia: n/a

can you suggest some reading material on this topic i cant seem to find any on the web.

can you suggest some reading material on this topic i cant seem to find any on the web.

Some members of this forum has compiled an abundance of C resources into a thread for your convenience. Take a look at it.
Just perusing around I was able to find about functions (very basic)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX 50
#define MAX_LEN 300

struct questions
{
    char question[200] ;
    char choice_1[20] ;
    char choice_2[20] ;
    char choice_3[20];
    char answer [2];
};


int sort(const void *x, const void *y)
{
    return (*(int*)x - *(int*)y);

};

int *generatequiz(void)
{
    static int seen[MAX];
    static int random_array[10];
    int i;

    srand(time(NULL));   /* Seed the random number generator. */

    for (i=0; i<10; i++)
    {
        int r;
        do
        {
            r = rand() / (RAND_MAX / MAX + 1);
        }
        while (seen[r]);
        seen[r] = 1;
        random_array[i] = r + 1;
    }

    qsort(random_array, 10, sizeof(int), sort);/*sort array*/

    return random_array;
}


struct questions Breakline( int random_array[],char my_string,int count)

{

    struct questions quiz_array[10];
    char *token[5];
    int  n = 0;
    token[0] = strtok(my_string, ":");

    if (token[0] != NULL)
    {
        for (n = 1; n < 5 && (token[n] = strtok(NULL,":")) != NULL; n++);
    }

    strcpy(quiz_array[count-1].question, token[0]);
    strcpy(quiz_array[count-1].choice_1, token[1]);
    strcpy(quiz_array[count-1].choice_2, token[2]);
    strcpy(quiz_array[count-1].choice_3, token[3]);
    strcpy(quiz_array[count-1].answer, token[4]);

    return quiz_array[count-1];
}


void Readquestions (struct questions quiz_array[])  /*reading questions to user give a score*/
{
    int i,j,score_count=0;
    char user_answer[2];

    for (j=0;j<10;j++)
    {
        printf( "Press keys a or b or c or d to give your answer \n");
        printf("%s\n %s\n %s\n s%\n",
               quiz_array[j].question,
               quiz_array[j].choice_1,
               quiz_array[j].choice_2,
               quiz_array[j].choice_3);

   scanf("%s",user_answer);
        if ((i = strcmp( user_answer, quiz_array[j].answer ))=0);
            score_count++;
    }
    printf( "Your score is %d out of 10 \n",score_count);

}

main(int random_array[])
{
    struct questions quiz_array[10];
    int count = 1;
    FILE *fp;

    generatequiz();

    fp = fopen("q.txt", "r");

    if (fp == NULL)
    {
        puts("Cannot open file for reading");
        exit(EXIT_FAILURE);
    }

    char my_string[MAX_LEN + 1];
    while (fgets(my_string, MAX_LEN + 1, stdin) != NULL)
    {

        if (count==random_array[count-1])
        {
            Breakline(random_array,my_string,count);
            count++;
        }
        fclose(fp);
    }

    Readquestions(quiz_array);

}

This is how far i have gone but i still get 4 errors,
line 57 error: invalid conversion from `char' to `char*
line 57 error: initializing argument 1 of `char* strtok(char*, const char*)
line 124 error:invalid conversion from `char* to `char'
line 124 error: initializing argument 2 of `questions Breakline(int*, char, int)'

Sorry i meant i still get 4 errors,
line 57 error: invalid conversion from `char' to `char*
line 57 error: initializing argument 1 of `char* strtok(char*, const char*)
line 118 error:invalid conversion from `char* to `char'
line 118 error: initializing argument 2 of `questions Breakline(int*, char, int)'

1. On line 57, strtok can be applied to char arrays and not chars

2. What kind of arguments have you supplied to main? Inspite of the fact that the compiler does not show you an error it is still a bad idea. main function should be defined either as
int main()
or
int main(int argc,char* argv[])

I think if you correct these 2 errors, your code may compile ....

Now my program runs well but cannot access token[4] in strtok

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define string_max 2000

main()
{
    FILE *fp;
    int randLine,i,j,n=0,score_count=0,nLines = 0;
    char my_string[string_max];
    char question[500] ;
    char choice_1[50] ;
    char choice_2[50] ;
    char choice_3[50];
    char choice_4[50];
    char answer  [1] ;
    char user_answer[1];
    char *token[6];

    srand(time(0));
    fp = fopen("q.txt", "r");

    for (j=0; j<10; j++)
    {
        while (!feof(fp))
        {
            fgets(my_string, string_max, fp);
            nLines++;
        }

        randLine = rand() % nLines;

        fseek(fp, 0, SEEK_SET);
        for (i = 0; !feof(fp) && i <= randLine; i++)
            fgets(my_string, string_max, fp);

        token[0] = strtok(my_string, ":");
        if (token[0] != NULL)
        {
            for (n = 1; n < 6 && (token[n] = strtok(NULL,":")) != NULL; n++);
        }
        strcpy(question, token[0]);
        strcpy(choice_1, token[1]);
        strcpy(choice_2, token[2]);
        strcpy(choice_3, token[3]);
        strcpy(choice_4, token[4]);
        strcpy(answer , token[5]);

        printf( "Press keys a or b or c to give your answers \n");
        printf("%s\n %s\n %s\n %s\n %s\n",
               question,
               choice_1,
               choice_2,
               choice_3,
               choice_4);

        scanf("%s",&user_answer);

        if (strcmp(answer,user_answer)==0)
        {
            score_count++;
        }
    }
    printf( "Your score is %d out of 10 \n",score_count);

}

How many languages and dialects are spoken by people all over the world?: A.6,000 :B.9,000 :C.4,000 D.1,000 :E.b
Approximately, how many people speak Chinese language?: A.1 billion :B.1 million :C.1 lakh .1 thousand :E.a

This article has been dead for over six months. Start a new discussion instead.