Problems with code

This is a discussion on Problems with code within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> using namespace std; int main() { int year_of_birth; cout<<"Please enter the year you were born below, and It ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    5

    Problems with code

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        
        int year_of_birth;
        
        cout<<"Please enter the year you were born below, and It will tell you how much older or younger you are from me\n";
        cin>> year_of_birth;
        cin.ignore();
        if ( year_of_birth < 1980 ) {
             cout<<" You are older than me by"<<( year_of_birth - 1980 )<<"year(s)!\n";
             }
             
             else ( year_of_birth > 1980 ) {
                  cout<<" You are younger than me by"<<( 1980 / year_of_birth )"year(s)!\n";
                  }
                  cin.get();
                  }
    I'm very much a begginer in C++ programming, and 2 days ago started following the tutorial. Did the examples and decided I was going to try making my own simple program. The Program is designed to figure out how many years apart your year of birth Is from my date of birth and output this data. The problem Is I don't know much yet about programming as I stated earlier.

    I was wondering If anyone could tell me what is wronge with it.
    Also I'm aware there are some major problems with it just by looking at them, I just don't know what they are.

    Thank You in Advance

  2. #2
    ------------
    Join Date
    Jun 2005
    Posts
    79
    it doesnt need to be:
    Code:
             cout<<" You are older than me by"<<( year_of_birth - 1980 )<<"year(s)!\n";
    because you dont have to put the variable in "(" and ")"... just make it:
    Code:
             cout<<" You are older than me by"<< year_of_birth - 1980 <<"year(s)!\n";
    and i think that will make it work, im working on something right now so i cant check ATM... BTW im new to c++ myself (3rd or fourth day... forget which one...)
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    paranthesis dont matter, you could do this

    cout << ((((((((((8-((((((((((3)))))))))))))))))))) << endl;

    and it would work fine.

  4. #4
    ------------
    Join Date
    Jun 2005
    Posts
    79
    hhmmm... well, thats why i put that im new too
    oh well... i tried
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        
        int year_of_birth;
        
        cout<<"Please enter the year you were born below, and It will tell you how much older or younger you are from me\n";
        cin>> year_of_birth;
        cin.ignore();
        if ( year_of_birth < 1980 ) {
           cout<<" You are older than me by "<<( 1980 - year_of_birth )<<"year(s)!\n";  //bad logic here, gives negative the other way
           }
        else  { //dont need yearbirth > 1980 unless you were doing an ifelse statement
           cout<<" You are younger than me by "<<( year_of_birth -1980 ) << "year(s)!\n"; //forgot an extra "<<" and bad logic again
           }
        cin.get();
        return 0; 
    }
    Last edited by JoshR; 06-23-2005 at 06:21 PM.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        
        int year_of_birth;
        
        cout<<"Please enter the year you were born below, and It will tell you how much older or younger you are from me\n";
        cin>> year_of_birth;
        cin.ignore();
        if ( year_of_birth < 1980 ) {
             cout<<" You are older than me by"<<( 1980 - year_of_birth )<<"year(s)!\n";
             }
             
             else if( year_of_birth > 1980 ) {
                  cout<<" You are younger than me by"<<( year_of_birth - 1980 )<<"year(s)!\n";
                  }
             else
                cout<<" Same age!!!"<<endl;
                  cin.get();
                  }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    It's still saying there is an error, Is there anything else I can do to make it work.

    thanx for the help

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    nevermind the prev. post works

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    wow we all posted at the same time beat u to it

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    lol, by the way do any of you know how to keep the window open for more than one entry?

    It will be greatly appreciated

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    if you want more than one, put it in a loop. you could use a while statement and break out of it if a sentinel value is entered or you could use a for loop and loop it as many times as you want

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    thank you all, you have been most helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with compiling code in cygwin
    By firyace in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2007, 08:16 AM
  2. Problems with GetDiskFreeSpaceEx/my code...
    By scrappy in forum Windows Programming
    Replies: 1
    Last Post: 07-30-2003, 11:16 AM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 01:02 AM
  5. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21