Thread: why is this not working

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    why is this not working

    hi i have a simple program i cant exit the program
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    main()
    {
    int x;
    printf("press 3 to exit"):
    scanf("%i",x):
    while(x!=3) // i want to exit from the loop if its 3 isthis right
    {
    
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    scanf("%i",&x):
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    This program will work . But if you give any non-numeric characters then the loop will run unterminatly.For that you have to check the input before processing that .

    Code:
     
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    main()
    {
    int x;
    printf("press 3 to exit");
    scanf("%i",&x);
    while(x!=3) // i want to exit from the loop if its 3 isthis right
    {
    scanf("%i",&x);
    }
    }
    Learn the basic first . C Programming Tutorial .

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Both fail in terms of error checking.
    You should be checking the return value of scanf(). As it stands if scanf() fails the default uninitialised value of x will be used for the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM