why is this not working

This is a discussion on why is this not working within the C Programming forums, part of the General Programming Boards category; 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 ...

  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
    Katy, Texas
    Posts
    2,309
    Code:
    scanf("%i",&x):
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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, 04: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, 08: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, 05:08 AM

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