Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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. */
#include<stdio.h>
int main(void)
{
double AA, BB; /* Input values */
/* Getting the first number */
printf("Please enter the first number: ");
scanf("%lf", &AA);
/* Getting the second number */
printf("Please enter the second number: ");
scanf("%lf", &BB);
/* Calculate the SUM, PRODUCT, and DIFFERENC. */
SUM = AA + BB;
PRO = AA * BB;
DIF = AA - BB;
/* Display the SUM, PRODUCT, and DIFFERENC. */
printf("The SUM of %f and %f is: %f\n\n", AA, BB, SUM);
printf("The PRODUCT of %f and %f is: %f\n\n", AA, BB, PRO);
printf("The DIFFERENC of %f and %f is: %f\n\n", AA, BB, DIF);
sleep(10);
return(0);
}
When I go to compile with gcc -o i get the following errors:
Code:
ssma-imac:ENG-3211 ssma$ gcc -o hw_1_4 hw_1_4.c
hw_1_4.c: In function ‘main’:
hw_1_4.c:20: error: ‘SUM’ undeclared (first use in this function)
hw_1_4.c:20: error: (Each undeclared identifier is reported only once
hw_1_4.c:20: error: for each function it appears in.)
hw_1_4.c:21: error: ‘PRO’ undeclared (first use in this function)
hw_1_4.c:22: error: ‘DIF’ undeclared (first use in this function)
From the example we did in class today I thought I had things in the correct order. I set the variables AA and BB as double floating. I am scanf for both of them to come from the user. Then I set the mathematic formulas for sum, product, and difference.
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. */
#include<stdio.h>
#define KM 1.609
int main(void)
{
double MILES, KMS;
/* Get the distance in miles. */
printf("Enter the distance in miles> ");
scanf("%lf", &MILES);
/* Convert the distance */
KMS = KM * MILES;
/* Display the results */
printf("That equals %f kilometers.\n", KMS)'
return(0);
}
Looking at that I have things in basically the same order as the example.
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. */
#include<stdio.h>
int main(void)
{
double AA, BB; /* Input values */
/* Getting the first number */
printf("Please enter the first number: ");
scanf("%lf", &AA);
/* Getting the second number */
printf("Please enter the second number: ");
scanf("%lf", &BB);
/* Calculate the SUM, PRODUCT, and DIFFERENC. */
//SUM = AA + BB;
//PRO = AA * BB;
//DIF = AA - BB;
/* Display the SUM, PRODUCT, and DIFFERENC. */
//printf("The sum of %f plus %f is: %f\n\n", AA, BB, SUM);
//printf("The product of %f multiplied %f is: %f\n\n", AA, BB, PRO);
//printf("The difference of %f minus %f is: %f\n\n", AA, BB, DIF);
/* attempt to remove the variables and replace them with direct math in the printf line */
printf("The sum of %f plus %f is: %f\n\n", AA, BB, AA + BB);
printf("The product of %f multiplied %f is: %f\n\n", AA, BB, AA * BB);
printf("The difference of %f minus %f is: %f\n\n", AA, BB, AA - BB);
sleep(10);
return(0);
}
Thanks in advance for the explanation as to what i did wrong the first time.
hopefully the following will help point you in the right direction.
Quote:
When I go to compile with gcc -o i get the following errors:
The compiler is telling you that it does not know what SUM, PRO and DIF are. Do you notice that you don't get the same errors for AA and BB? What is the difference in what you have done with AA and BB, compared to the variables that the compiler is complaining about (hint: can be fixed by adding to the first line in your main()).
hopefully the following will help point you in the right direction.
The compiler is telling you that it does not know what SUM, PRO and DIF are. Do you notice that you don't get the same errors for AA and BB? What is the difference in what you have done with AA and BB, compared to the variables that the compiler is complaining about (hint: can be fixed by adding to the first line in your main()).
Cheers,
Evo2.
ahh duh so I should have
Code:
double AA, BB, SUM, PRO, DIF;
is that what you are saying? if so that is so blonde of me not to get. thanks.
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.
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?
That is a #define which is different (you'll probably learn about these preprocessor directives at some point) and is not what you would want to do in this case.
Quote:
edit to say Thank you again. That was such a simple fix. Also thank you for not doing the homework for me.
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).
printf("The sum of %f plus %f is: %f\n\n", AA, BB, AA + BB);
printf("The product of %f multiplied %f is: %f\n\n", AA, BB, AA * BB);
printf("The difference of %f minus %f is: %f\n\n", AA, BB, AA - BB);
just makes it a bit more clear IMHO when reporting the results.
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);
printf("The product of %.2f multiplied by %.2f is: %.2f\n\n", AA, BB, AA * BB);
printf("The difference of %.2f minus %.2f is: %.2f\n\n", AA, BB, AA - BB);
with the following output of the program:
Code:
$ ./hw_1_4
Please enter the first number: 33
Please enter the second number: 45
The sum of 33.00 plus 45.00 is: 78.00
The product of 33.00 multiplied by 45.00 is: 1485.00
The difference of 33.00 minus 45.00 is: -12.00
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.