functions don't work!

This is a discussion on functions don't work! within the C++ Programming forums, part of the General Programming Boards category; i'm a super newbie at C++ and i'm following the tutorials provided at cpogramming.com . i use the compiler Dev-C++ ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    functions don't work!

    i'm a super newbie at C++ and i'm following the tutorials provided at cpogramming.com . i use the compiler Dev-C++ Beta version, and when i run these codes:

    #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 type any no. in there and hit enter. the window just immediately exits. why?

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    The window closes because the program has finished executing, and there is nothing to pause the program.

    You can;
    A) Use the search button on this board to find an answer that suits your needs

    B) Open a DOS window first then run the program from within it, or if you're executing from insdie a DOS compiler, look for a 'view output' option

    C) Add something like 'getchar();' to the end of the program, before the 'return 0;' statement. Then the program will wait for you to hit enter.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    59
    or just write

    system ("PAUSE");

    just before return 0;

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    i tried

    system(pause)

    and

    getchar()

    but when i compiled, it says that:

    implict declaration of function 'int system' or 'int getchar()'

    (something like that)


  5. #5
    Shadow12345
    Guest
    the pause needs to be in quotes
    system("pause");
    If that still doesn't work try typing
    #inlclude <windows>
    or
    #include <windows.h>
    at the top of your program

    if THAT doesn't work try typing
    #include <conio.h>
    or
    #include <conio.c>
    or
    #include <conio>
    at the top of your program (I don't know if conio is supported by Dev-C++)

    and then if that doesn't work go bite someone really REALLY hard!

    azuth as if you are telling him to go search, i mean yes searching is a good thing but christ he's got 2 posts and already ur makin life difficult for him, if you don't want to tell him the answer to this simple problem that we've ALL had then don't, but don't tell him to go find all the answers on his own. Wait until he has 15 posts before you tell him that
    Last edited by Shadow12345; 10-10-2002 at 05:18 PM.

  6. #6
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    system is in stdlib.h so do this
    Code:
    #include <stdlib.h>
    
    //code
    system("PAUSE");

  7. #7
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Wrong side of bed this morning Shadow? You don't like the idea of starting as you mean to go on, and discouraging bad habbits early?

    People with far more experience both in programming and on this board have often left a reply at 'Read the faq'. I thought that all 3 options I offered were constructive and helpful. Being helpful was why I posted.

    It is however nice to see people that have been around for a long time discouraging the upper end of the beginner spectrum (namely me) from offering any help at all, cheers for that!

    Demonographic rhinology is not the only possible outcome, but why take the chance

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    3
    Azuth...calm down...
    Anyways, i got it!
    thx to everybody~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 02:54 PM
  2. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  3. Trying to do basic math functions. Why doesn't this work?
    By crazychile in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:14 PM
  4. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM

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