Very new to C++ or other programming, stuck on IF's

This is a discussion on Very new to C++ or other programming, stuck on IF's within the C++ Programming forums, part of the General Programming Boards category; I just started learning the C++ language. Right now I'm using the tutorial on this site and I got to ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Very new to C++ or other programming, stuck on IF's

    I just started learning the C++ language. Right now I'm using the tutorial on this site and I got to the IF statements. I read the FAQ's and all they had on my problem was that the compilers in C++ don't recognize the codes or something like that until you press ENTER. My problem is this; I set my program up like this:
    Code:
    #include <iostream.h>			
    int main()				//Most important part of the program!
    {
      int age;				//Need a variable...
      cout<<"Please input your age: ";	//Asks for age
      cin>>age;				//The input is put in age
      if(age<100)				//If the age is less than 100
      {
         cout<<"You are pretty young!";     //Just to show it works
      }
      else if(age==100)		//I use else just to show an example 
      {
         cout<<"You are old";		//Just to show you it works...
      }
      else
      {
        cout<<"You are really old";	//Executed if no other statement is executed
      }
      return 0;
    }
    When I press ENTER after putting in a number the window just closes and doesn't give me an answer. I am using BloodshedDev-C++ maybe I need a different one? Please help I just started doing this and this problem sort of stopped me.
    Too Fast Too Furious,
    too Fast For Y'all.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    read the FAQ...

    do the following:
    #include <cstdlib>

    and at the end of the program:
    system("pause");


    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    That is because your window is just closing after it displays your string. Your program won't pause on its own. You have to make it pause. Check out the tutorials on how to pause the screen and you should be good to go.

    Damn ya beat me to the punch
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    do you mean?

    do you mean the #include <cstdlib> instead of the #include <iostream.h>?
    Too Fast Too Furious,
    too Fast For Y'all.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    no, along with...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Wink Thank's a lot!

    Too Fast Too Furious,
    too Fast For Y'all.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Since you are new and using this site's tutorials, let me warn you of something. The tutorials are a little old. In general, I think they are still very helpful (I haven't used them personally, though). However, they use old header files. For example, in the tutorial (and your code) there is:

    #include <iostream.h>

    That is old. The new/more correct version is:

    #include <iostream>

    The new version creates one small complication, which is that everything in that header is in the namespace std. You don't really need to understand namespaces at first, you just have to know that they are there. There are several ways to deal with this small complication. The easiest way is to put:

    #include <iostream>
    #include <cstdlib>
    ... the rest of the #includes.
    using namespace std;


    The way I like to do it is to put std:: in front of the things I use from the header, like this:

    std::cout << "Please input your age: ";
    std::cin >> age;


    Either way, I would suggest getting into the habit of using the new headers, even if the tutorial uses the old ones.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Lightbulb On the same line? or below it?

    On the same line? or below it?
    Too Fast Too Furious,
    too Fast For Y'all.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Is that why?

    jlou, is that why sometimes it gives me errors and say's something about using different cin/ cout methods or something.?
    Too Fast Too Furious,
    too Fast For Y'all.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    I meanit say's...(more)

    'cout' undeclared
    'cin' undeclared
    Do I have to use those namespc std's?
    Too Fast Too Furious,
    too Fast For Y'all.

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    Wow..

    I just used the namespace std and it worked! should I still follow the tutorial or should I get an upto-date one?
    Too Fast Too Furious,
    too Fast For Y'all.

  12. #12
    NIM
    NIM is offline
    Registered User
    Join Date
    Oct 2003
    Posts
    24
    Nothing too fancy - the C++ headers don't require the .h at the end.

    Code:
    #include <iostream>
    
    using std::namespace;
    			
    int main()				//Most important part of the program!
    {
      int age;				//Need a variable...
      cout<<"Please input your age: ";	//Asks for age
      cin>>age;				//The input is put in age
      
    if(age<100)				//If the age is less than 100
         cout<<"You are pretty young!";     //Just to show it works
        
    else if(age==100)		//I use else just to show an example 
    cout<<"You are old";		//Just to show you it works...
      
    else
          cout<<"You are really old";	//Executed if no other statement is executed
      
      return 0;
    }

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088

    Re: Wow..

    Originally posted by ivanlucrazy
    I just used the namespace std and it worked! should I still follow the tutorial or should I get an upto-date one?
    Don't have an answer for that. I don't know any other good tutorials. You could buy a book, there are several good ones if you search this forum for suggestions. You can still use the tutorial if you just remain aware of the new headers. If you can adapt their examples to the new headers on your own, that is pretty good understanding and a good start.

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    13

    umm, Thank's guy's you helped a lot!

    umm, Thank's guy's you helped a lot!
    Too Fast Too Furious,
    too Fast For Y'all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

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