LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help with a basic C script (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-a-basic-c-script-4175462419/)

lleb 05-17-2013 08:40 PM

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. */

#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.

Thanks for the pointers.

lleb 05-17-2013 08:54 PM

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);
that would eliminate the need for me to set SUM, PRO, and DIF that are currently causing my issue.

lleb 05-17-2013 09:00 PM

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.

evo2 05-17-2013 09:00 PM

Hi,

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()).

Cheers,

Evo2.

lleb 05-17-2013 09:28 PM

Quote:

Originally Posted by evo2 (Post 4953441)
Hi,

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.

evo2 05-17-2013 09:45 PM

Hi,
Quote:

Originally Posted by lleb (Post 4953458)
ahh duh so I should have

Code:

double AA, BB, SUM, PRO, DIF;
is that what you are saying?

Bingo!
Quote:

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.
No problem. Good luck with the course.

Evo2.

lleb 05-17-2013 11:08 PM

thanks again. looking forward to learning more.

lleb 05-17-2013 11:22 PM

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?

evo2 05-17-2013 11:48 PM

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.

lleb 05-17-2013 11:56 PM

ok so there are functions in C. good that is a head start.

David the H. 05-19-2013 11:27 AM

Quote:

Originally Posted by lleb (Post 4953437)
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);
that would eliminate the need for me to set SUM, PRO, and DIF that are currently causing my issue.


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 );

lleb 05-19-2013 12:54 PM

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);
        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



All times are GMT -5. The time now is 01:30 PM.