#Simple calculator project errors

71 messages ยท Page 1 of 1 (latest)

runic spade
#

I made a simple C programming calculator project including areas and volumes, but there are errors.
I have attatched my code and the instructions I had.

#include <stdio.h>

int main() {

    char name[100];

    printf("Hello! I am a computer, haha\n\n");

    printf("Enter your name:\n");

    scanf("%s", name);

//Calculate geometric formulas for using user Input

    const float PI = 3.14159265;

    float Area;
    float Volume;
    int Radius;
    int Height;

    printf("\n Hello Aaron! We will calculate some geometric formulas!\n");

//Area and volume of a sphere

    printf("\n Sphere: \n\n");

    printf("Enter the radius of the sphere: \n");

    scanf("%d", Radius);

    Area = Radius * Radius * PI
    Volume = 4*PI*Radius*Radius/3

    printf("/nArea is ==> {Area}");
    printf("/nVolume is ==> {Volume}");


}
long foxBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

tawny herald
#

well your formulas are wrong

#

for both of the area and volume

runic spade
#

oh

#

oh wait yea

knotty light
#

printf("\n Hello Aaron! We will calculate some geometric formulas!\n");
You're supposed to use "name" here btw

runic spade
#
#include <stdio.h>

int main() {

    char name[100];

    printf("Hello! I am a computer, haha\n\n");

    printf("Enter your name:\n");

    scanf("%s", name);

//Calculate geometric formulas for using user Input

    const float PI = 3.14159265;

    float Area;
    float Volume;
    int Radius;
    int Height;

    printf("\n Hello Aaron! We will calculate some geometric formulas!\n");

//Area and volume of a sphere

    printf("\n Sphere: \n\n");

    printf("Enter the radius of the sphere: \n");

    scanf("%d", Radius);

    Area = Radius * PI * Radius * Radius;
    Volume = (4 * PI * Radius * Radius * Radius) / 3;

    printf("/nArea is ==> {Area}");
    printf("/nVolume is ==> {Volume}");
}

#

this is the code i have so far

#
IOVP.c:30:17: error: format specifies type 'int *' but the argument has type 'int' [-Werror,-Wformat]
    scanf("%d", Radius);
           ~~   ^~~~~~
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
make: *** [<builtin>: IOVP] Error 1
$ 
#

and this is the error i get

tawny herald
#

did your classes cover usage of scanf?

runic spade
#

yes

tawny herald
#

so you know that scanf needs the address of the variable it's supposed to put the value in right?

runic spade
#

yes

#

right

tawny herald
#

anything wrong when comparing that to what you're doing? ๐Ÿ˜›

runic spade
#

hmm

#

umm

#

what is wrong

tawny herald
#

you're not giving an address to scanf

runic spade
#

well it's supposed to take user input right?

tawny herald
#

it is, but it needs an address to fill the variable that you want, and you're not giving it an address

runic spade
#

what do you mean by an address

#

i think i know, but i just don't know like the word address

tawny herald
#

the address of the variable it's supposed to fill with the user input

#

like the memory address of that variable

#

have you learnt about pointers and/or memory in general yet?

runic spade
#

umm, i don't think so

#

but i did learn scanf

#

wait do u mean by

#

scanning user input

tawny herald
#

well that explains it then

runic spade
#

then storing it into "radius"

tawny herald
#

when you do scanf("%d", Radius);, you call scanf and give it a string and the value of Radius

#

scanf can do nothing meaningful with the actual value, it needs to write to Radius, that's what you want it to do

runic spade
#

right

tawny herald
#

so instead of giving scanf the value of Radius, you give its memory address: scanf("%d", &Radius);

runic spade
#

oh right

#

u use the &

tawny herald
#

with that scanf is able to write the user input at the correct place in memory, and that place in memory happens to be what you call variable Radius in your main funtion

#

& yields the memory address of whatever comes next, provided it has one

runic spade
#

ahh ok

#

is that the only problem you see so far with my code?

#

wait there is a problem

#

cus when I do name

#

it shows up as Hello name! We will calculate some geometric formulas!

tawny herald
#

as pointed out, you don't print the name that you queried before, you just print "Aaron" every time

runic spade
#

when i want it to take user input and store it in naem

#

printf("\n Hello name! We will calculate some geometric formulas!\n");

#

how do i change this code

#

printf("\n Hello %s! We will calculate some geometric formulas!\n", name);

#

is that it?

tawny herald
#

sounds about right

#

didn't your classes cover this as well? thonk

runic spade
#

yes, it did

#

forgot lol

#

wait how do I add like

#

a line of space

#

printf("/nArea is ==> %f", Area);
printf("/nVolume is ==> %f", Volume);
}

#

so for this

#

/nArea is ==> 25.132742/nVolume is ==> 33.510323$

#

it shows up like that

tawny herald
#

because /n is not the newline character

#

/n is two characters, namely / and n

runic spade
#

oh

tawny herald
#

you used the newline character properly before ๐Ÿ˜›

runic spade
#

printf("Area is ==> %f\n", Area);
printf("Volume is ==> %f\n", Volume);

#

i think this does it?

tawny herald
#

go ahead and try it ๐Ÿ‘

runic spade
#

it worked