Challenge issue


#1

so im stumped. I’m coming over from limited knowledge of python and trying to learn objective-c. for whatever reason, on the challenge, i cannot print the values of the sum, what is displayed is SUM instead of the value. can someone please post their successful code to this challenge so i can visually see what i did wrong. here is my sample code. I’m sure its something so small and i feel ridiculous that i cannot get the result as i could do this so simply in python. thanks in advance.

#include <stdio.h>

int main (int argc, const char * argv[])
{

float pie;
pie = 3.14;

float money;
money = 42.00;

double sum;
sum = pie + money;

printf("sum");




return 0;

}


#2

here is what I have for the printf statement

printf(" %f. \n", sum);

sending “sum” will give you literally what you have in the quotes. the %f will assign/print the value of sum which is a , and the sum-- , sum)


#3

ok thank you for the reply. i kind of see whats happening but it still seems odd to me on how you have to get the value. I’m not 100% yet on understanding why you did and why it worked, but its a good starting point. thank you.


#4

I did it this way.

int main (int argc, const char * argv[])
{
    float num1 = 15.2;
    float num2 = 13.8;
    double sumNum = num1 + num2;
    printf("The sum of %f and %f equals %f", num1, num2, sumNum);
    return 0;
}

When you put values in double quotes, the printf statement will print whatever is within quotes. The exception is the use of ‘escape’ characters (the % sign in this case) which is telling the compiler that you have a float value (%f). Outside of the quotes, you have your variables which correspond positionally to the %f values within the quotes. If I had done ‘num1, sumNum, num2’ then the numbers would have printed wrong - “The sum of 15.2 and 29.0 equals 13.8”. I’m positive that this will be addressed further in the book with a much better explanation.


#5

Thanks PHBeagle. I was wondering for an exact definition of what %f actually meant. After reading your reply, I was successfully able to complete the challenge.

And now, on to chapter 4!


#6

#include <stdio.h>

int main (int argc, const char * argv[])
{
//Declare the first variable

float firstNum;

//Assign the value to the variable

firstNum = 3.14;

//Declare and assign the second variable

float secondNum = 42.0;

// declare the double variable called Total

double total;

//The sum of the firstNum and the secondNum using the '+'

total = firstNum + secondNum;

//log it to the user

printf("The sum of %f and %f is %f \n.", firstNum, secondNum, total);

// exist the program sucessfully

return 0;

}


#7

Looks fine, Alexander. Best of luck as you proceed.

My only suggestion is cosmetic related to the forum: it’s helpful to wrap your code with the proper phpBB BBCode so that we can read it more easily.

If you place your code inside [ code ] blocks (without the extra space around the word “code”, that would be helpful in the future.

For example:

[ code ]
#include <stdio.h>

int main(int argc, const char * argv[])
{
return 0;
}
[ /code ]

When I remove the extra spaces inside the brackets, it looks like this:

#include <stdio.h>

int main(int argc, const char * argv[])
{
    return 0;
}

#8

thanks for your help and thanks for the hint


#9

Thank YOU all of you. I really appreciate your help. :smiley:


#10

So far so good, thanks everyone. :sunglasses: