help with a basic C script
First off this is homework. I am not asking you to complete my homework for me, I am asking for a direction as to what I have done wrong.
Code:
/* 4. Write a program that asks the user to enter two numbers and prints their sum, product, and difference. */ Code:
ssma-imac:ENG-3211 ssma$ gcc -o hw_1_4 hw_1_4.c reading the error it seems i have messed up when setting the formulas. I have a basic understanding of BASH scripting so I first goofed and set both the lines for scanf as $AA/$BB instead of &AA/&BB. I fixed that as soon as I saw the error and realized what I had done, but im kind of stumped with my formulas... here is the example we did in class: Code:
/* Converts distance in miles to kilometers. */ Thanks for the pointers. |
an other question. still hitting google looking for examples. i found something interesting on the printf line for a result. can you do this:
Code:
printf("The sum of %f and %f is: \n", AA + BB); |
now the question is why the first set of code failed, but this one worked:
Code:
/* 4. Write a program that asks the user to enter two numbers and prints their sum, product, and difference. */ |
Hi,
hopefully the following will help point you in the right direction. Quote:
Cheers, Evo2. |
Quote:
Code:
double AA, BB, SUM, PRO, DIF; ahh i also see now that in the example from class KM was pre-defined early before the main(void) thus it didnt need to be in the double later on. is that correct? edit to say Thank you again. That was such a simple fix. Also thank you for not doing the homework for me. |
Hi,
Quote:
Quote:
Quote:
Evo2. |
thanks again. looking forward to learning more.
|
next question. way out of the scope of my homework. just an idea id like to try in my code for all of the scripts i had to write for homework.
instead of sleep at the end with return(0), what if i wanted to do a standard if then like in BASH. id like to ask the question do you have more numbers to try type thing. if yes then start over if anything but [Yy] then exit. now i am asking for some code, or at least a link to know what to even google for. thanks. found some info, but no clue howto get it to go back and start over. in BASH id just call the function again. how do i do that in C? |
Hi,
sure you can put your code in a function and then call that function in a loop. I think you may find it worthwhile to work through some online tutorials (I don't have any specific suggestions though). Cheers, Evo2. |
ok so there are functions in C. good that is a head start.
|
Quote:
I'm not a C person, but wouldn't AA + BB sum first, meaning there's only one %f value for printf to use? I imagine you want something more like this: Code:
printf( "The sum of %f and %f is: %f\n", AA , BB , AA + BB ); |
oh, yes that is correct. thank you. great catch.
edit to note* turns out that is exactly what i have done: Code:
printf("The sum of %f plus %f is: %f\n\n", AA, BB, AA + BB); after reading an other book i found that if i do %.2f I can reduce the number of .00000 after the decimal. so now those three lines look like: Code:
printf("\nThe sum of %.2f plus %.2f is: %.2f\n\n", AA, BB, AA + BB); Code:
$ ./hw_1_4 |
All times are GMT -5. The time now is 01:30 PM. |