Thread: Cannot figure why I'm getting this error code...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    5

    Cannot figure why I'm getting this error code...

    This is a partial code, but it should compile ok I think. However, I get this error:

    Compiling c:\documents and settings\desktop\week_5\try2.c
    title
    names
    matrix
    grades
    averages

    c:\documents and settings\dan redmond\desktop\week_5\try2.c: line 133: bad type in function choose
    'void choose(char student[4][20], int scores[4][9]) { int answer=0'
    aborting compile


    Could anyone point out what is probably obvious.

    Thanks a ton.


    Code:
    #include <stdio.h>
    
    char title()
    	{
    	printf("\n\n\t-= Student Grading Calculator =-\n\n");
    	printf("You will first need to enter in the students.\n\n");
    	}
    	
    
    char names(char student[4][20])
    	{
    	student[0][20]='a';
    	student[1][20]='a';
    	student[2][20]='a';
    	student[3][20]='a';
    	student[4][20]='a';
    	printf("\nPlease enter the name of Student 1: ");
    	scanf("%s", &student[0][20]);
    	printf("\nPlease enter the name of Student 2: ");
    	scanf("%s", &student[1][20]);
    	printf("\nPlease enter the name of Student 3: ");
    	scanf("%s", &student[2][20]);
    	printf("\nPlease enter the name of Student 4: ");
    	scanf("%s", &student[3][20]);
    	printf("\nPlease enter the name of Student 5: ");
    	scanf("%s", &student[4][20]);
    	return student;
    	}
    void matrix(char student[4][20], int scores[4][9])
    	{
    	printf("\n\n\n\t\t-= Current Scores =-\n");
    	printf("\nName\t\t1   2   3   4   5   6   7   8   9   10\n");
    	printf("\n\n%s\t\t%d   %d   %d   %d   %d   %d   %d   %d   %d   %d", student[0][20], scores[0][0], scores[0][1], 
    	scores[0][2], scores[0][3], scores[0][4], scores[0][5], scores[0][6], scores[0][7], scores[0][8], scores[0][9]);			
    	printf("\n\n%s\t\t%d   %d   %d   %d   %d   %d   %d   %d   %d   %d", student[0][20], scores[1][0], scores[1][1], 
    	scores[1][2], scores[1][3], scores[1][4], scores[1][5], scores[1][6], scores[1][7], scores[1][8], scores[1][9]);
    	printf("\n\n%s\t\t%d   %d   %d   %d   %d   %d   %d   %d   %d   %d", student[0][20], scores[2][0], scores[2][1], 
    	scores[2][2], scores[2][3], scores[2][4], scores[2][5], scores[2][6], scores[2][7], scores[2][8], scores[2][9]);
    	printf("\n\n%s\t\t%d   %d   %d   %d   %d   %d   %d   %d   %d   %d", student[0][20], scores[3][0], scores[3][1], 
    	scores[3][2], scores[3][3], scores[3][4], scores[3][5], scores[3][6], scores[3][7], scores[3][8], scores[3][9]);
    	printf("\n\n%s\t\t%d   %d   %d   %d   %d   %d   %d   %d   %d   %d", student[0][20], scores[4][0], scores[4][1], 
    	scores[4][2], scores[4][3], scores[4][4], scores[4][5], scores[4][6], scores[4][7], scores[4][8], scores[4][9]);
    	printf("\n\nHit enter to return to the main menu");
    	getchar();
    	choose(student[4][20], scores[4][9]);
    	}
    	
    int grades(char student[4][20], int scores[4][9])
    	{
    	int rows;
    	int cols;
    	int num=0;
    	int selection=0;
    	int count=0;
    	printf("\n\n\n\t-= Grade Entry Menu =-\n\n");
    	printf("\n\n1. %s", student[0][20]);			
    	printf("\n\n2. %s", student[1][20]);
    	printf("\n\n3. %s", student[2][20]);
    	printf("\n\n4. %s", student[3][20]);
    	printf("\n\n5. %s", student[4][20]);
    	printf("\n\nPlease enter your selection: ");
    	scanf("%d", &selection);
    	if(selection==1)
    		{
    		for(cols=0; cols<=9; cols++)
    		{
    		count++;
    		printf("\nPlease enter grade #%d ", count);
    		scanf("%d", num);
    		scores[0][cols] = num;
    		}
    		}
    	if(selection==2)
    		{
    		for(cols=0; cols<=9; cols++)
    		{
    		count++;
    		printf("\nPlease enter grade #%d ", count);
    		scanf("%d", num);
    		scores[1][cols] = num;
    		}
    		}
    	if(selection==3)
    		{
    		for(cols=0; cols<=9; cols++)
    		{
    		count++;
    		printf("\nPlease enter grade #%d ", count);
    		scanf("%d", num);
    		scores[2][cols] = num;
    		}
    		}
    	if(selection==4)
    		{
    		for(cols=0; cols<=9; cols++)
    		{
    		count++;
    		printf("\nPlease enter grade #%d ", count);
    		scanf("%d", num);
    		scores[3][cols] = num;
    		}
    		}
    	if(selection==5)
    		{
    		for(cols=0; cols<=9; cols++)
    		{
    		count++;
    		printf("\nPlease enter grade #%d ", count);
    		scanf("%d", num);
    		scores[4][cols] = num;
    		}
    		}
    	return scores;	
    	printf("\n\nHit enter to return to the main menu");
    	getchar();
    	choose(student[4][20], scores[4][9]);
    	}
    
    int averages(char student[4][20], int scores[4][9])
    	{
    	int choice=0;
    	int count=0;
    	printf("\n\n\n\t-= Student Averages Menu =-\n\n");
    	printf("\n\n1. %s", student[0][20]);			
    	printf("\n\n2. %s", student[1][20]);
    	printf("\n\n3. %s", student[2][20]);
    	printf("\n\n4. %s", student[3][20]);
    	printf("\n\n5. %s", student[4][20]);
    	printf("\n\nPlease enter your selection: ");
    	scanf("%d", &choice);
    	}
    	
    void choose(char student[4][20], int scores[4][9])
    	{
    	int answer=0;
    	printf("\n\n\t-= Student Grading Calculator Menu =-\n\n");
    	printf("1.\tEnter in grades for the students.\n");
    	printf("2.\tDisplay the grades for the students.\n");
    	printf("3.\tSee grade averages for the students.\n");
    	printf("4.\tRestart Program.\n");
    	printf("5.\tExit the program.\n");
    	printf("\n\nChoose now: ");
    	{
    	scanf("%d", &answer);
    	if (answer == 1)
    	{
    	grades(student[4][20], scores[4][9]);
    	}
    	if (answer == 2)
    	{
    	matrix(student[4][20], scores[4][9]);
    	}
    	if (answer == 3)
    	{
    	averages(student[4][20], scores[4][9]);
    	}
    	if (answer == 4)
    	{
    	return main();
    	}
    	if (answer == 5)
    	{
    	printf("The program will now close.");
    	return 0;
    	}
    	}
    	}	
    	
    int main()
    	{
    	char student[4][20];
    	int scores[4][9];
    	title();
    	names(student[4][20]);
    	choose(student[4][20], scores[4][9]);
    	getchar();
    	}

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    When you call choose() from main(), just pass student and scores. Don't include the subscripting. Also, your choose() function is trying to return values even though you specify the return type as void.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char names(char student[4][20])
    	{
    	student[0][20]='a';
    You have a buffer overrun here. The last addressable element in an array declared like so
    Code:
    char array[20];
    is array[19], because array subscripts start at zero.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    Thanks all. I can get it to compile now, btu it isn't passing my values as I need them too. I'll work on it a bit - at least it compiles so I can see the effects of my changes now.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("\n\n1. %s", student[0][20]);
    student[0][20] is a character (an out-of-bounds character), but printf() is expecting a string. Pass it student[0].

    [edit] You probably want averages() to return choice.

    Code:
    	return scores;	
    	printf("\n\nHit enter to return to the main menu");
    	getchar();
    	choose(student[4][20], scores[4][9]);
    Code after a return statement is less than useless . . . .

    You could replace those long printf() statements in matrix() with a few for loops.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    scanf("%s", &student[2][20]);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    If you must use scanf(), then pass it like so:
    Code:
    scanf("%s", student[2]);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    Quote Originally Posted by dwks
    Code:
    scanf("%s", &student[2][20]);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    If you must use scanf(), then pass it like so:
    Code:
    scanf("%s", student[2]);

    Ahhh, yeah a lot makes sense now.

    Thanks again.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    Quote Originally Posted by 77_suzuki
    Ahhh, yeah a lot makes sense now.

    Thanks again.
    Ok, another stupid question.

    I've got it running now at least and have incorporated more loops and such. I know I'm slow but I'm trying to get this thing down.

    Here's what I think is my final hangup.

    I am obviously passing the student 2D array variable just fine to multiple functions.

    What is killing my program now, is the fact that my Grades 2D array is not passing correctly.

    I define Grades[5][10] in the InputGrades() function, but it is not transferring to my Matrix() function.

    Any suggestions specific to this problem?

    Again, keep in mind I really want to learn how to do it, not just get a hand out.



    Code:
    #include <stdio.h>
    
    char StudentName[5][20]={'\0'}; //Student's names - Storage array variable.
    int Grades[5][10]={0};
    int cols=0;
    int num;
    int count;
    
    void title() //Function: Project Title
    {
    	printf("\n\n\t -= Week 5 - Learning Team C =-\n\n");
    	printf("\t-= Student Grading Calculator =-\n\n");
    	printf("You will first need to enter in the students.\n\n");
    }
    	
    char StudentCreation() //Function: Enter Students' Names
    {
    	int StudentCounter=0;
    	while (StudentCounter < 5)
    	
    	{
    		printf("\nPlease enter in the name of the student number %d: ",(StudentCounter+1));
    		scanf("%s",StudentName[StudentCounter]);
    		StudentCounter++;
    	}
    	
    }   
    	
    int InputGrades()
    {
    	int selection=0;
    	int go=0;
    	printf("\n\n\n\t-= Grade Entry Menu =-\n\n");
    	printf("\n\n1. %s", StudentName[0]);			
    	printf("\n\n2. %s", StudentName[1]);
    	printf("\n\n3. %s", StudentName[2]);
    	printf("\n\n4. %s", StudentName[3]);
    	printf("\n\n5. %s", StudentName[4]);
    	printf("\n\nPlease enter your selection: ");
    	scanf("%d", &selection);
    	if(selection==1)
    	
    	{
    		while (cols<10)
    		
    		{
    			count++;
    			printf("\nEnter grade #%d: ", count);
    			scanf("%d",num);
    			Grades[0][cols]=num;
    			cols++;
    		}	
    			
    	}
    	
    	printf("\n\n1. Enter in another student's grades.");			
    	printf("\n\n2. Return to the main menu.");
    	printf("\n\nPlease enter your selection: ");
    	scanf("%d", &go);
    	if(go==1)
    	
    	{
    		InputGrades();
    	}
    	
    	if(go==2)
    	
    	{
    		menu();
    	}
    	
    }
     
    	
    int matrix() //Function: Display Students' Grades
    {
    	int selection=0;
    	int go=0;
    	int count=0;
    	printf("\n\n\n\t\t-= Current Scores =-\n");
    	printf("\n\n1. %s", StudentName[0]);			
    	printf("\n\n2. %s", StudentName[1]);
    	printf("\n\n3. %s", StudentName[2]);
    	printf("\n\n4. %s", StudentName[3]);
    	printf("\n\n5. %s", StudentName[4]);
    	printf("\n\nPlease enter your selection: ");
    	scanf("%d", &selection);
    	if(selection==1)
    	
    	{
    		printf("\n\nHere are the grades that have been entered for %s:"StudentName[0]);
     		for (cols=0; cols<10; cols++)
     		
     		{
     			count++;
     			printf("\n\nGrade #%d \t %d", count, Grades[0][cols]);
      			
     		}
     	}
     	
     	printf("\n\n1. Display another student's grades.");			
    	printf("\n\n2. Return to the main menu.");
    	printf("\n\nPlease enter your selection: ");
    	scanf("%d", &go);
    	if(go==1)
    	
    	{
    		matrix();
    	}
    	
    	if(go==2)
    	
    	{
    		menu();
    	}
    	
    }
    
    int menu() //Function: Main Menu
    {
    	int answer=0;
    	printf("\n\n\n\t-= Student Grading Calculator Menu =-\n\n");
    	printf("1.\tEnter in grades for the students.\n");
    	printf("2.\tDisplay the grades for the students.\n");
    	printf("3.\tSee grade averages for the students.\n");
    	printf("4.\tRestart Program.\n");
    	printf("5.\tExit the program.\n");
    	printf("\n\nChoose now: ");
    	
    	{
    		scanf("%d", &answer);
    		if (answer == 1)
    		
    		{
    			InputGrades();
    		}
    		
    		if (answer == 2)
    		
    		{
    			matrix();
    		}
    		
    		if (answer == 3)
    		
    		{
    
    		}
    		
    		if (answer == 4)
    		
    		{
    			main();
    		}
    		
    		if (answer == 5)
    		
    		{
    			printf("\nThe program will now close.");
    		}
    		
    	}
    }
    	
     
     
    	
    int main() //Function: Main Program Start
    {
    	title();
    	StudentCreation();
    	menu();
    	getchar();
    }

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Well you're not passing anything, so as long as your array is being filled properly it should be available to all of your functions

    any function that does not return a value should be void

    Code:
    scanf("%d",&num);
    Code:
    printf("\n\nHere are the grades that have been entered for %s:", StudentName[0]);
    You should not be calling main() at all.
    Something like this may work. (where menu() returns answer)
    if answer == 4 call StudentCreation()
    Code:
    int main(void) //Function: Main Program Start
    {
        title();
        StudentCreation();
        while(menu() != 5);
        return 0;
    }
    You may want to prototype your functions, as you are calling some (menu) before they are defined
    Last edited by spydoor; 09-01-2006 at 02:44 PM.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    Quote Originally Posted by spydoor
    Well you're not passing anything, so as long as your array is being filled properly it should be available to all of your functions

    any function that does not return a value should be void

    Code:
    scanf("%d",&num);
    Code:
    printf("\n\nHere are the grades that have been entered for %s:", StudentName[0]);
    You should not be calling main() at all.
    Something like this may work. (where menu() returns answer)
    Code:
    int main(void) //Function: Main Program Start
    {
        title();
        StudentCreation();
        while(menu() != 5);
        return 0;
    }
    You may want to prototype your functions, as you are calling some (menu) before they are defined


    !!!!!!!!!!

    Thank you!! It works!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot figure out fscanf
    By deathrattle in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 02:27 AM
  2. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM