Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
03-17-2012, 05:07 PM
|
#1
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Rep:
|
age_in_days value not computing when I try to multiply user input by 365
I am trying to create a program that will prompt the user to input their age in years. Then I want to take this value and multiply it by 365 in order to get the age of the user in days and print this result onto the screen. Every time that I run my program, it prints the age of the user in years correctly. However, whenever I try to print the users age in days, I get an odd answer like -107407844 for instance. I think that this may have something to do with my scanf() function and maybe instead of multiplying the user input by 365, it is multiplying the address of the user input by 365. Can someone please help me?
|
|
|
03-17-2012, 05:18 PM
|
#2
|
Member
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854
Rep: 
|
Firstly, taking the age in years, as an input, wont give you an accurate calculation, you will need to take the date of birth, and calculate from there.
Secondly, you should read (and then reread) http://www.linuxquestions.org/linux/...Ask_a_Question.
You have provided no information for anybody to be able to help you. You haven't provided the code that is causing the problem, and you haven't even specified what programming language you are using.
Thirdly, I would suggest you read (and reread) this then try posting again.
Fourthly, a google search shows an example script in bash.
http://www.unix.com/unix-dummies-que...n-2-dates.html
And one in C
http://www.askdavetaylor.com/date_ma...ll_script.html
|
|
|
03-17-2012, 05:27 PM
|
#3
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Original Poster
Rep:
|
Hello fukawi1 here is the code that I have, I am not sure how I can insert the code any better than this.
I tried to store the user input inside of the userinput address as an integer. Then whatever the user inputs, I wanted to
multiply this value by 365 in order to get the number in years and change that to an even larger but still equivalent value
in days. I am also using the C language.
Code:
#include <stdio.h>
int main()
{
int year, userinput, age_in_days, age_in_years; // type declaration
userinput = age_in_years;
year = 365; // assignment
age_in_days = userinput * year;
printf("please input your age in years below:\n");
scanf("%d", &userinput);
printf(" I am %d years old\n", userinput);
printf(" I am %d days old\n", age_in_days);
return 0;
}
Last edited by UnixCube; 03-17-2012 at 09:26 PM.
|
|
|
03-17-2012, 05:51 PM
|
#4
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Original Poster
Rep:
|
Here is the output of the program
I'd really appreciate if someone could please help, i'm not sure why the output is that -107408924 number instead of 3 * 365.
please input your age in years below:
3
I am 3 years old
-1074084924
jkc0144@csp04:
|
|
|
03-17-2012, 06:09 PM
|
#5
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by UnixCube
age_in_days = userinput * year;
...
printf("please input your age in years below:\n");
scanf("%d", &userinput);
...
printf(" I am %d days old\n", age_in_days);
|
Are you aware that steps in a computer program happen in sequence?
Variables in a computer program are very different from variables in algebra.
When you compute age_in_days = userinput * year that does NOT establish an ongoing relationship among variables as it would in algebra. It computes the value of userinput*year at that moment (before the value of userinput is initialized). The value of userinput at that moment is garbage, so the value of age_in_days becomes garbage. When userinput changes LATER that has no effect on the garbage in age_in_days.
Last edited by johnsfine; 03-17-2012 at 06:14 PM.
|
|
1 members found this post helpful.
|
03-17-2012, 06:09 PM
|
#6
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by UnixCube
age_in_days = userinput * year;
...
printf("please input your age in years below:\n");
scanf("%d", &userinput);
...
printf(" I am %d days old\n", age_in_days);
|
Are you aware that steps in a computer program happen in sequence?
Variables in a computer program are very different from variables in algebra.
When you compute age_in_days = userinput * year that does NOT establish an ongoing relationship among variables as it would in algebra. It computes the value of userinput*year at that moment (before the value of userinput is initialized). The value of userinput at that moment is garbage, so the value of age_in_days becomes garbage. When userinput changes LATER that has no effect on the garbage in age_in_days.
Last edited by johnsfine; 03-17-2012 at 06:14 PM.
|
|
1 members found this post helpful.
|
03-17-2012, 06:31 PM
|
#7
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Original Poster
Rep:
|
Thanks johnsfine, I now see the error of my ways, I thought that I could initialize all of the variables at the beginning of the function main, now I see that I have to create my assignments in a particular order in order for the compiler to read it in a certain sequence. I got the program to work the way that I wanted it to!
Thank You again guys.
|
|
|
03-17-2012, 06:31 PM
|
#8
|
LQ Muse
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,690
|
then calculate the Leap years and add the appropriate days for the fourth year ( the 1 Feb 29 every 4 years)
while not including the year 2000 ( not a leap year)
|
|
|
03-17-2012, 06:38 PM
|
#9
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Original Poster
Rep:
|
Hey John, I got the program to work! Thanks for your help. : )
|
|
|
03-17-2012, 06:40 PM
|
#10
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Original Poster
Rep:
|
Hey guys, here is my corrected code, and the correct output that it produced:
Code:
#include <stdio.h>
int main()
{
int k, year, userinput, age_in_days, age_in_years; // type declaration
userinput = age_in_years; // assignment
printf("please input your age in years below:\n");
scanf("%d", &userinput);
age_in_days = userinput * 365; // assignment
printf(" I am %d years old\n", userinput);
printf(" I am %d days old\n", age_in_days );
return 0;
}
output:
please input your age in years below:
25
I am 25 years old
I am 9125 days old
Last edited by UnixCube; 03-17-2012 at 09:25 PM.
|
|
|
03-17-2012, 07:54 PM
|
#11
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Your variable age_in_years and your assignment userinput = age_in_years; accomplish nothing.
Whatever you thought that was doing, that isn't doing it and it isn't necessary.
|
|
|
03-17-2012, 07:59 PM
|
#12
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Original Poster
Rep:
|
Hey johnsfine, you are absolutely right, I actually wound up deleting that line entirely. I am going to post the code that I have finally so that everyone can see it. The userinput = age in years is actually not doing anything for, or against the program. So it still compiles just fine with or without it.
Code:
#include <stdio.h>
int main()
{
int k, year, userinput, age_in_days, ; // type declaration
printf("please input your age in years below:\n");
scanf("%d", &userinput);
age_in_days = userinput * 365; // assignment
printf(" I am %d years old\n", userinput);
printf(" I am %d days old\n", age_in_days );
return 0;
}
Last edited by UnixCube; 03-17-2012 at 09:22 PM.
|
|
|
03-17-2012, 09:09 PM
|
#13
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,820
|
Quote:
Originally Posted by UnixCube
I am not sure how I can insert the code any better than this.
|
Wrap your code in [code][/code] tags.
|
|
1 members found this post helpful.
|
03-17-2012, 09:24 PM
|
#14
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Original Poster
Rep:
|
thank you ntubski, I was wondering how that worked. Also, by any chance does anyone know why my reputation is disabled? I'm new to the forum, and I'd like to get some of those really neat green squares going.
|
|
|
03-17-2012, 10:21 PM
|
#15
|
Member
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854
Rep: 
|
If you go to "My LQ" top of the page, RHS
Then "Edit Options", on the LHS.
You can enable displaying your reputation there.
|
|
1 members found this post helpful.
|
03-18-2012, 01:09 AM
|
#16
|
Member
Registered: Feb 2012
Location: Texas
Posts: 58
Original Poster
Rep:
|
thank you fukawi1, I just made the changes to my account to enable reputation points to be displayed.
|
|
|
All times are GMT -5. The time now is 10:41 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|