C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-09-2009, 08:50 PM   #1
Registered User
 
Join Date: Sep 2009
Posts: 10
Question help with functions?

hey evrabodah.
im having trouble with a function, for some reason my function is not returning anything. i've been staring at it for a good long while and i think i may be overlooking something, wouldn't be the first time. if you have the time and can see anything could you let me know?
thanks
emily


Code:
#include <stdio.h>
#define SENTINEL -1

double calc_percent (int gradeCount, int inputCount);

int main()
{
	int    gradeInput,
	        inputCount,
	        total = 0,
                highScore = 60,
	        lowScore = 59,
	        aCount = 0,
	        bCount = 0,
	        cCount = 0,
	        dCount = 0,
		fCount = 0;
	double	average;

	printf("I am ColossusGradeBot.  Give to me your offerings of human exam scores so that I may provide the answers that you require.");
	printf("\nEnter a numerical score to sum, enter -1 for report: ");
	scanf("%d", &gradeInput);

		while (gradeInput > SENTINEL)
		{	
			inputCount++;
			total += gradeInput;
			
				if (gradeInput >= 90)
				{
					aCount++;
				}
				else if (gradeInput >= 80)
				{
					bCount++;
				}
				else if (gradeInput >= 70)
				{
					cCount++;
				}
				else if (gradeInput >= 60)
				{
					dCount++;
					
				}
				else
				{
					fCount++;
						if (gradeInput <= lowScore)
						{
							lowScore = gradeInput;
						}
				}
			
			if (gradeInput >= highScore)
			{
				highScore = gradeInput;
			}
		
			printf("\nEnter another score to sum, enter -1 for report: ");
			scanf("%d", &gradeInput);	
	}
		

	printf("\nPlease enjoy report on human exam scores.");
	printf("\n*****************************************");

	printf("\nYou entered %d human exam scores.", inputCount);

	printf("\nA's - %d", aCount);
	printf("\nB's - %d", bCount);
	printf("\nC's - %d", cCount);
	printf("\nD's - %d", dCount);
	printf("\nF's - %d", fCount);
	printf("\n********");	

	printf("\nHighest human exam score - %d", highScore);
	printf("\nLowest human exam score - %d", lowScore);	
	
	average = total / inputCount;
	printf("\nAverage of human exam scores - %0.2f", average);
	
	printf("\n**********************************************\n");
	printf("\nPercentage of A's - %0.2f%%", calc_percent (aCount, inputCount));
	printf("\nPercentage of B's - %0.2f%%", calc_percent (bCount, inputCount));
	printf("\nPercentage of C's - %0.2f%%", calc_percent (cCount, inputCount));
	printf("\nPercentage of D's - %0.2f%%", calc_percent (dCount, inputCount));
	printf("\nPercentage of F's - %0.2f%%\n", calc_percent (dCount, inputCount));
	
	return(0);
}
			 	
double calc_percent (int gradeCount, int inputCount)
{
	double result;
	
	result = ((double)gradeCount) / inputCount * 100;
	
	return (result);
}
el9335 is offline   Reply With Quote
Old 10-09-2009, 08:58 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
My compiler says this:
Code:
temp.c:9: warning: 'inputCount' might be used uninitialized in this function
tabstop is offline   Reply With Quote
Old 10-09-2009, 09:06 PM   #3
Registered User
 
Join Date: Sep 2009
Posts: 10
thanks for running it i appreciate it. im not sure why it thinks i haven't identified it. i thought that when you identify the parameters in the prototype and at the top of the function you are declaring those variables. am i missing something? i'm new at this.
el9335 is offline   Reply With Quote
Old 10-09-2009, 09:11 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Read it again, and note that nowhere does it say "identify". Also note the difference between inputCount and, say, total. Or aCount. Or bCount. Or cCount. Or dCount. Or fCount.
tabstop is offline   Reply With Quote
Old 10-09-2009, 09:14 PM   #5
Registered User
 
Join Date: Sep 2009
Posts: 10
i thought that it was saying that i haven't identified, i didn't understand the compiler message so i looked it up and that was the only answer that i could find. i'll take a look thanks.
el9335 is offline   Reply With Quote
Old 10-09-2009, 09:22 PM   #6
Registered User
 
Join Date: Sep 2009
Posts: 10
got it. i thought it might be something silly. thanks for your help.
el9335 is offline   Reply With Quote
Old 10-09-2009, 09:26 PM   #7
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 731
Also, you have a copy/paste error here:
Code:
printf("\nPercentage of D's - %0.2f%%", calc_percent (dCount, inputCount));
printf("\nPercentage of F's - %0.2f%%\n", calc_percent (dCount, inputCount));
rags_to_riches is offline   Reply With Quote
Old 10-09-2009, 09:37 PM   #8
Registered User
 
Join Date: Sep 2009
Posts: 10
thanks, i caught that too, and changed the alg for high score. appreciate you helping me!
el9335 is offline   Reply With Quote
Old 10-10-2009, 01:17 AM   #9
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Quote:
Originally Posted by tabstop View Post
My compiler says this:
Code:
temp.c:9: warning: 'inputCount' might be used uninitialized in this function

Look out the changed code which is correct

Code:
#include <stdio.h>
#define SENTINEL -1

double calc_percent (int gradeCount, int inputCount);

int main()
{
	int    gradeInput,
	        inputCount,
	        total = 0,
                highScore = 60,
	        lowScore = 59,
	        aCount = 0,
	        bCount = 0,
	        cCount = 0,
	        dCount = 0,
		fCount = 0;
	double	average;

	printf("I am ColossusGradeBot.  Give to me your offerings of human exam scores so that I may provide the answers that you require.");
	printf("\nEnter a numerical score to sum, enter -1 for report: ");
	scanf("%d", &gradeInput);

		while (gradeInput > SENTINEL)
		{	
			inputCount++;
			total += gradeInput;
			
				if (gradeInput >= 90)
				{
					aCount++;
				}
				else if (gradeInput >= 80)
				{
					bCount++;
				}
				else if (gradeInput >= 70)
				{
					cCount++;
				}
				else if (gradeInput >= 60)
				{
					dCount++;
					
				}
				else
				{
					fCount++;
						if (gradeInput <= lowScore)
						{
							lowScore = gradeInput;
						}
				}
			
			if (gradeInput >= highScore)
			{
				highScore = gradeInput;
			}
		
			printf("\nEnter another score to sum, enter -1 for report: ");
			scanf("%d", &gradeInput);	
	}
		

	printf("\nPlease enjoy report on human exam scores.");
	printf("\n*****************************************");

	printf("\nYou entered %d human exam scores.", inputCount);

	printf("\nA's - %d", aCount);
	printf("\nB's - %d", bCount);
	printf("\nC's - %d", cCount);
	printf("\nD's - %d", dCount);
	printf("\nF's - %d", fCount);
	printf("\n********");	

	printf("\nHighest human exam score - %d", highScore);
	printf("\nLowest human exam score - %d", lowScore);	
	
	average = total / inputCount;
	printf("\nAverage of human exam scores - %0.2f", average);
	
	printf("\n**********************************************\n");
	printf("\nPercentage of A's - %0.2f%%", calc_percent (aCount, inputCount));
	printf("\nPercentage of B's - %0.2f%%", calc_percent (bCount, inputCount));
	printf("\nPercentage of C's - %0.2f%%", calc_percent (cCount, inputCount));
	printf("\nPercentage of D's - %0.2f%%", calc_percent (dCount, inputCount));
	printf("\nPercentage of F's - %0.2f%%\n", calc_percent (dCount, inputCount));
	
	return(0);
}
			 	
double calc_percent (int gradeCount, int inputCount)
{
	double result;
	
	result = ((double)gradeCount) / inputCount * 100;
	
	return (result);
}


See alway try to intialize the variable while declaring it ...
Acually in case of inputCount , as it is a local variable then it is holding some garbage value and when u are incrementing it it is having garbage
when you initialize the inputCount with 0 then the problem will not happen

Enjoy Porgamming
RockyMarrone is offline   Reply With Quote
Reply

Tags
function, yarg

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Void Functions Help bethanne41 C++ Programming 1 05-09-2005 05:30 PM
Functions and Classes - What did I do wrong? redmage C++ Programming 5 04-11-2005 11:50 AM
calling functions within functions edd1986 C Programming 3 03-29-2005 03:35 AM
Factory Functions HOWTO GuardianDevil Windows Programming 1 05-01-2004 01:41 PM
Shell functions on Win XP geek@02 Windows Programming 6 04-19-2004 05:39 AM


All times are GMT -6. The time now is 10:48 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22