Thread: New Programmer needs help! Very Easy Question!

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    1

    New Programmer needs help! Very Easy Question!

    I am a beginner programmer and tried to write a code to calculate whether you are an official adult or not. Whenever i type in my age and press return, it goes on to a new line and makes me type more before i can continue. What is wrong? Here is my code...

    Code:
    #include <stdio.h>
    
    int age;
    int const senior = 61;
    int const adult = 18;
    int main ()
    {
        printf ("Input Age\n");
        scanf ("%d\n", &age);
        if (age >= adult && age<= senior)
        printf ("you are a legal adult\n");
        else
        printf ("you are not an adult\n");
        system ("PAUSE");
        return 0;
    }
    Notice how i had to use system pause because on my computer whenever i run the program it quits right after its finished calculating. Is there a simple way to insert system ("PAUSE") after all of my code automatically?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > scanf ("%d\n", &age);
    Remove the "\n"

    > Is there a simple way to insert system ("PAUSE") after all of my code automatically?
    If your IDE uses some kind of template file for creating your main(), then edit that to include the pause command automatically.

    The 'exit' problem only happens when you run the program from within the IDE. If you run the program from the command prompt, there is no need for an additional pause.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    I get

    getch()

    to create a 'pause'

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    You could make the program loop so it will never close, do this
    Code:
    #include <stdio.h>
    
    int age, x=1;
    int const senior = 61;
    int const adult = 18;
    int main ()
    {
       while(x==1)
      {   
         printf ("Input Age\n");
         scanf ("%d\n", &age);
         if (age >= adult && age<= senior)
         printf ("you are a legal adult\n");
         else
         printf ("you are not an adult\n");
         
       }
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  2. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  3. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  4. easy question about machine code
    By Jaguar in forum Tech Board
    Replies: 3
    Last Post: 10-07-2003, 09:11 AM
  5. I have a 2 easy question about some homework?
    By correlcj in forum C Programming
    Replies: 3
    Last Post: 07-18-2002, 07:28 PM