Question:
C Programming - Sums always 2 less than they need to be?
Steven
2012-11-28 12:44:59 UTC
I'm writing a code in C (using dev-C++ saved as a c-type file), and I'm suppose to use functions to do everything. Well I have a function, and it works great! Problem is, the getGradeTotal function always sends back a sum 2 less than what the sum is. I see no problems with the coding, is it just an error with my editor, or am I missing something o.O? Here's the code as of right now.


#include
#include
#include

int getRange(int a, int b){
int grade;
do {
printf("Please enter a number inclusively between %d and %d.\n", a, b);
scanf("%d",&grade);
if (grade < a || grade > b) {
printf("This number is not between %d and %d.\n", a, b);}
}while (grade < a || grade > b);
printf("%d was accepted\n", grade);
return grade;
}

int getGradeTotal(int a, int b, int rep) {
int ttl;
while (rep > 0) {
ttl += getRange(a , b);
rep --;}
printf("The total is %d.\n",ttl);
return ttl;}



int main() {
int a = 0, b = 100, c = 110, ttl = 0, rept = 2, repp = 5, sal, test;
sal = getRange(a , b);
printf("%d\n",sal);
test = getGradeTotal(a, b, repp);
printf("%d\n",test);
scanf("%d",&sal);
return 0;
}

The scanf at the end is because dev C++ program window thingy will close immediately after running, so I'm unable to accually view the information it gives back. (But with the scanf there, I can read it all while it waits for input =3)


Sorry for the terrible spacing, Yahoo answers normally murders it.
Four answers:
?
2012-11-29 21:03:50 UTC
The only mistake was not initializing variable ttl in getGradeTotal(int, int, int)



function getRange works perfectly.

I simply commented main() for u. functions :-





int getGradeTotal(int a, int b, int rep) {

int ttl = 0; //INITIALIZED ttl to 0



while (rep > 0) {

ttl += getRange(a , b);

rep --;}



printf("The total is %d.\n",ttl);

return ttl; }



int main() {

int a = 0, b = 100, c = 110, ttl = 0, repp = 5, sal, test;



/*simply reading a number, and printing here - that's what you did*/

sal = getRange(a , b);

printf("%d\n",sal);





/*reading repp (here 5) number of numbers, and printing the sum*/

test = getGradeTotal(a, b, repp);

printf("%d\n",test);





/*till here, we had 6 inputs - first 1, and then 5 for getGradeTotal()*/





/*now another read*/

scanf("%d",&sal);

return 0;

}
some guy named mike
2012-11-28 12:54:42 UTC
in getGradeTotal() you should probably have

int ttl = 0;



you never know if ttl already has something in it.
?
2016-12-13 15:26:16 UTC
ok, in binary, a million = on, 0 = off. as a result, we use "a million" to declare a functionality as on, and "0" as off.. i'm constructive you recognize this. So, if a value replaced into set to "0" it may be off and valueless... till you needed, for some reason, to come back returned and edit it later (jointly with not making use of it in a BETA attempt)
?
2012-11-28 14:48:47 UTC
You didn't initialize ttl. And delete "rept" from those definitions if you don't need that. A bad name. You can get it mistaken with repp. (if u wanna use it).





Thanks!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...