Reputation: 21
I am in an intro coding class and I cannot figure out why this program isn't giving me the correct answer and is instead giving me a seemingly random number.
I have tried just putting it as a constant instead of a scanf and it still gives me issues
#include <stdio.h>
const int MIN_CONST = 7;
int ComputeMinutesLost(int userCigarettes) {
int minLost;
int MIN_CONST;
minLost = userCigarettes * MIN_CONST;
return minLost;
}
int main(void) {
int userCigarettes;
printf("How many cigarettes have you smoked?\n");
scanf("%d", &userCigarettes);
printf("You have lost %d minutes to cigarettes. ", ComputeMinutesLost);
return 0;
}
It should just say how many minutes are lost (cigarettes times 7) but it gives a seemingly random number.
Upvotes: 2
Views: 623
Reputation: 23208
Note: The code you posted probably should have flagged you with compile errors, or warnings. Do you have them turned on?
Change the following:
printf("You have lost %d minutes to cigarettes. ", ComputeMinutesLost);
To:
printf("You have lost %d minutes to cigarettes. ", ComputeMinutesLost(userCigarettes));
^------------^ // forgot to include argument
By the way, the numeric value you are seeing is an integer representation of the address of the function ComputeMinutesLost
.
Also, (thanks to @unimportant's comment)
in the following code section: //read the comments...
const int MIN_CONST = 7; // one of these...
int ComputeMinutesLost(int userCigarettes) {
int minLost;
int MIN_CONST; // is not necessary, and masks the other
// remove one or the other
// (as is, this one invokes undefined behavior.)
Upvotes: 3