LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-17-2013, 08:40 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 05-17-2013, 08:54 PM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 05-17-2013, 09:00 PM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 05-17-2013, 09:00 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
1 members found this post helpful.
Old 05-17-2013, 09:28 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by evo2 View Post
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.

Last edited by lleb; 05-17-2013 at 09:32 PM.
 
Old 05-17-2013, 09:45 PM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by lleb View Post
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.
 
1 members found this post helpful.
Old 05-17-2013, 11:08 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
thanks again. looking forward to learning more.
 
Old 05-17-2013, 11:22 PM   #8
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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?

Last edited by lleb; 05-17-2013 at 11:37 PM.
 
Old 05-17-2013, 11:48 PM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
Old 05-17-2013, 11:56 PM   #10
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
ok so there are functions in C. good that is a head start.
 
Old 05-19-2013, 11:27 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by lleb View Post
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 );
 
1 members found this post helpful.
Old 05-19-2013, 12:54 PM   #12
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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

Last edited by lleb; 05-19-2013 at 01:01 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with a basic perl script kdelover Programming 10 09-14-2009 04:31 PM
Need help with a basic script rsmccain Programming 5 07-03-2006 06:28 AM
Some basic script q's viniosity Programming 11 02-02-2005 05:57 PM
basic script help required jimmorrison Linux - General 5 10-26-2004 11:31 PM
basic shell script help lin00b Linux - Newbie 2 10-08-2004 11:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration