Hey guys, I'm trying to work on this switch menu and I'm not sure how to call a function with parameters from a case statement. Here is the code for my program so far (not finished with the actual program... Just need help with calling functions from the menu, so please don't comment on anything else as I'm aware that there are other things that still need to be fixed!)

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

int menu();
void heading(void);
double getgrades(double s[], int x);
double getdouble(double min, double max, char item[]);
void showgrades(double s[], int count);
double changegrade();
double findgrades();
void message(char msg[]);
int menuerror(int min, int max, char prompt[]);
int prompt(char msg[]);
int quit(void);

/* ================================================================== */

// ** Functions **

int main()
{

	int TRUE = 1;
	int FALSE = 0;
	int select, end = TRUE;
	double s;
	int x;
	int count;
	double grade;
	double scores[101];

	do
	{

		select = menu();
		switch (select)
		{
		case 1: getgrades((double) s, (int) x); break;
		case 2: showgrades((double) s, (int) count); break;
		case 3: changegrade(); break;
		case 4: findgrades(); break;
		case 5: end = quit(); break;
		default: message("\n\nPlease make a selection between 1 and 5.\a");
		}
	}

	while (end);
	return 0;
}
/*  ================================================================  */

int menu()
{
	int choice;

	system("cls");

	printf("Grade Score Database\n\n");
	printf("1  =  Get Grades\n");
	printf("2  =  Show Grades\n");
	printf("3  =  Change Grade\n");
	printf("4  =  Find Grade\n");
	printf("5  =  Quit\n\n");
	choice = menuerror(1, 5, "Enter your selection");

	return(choice);
}
/*  ================================================================  */

void heading()
{
	system("cls");
	printf("Grade Score Database\n\n\n");
	return;
}
/*  ================================================================  */

double getgrades(double *s, int x)
{
	double scores[101];
	int z;

	for (z = 1; z < 101; z++)
	
	{
	printf("Enter a grade, or 0 to quit: ");
	scanf("%d", &*(scores+z));
	}
	while (*(scores+z) != 0);

	return 0;
}
/*  ================================================================  */

void showgrades(double *s, int x)
{
	int z;
	double scores[101];

	system("cls");

	printf("Score Report\n\n");
	printf("_Position________Grade_\n\n");
	for (z = 1; z < 101; z++)
	{
		printf(" %2d%20.2lf\n", z, *(s+z));
		
	}
	
	printf("Press ENTER to return to the Main Menu...");
	getchar();


}
/*  ================================================================  */

void changegrade(double *s, int x)
{
	int z;
	double total = 0;

	system("cls");

	printf(" Product Cost by Type\n\n");
	printf("_Type_____________Cost_\n\n");

	for (z = 1; z < x; z++)
	{
		printf(" %2d%2d\n", z, *(s+z));
	}
	
	printf("Press ENTER to return to the Main Menu...");
	getchar();
}
/*  ================================================================  */

void message(char msg[])
{
	printf("%s\n\n", msg);
}
/*  ================================================================  */

int menuerror(int min, int max, char item[])
{
	int z;
	printf("%s from %d to %d: ", item, min, max);
	scanf("%d%*c", &z);
	while (z < min || z > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("%s from %d to %d: ", item, min, max);
		scanf("%d%*c", &z);
	}
	return(z);
}

/*  ================================================================  */

int prompt(char msg[])
{
	int z;
	do
	{
		printf("%s (Y/N)? ", msg);
		z = toupper(getchar());
		if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
	}
	while (z != 'Y' && z != 'N');
	return(z);
}
/*  ================================================================  */

int quit()
{
	int TRUE = 1;
	int FALSE = 0;
	int z, end = TRUE;

	z = prompt("\nEnd program\a");
	if (z == 'Y')
	{
		system("cls");
		message("\nGrade Score Database Closed Succesfully.");
		end = FALSE;
	}
	return(end);
}
/*  ================================================================  */

K just to clarify the problem I'm having, I'm trying to call getgrades(), showgrades(), etc. from the menu, but it keeps giving me errors regarding the parameters. The original parameters I had for the switch statement functions were the same as the ones in the prototypes, but it kept giving me errors so I corrected it to what it is now. Still won't work though!

Getgrader() is defined like this: double getgrades(double s[], int x); But when you call it, you give double s; as a parameter. S is a single double, but your function is expecting a pointer to an array of doubles.

Why would you want to send an array anyway? You don't do anything with it in your function? Perhaps you should look up "passing values by reference"

Yeah I'm extremely confused about the whole thing. I finished writing the code and Im only getting 5 errors but I have a feeling it's not going to work out at all the way it's supposed to. To make things even more confusing all references to the scores array (outside of the main()function) have to use pointer notation. Basically I'm writing a program to input up to 100 grades into positions 1-100 of an array. Getgrades() prompts for a grade to input into the array, and keeps going until the user enters 0. Showgrades() should display the grades entered, along with their position in the array. Changegrade() is supposed to prompt for a position in the array and allow the user to change the value currently stored in it. Findgrades() is supposed to ask for the lowest grade to search for and the highest grade to search for, and then display all of the grades that fall in that range. So that basically sums it up, but I'm having a really hard time trying to figure out all of the stuff with the array. It says in my homework assignment that the main function should define the array and the other functions should receive it, so I thought that's what I was doing... Here is what I have. Please help guys, this is a really important assignment and I have been working on it for days. This is an online class so it's really difficult to ever get ahold of the instructor for help, and it makes the course much more difficult because the book doesn't help at all!! I haven't even had the chance to run the program to see what it looks like because I'm still getting build errors!

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

int menu();
void heading(void);
double getgrades(double s[]);
void showgrades(double s[], int count);
double changegrade(double s[], int count);
double findgrades(double s[], int count);
void message(char msg[]);
void show(double s[]);
int menuerror(int min, int max, char prompt[]);
int prompt(char msg[]);
int quit(void);

/* ================================================================== */

// ** Functions **

int main()
{

	int TRUE = 1;
	int FALSE = 0;
	int select, end = TRUE;
	int x;
	int count;
	double scores[101];

	do
	{

		select = menu();
		switch (select)
		{
		case 1: getgrades(double s[]); break;
		case 2: showgrades(double s[], int count); break;
		case 3: changegrade(double s[], int count); break;
		case 4: findgrades(double s[], int count); break;
		case 5: end = quit(); break;
		default: message("\n\nPlease make a selection between 1 and 5.\a");
		}
	}

	while (end);
	return 0;
}
/*  ================================================================  */

int menu()
{
	int choice;

	system("cls");

	printf("Grade Score Database\n\n");
	printf("1  =  Get Grades\n");
	printf("2  =  Show Grades\n");
	printf("3  =  Change Grade\n");
	printf("4  =  Find Grade\n");
	printf("5  =  Quit\n\n");
	choice = menuerror(1, 5, "Enter your selection");

	return(choice);
}
/*  ================================================================  */

void heading()
{
	system("cls");
	printf("Grade Score Database\n\n\n");
	return;
}
/*  ================================================================  */

double getgrades(double *s)
{
	double scores[101];
	int z, count;

	for (z = 1; z < 101; z++)
	count += 1;
	{
	printf("Enter a grade, or 0 to quit: ");
	scanf("%d", &*(scores+z));
	}

	
	while (*(scores+z) != 0);

	return(count);
}
/*  ================================================================  */

void showgrades(double *s, int count)
{
	int z;
	double scores[101];

	system("cls");

	printf("Score Report\n\n");
	printf("_Position________Grade_\n\n");
	for (z = 1; z < 101; z++)
	{
		printf(" %2d%20.2lf\n", z, *(s+z));
		
	}
	
	printf("Press ENTER to return to the Main Menu...");
	getchar();


}
/*  ================================================================  */

double changegrade(double *s, int count)
{
	int z;
	double scores[101];

	for (z = 1; z < 101; z++)
	
	{
	printf("Enter grade position to edit, or 0 if no changes: ");
	scanf("%d", &z);
	printf("Enter new grade for position %d: ", z);
	scanf("%d", &*(scores+z));
	}
	while (*(scores+z) != 0);

	return 0;
}
/*  ================================================================  */

double findgrades(double *s, int count)
{
	int z, low, high;
	double scores[101];

	printf("Enter lowest grade to locate: \n");
	scanf("%d", &low);
	printf("Enter highest grade to locate: \n");
	scanf("%d", &high);
	
	printf("The following grades are within the specified range: \n\n");
	for (z = 1; z < 101; z++)

	if (scores[z] > low || scores[z] < high)
	{
		show((double) *s);
	}
	printf("Press ENTER to return to the Main Menu...");
	getchar();

}
/*  ================================================================  */

void show (double *s)
{
  int z;
  double scores[101];

for (z = 1; z < 101; z++) printf ("%4d", scores[z]);
printf ("\n");

}
/*  ================================================================  */

void message(char msg[])
{
	printf("%s\n\n", msg);
}
/*  ================================================================  */

int menuerror(int min, int max, char item[])
{
	int z;
	printf("%s from %d to %d: ", item, min, max);
	scanf("%d%*c", &z);
	while (z < min || z > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("%s from %d to %d: ", item, min, max);
		scanf("%d%*c", &z);
	}
	return(z);
}

/*  ================================================================  */

int prompt(char msg[])
{
	int z;
	do
	{
		printf("%s (Y/N)? ", msg);
		z = toupper(getchar());
		if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
	}
	while (z != 'Y' && z != 'N');
	return(z);
}
/*  ================================================================  */

int quit()
{
	int TRUE = 1;
	int FALSE = 0;
	int z, end = TRUE;

	z = prompt("\nEnd program\a");
	if (z == 'Y')
	{
		system("cls");
		message("\nGrade Score Database Closed Succesfully.");
		end = FALSE;
	}
	return(end);
}
/*  ================================================================  */

Ok. Let's start from the top with the first thing I notice: in main() you declare double scores[101]; But when you call the functions you pass S. S is not defined, so that probably gives you a few errors. Call the functions with 'scores' : getgrades( scores); And why are you passing the count variable? It's never used or assigned a value.

yeah the whole scores[101] thing is confusing me. Don't know when to use it! As far as count goes, it says to receive the array and count in each function... I assumed this meant the count that I got from how many grades were entered...

Can anyone help me with this? I'm stressing out so much because I can't get it to work and it's due at midnight. I've been up since yesterday trying to figure it out while I'm not at work, and I'm going out of my mind! I just need to at least get it to build and run so I can go through the rest of the errors from there! Any help would be sincerely appreciated.

The thing I can't figure out is why I'm getting these errors in the switch menu() function. I thought I was supposed to just put exactly what I had written in the prototype as the parameters. The errors I'm getting are:

Error 1 error C2144: syntax error : 'double' should be preceded by ')' 48
Error 2 error C2660: 'showgrades' : function does not take 0 arguments 48
Error 3 error C2059: syntax error : ')' 48
Error 4 error C2144: syntax error : 'double' should be preceded by ')' 49
Error 5 error C2660: 'changegrade' : function does not take 0 arguments 49
Error 6 error C2059: syntax error : ')' 49
Error 7 error C2144: syntax error : 'double' should be preceded by ')' 50
Error 8 error C2660: 'findgrades' : function does not take 0 arguments 50
Error 9 error C2059: syntax error : ')' 50
Error 10 error C2664: 'show' : cannot convert parameter 1 from 'double' to 'double []' 162

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.