LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 09-13-2012, 08:14 AM   #1
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Rep: Reputation: Disabled
Program keeps giving me different answers


Whenever I type in 1994 and M it will give me one answer then I do it again and its different

#include <iostream>
using namespace std;
int main ()
{
int yr;
char gender;
int yrd;
yrd = ( 2012 ) - yr ;
cout << "Enter your year of birth:" << endl;
cin >> yr ;
cout << "Enter your Gender(M/F):" << endl;
cin >> gender ;
if ( ( gender == 'M') && yr <= 2012)
cout << "You will die in " <<(75 - yrd ) * (365 * 24 *60 *60) << "seconds" << endl;
else if (( gender == 'F') && yr <= 2012)
cout << "You will die in " << (80 - yrd ) * ( 365 * 24 * 60 *60) <<" seconds" << endl;

else
cout << "Your input was invalid you idiot. Run the program again!" << endl;


return 0;
}
 
Old 09-13-2012, 08:45 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Your problem is you are doing the yrd calculation before the user enters his input.

Code:
#include <iostream>
using namespace std;
int main ()
{
   int yr;
   char gender;
   int yrd;
   long die;
   int mf = 0;

   cout << "Enter your year of birth:" << endl;
   cin >> yr ;
   cout << "Enter your Gender(M/F):" << endl;
   cin >> gender ;

   yrd = ( 2012 ) - yr ;
   if ( ( gender == 'M') && yr <= 2012)
      mf = 75;
   else if (( gender == 'F') && yr <= 2012)
      mf = 80;
   else
      cout << "Your input was invalid you idiot. Run the program again!" << endl;
   die = (( mf - yrd ) * ( 365 * 24 * 60 * 60 ));
   cout << "You will die in " << die << "seconds" << endl;


return 0;
}

Last edited by crabboy; 09-13-2012 at 09:35 AM.
 
1 members found this post helpful.
Old 09-13-2012, 08:56 AM   #3
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Use [CODE][/CODE] tags around your code to make it more legible.

The problem is here:

Code:
//...
    int yr;
//...
    int yrd;
    yrd = ( 2012 ) - yr ; //'yr' has no value!
    cout << "Enter your year of birth:" << endl;
    cin >> yr ;
You're running the line to set yrd before you've assigned a value to yr, so you're going to get unpredictable behaviour! A good habit to get into is always assigning a value to variables before using them, and that way you will at least have a predictable (read: more easily found) bug.

EDIT: oops, beaten by crabboy... Sorry
 
1 members found this post helpful.
Old 09-13-2012, 10:03 AM   #4
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
it still comes out with random answers, where should I move it?
 
Old 09-13-2012, 10:14 AM   #5
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,046

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by carlosk711 View Post
it still
How are we supposed to guess what your program looks like now?

I assume you changed something based on the earlier answers you got. But I don't know what you changed.

Also, did you remember to recompile the program after changing the source code? Maybe you are still running the earlier version.
 
  


Reply

Tags
c++, program


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
Why is my program giving me segmentation fault? Ahmad Mujtaba Programming 5 12-25-2010 07:33 AM
[SOLVED] program to create a link list is giving an error jamesbon Programming 4 09-23-2010 07:12 AM
ethtool giving conflicting answers!! kamalsivadas Linux - Networking 1 04-23-2009 07:51 AM
I have written a program and it is giving me the following warning sandeep0424 Linux - Newbie 7 12-10-2008 08:26 AM


All times are GMT -5. The time now is 09:31 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration