LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-17-2012, 05:07 PM   #1
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Rep: Reputation: 0
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?
 
Old 03-17-2012, 05:18 PM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
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
 
Old 03-17-2012, 05:27 PM   #3
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
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.
 
Old 03-17-2012, 05:51 PM   #4
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
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:
 
Old 03-17-2012, 06:09 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by UnixCube View Post
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.
Old 03-17-2012, 06:09 PM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by UnixCube View Post
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.
Old 03-17-2012, 06:31 PM   #7
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
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.
 
Old 03-17-2012, 06:31 PM   #8
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,627

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
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)
 
Old 03-17-2012, 06:38 PM   #9
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
Hey John, I got the program to work! Thanks for your help. : )
 
Old 03-17-2012, 06:40 PM   #10
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
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.
 
Old 03-17-2012, 07:54 PM   #11
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
Old 03-17-2012, 07:59 PM   #12
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
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.
 
Old 03-17-2012, 09:09 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,787

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
Quote:
Originally Posted by UnixCube View Post
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.
Old 03-17-2012, 09:24 PM   #14
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
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.
 
Old 03-17-2012, 10:21 PM   #15
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
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.
Old 03-18-2012, 01:09 AM   #16
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0

thank you fukawi1, I just made the changes to my account to enable reputation points to be displayed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Nagios Office 365 Check hoggy_vegas Linux - General 1 12-02-2011 06:32 AM
New User and Linux High Performance computing environment venki_nyn LinuxQuestions.org Member Intro 3 02-26-2009 08:27 AM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
LXer: Quantum computing billions of times faster than conventional computing LXer Syndicated Linux News 0 11-24-2006 02:21 AM
Installing Linux on an old IBM 365-series Thinkpad. Milky Moo Cow Mandriva 10 08-27-2004 05:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:46 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