simple C program

This is a discussion on simple C program within the C Programming forums, part of the General Programming Boards category; Hi, I am a newbie, and i am trying to write my first few simple programs in C. In the ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    8

    simple C program

    Hi,

    I am a newbie, and i am trying to write my first few simple programs in C. In the following example i am trying to write a small program which gives me the factorial of a particular number, however, when i hit the "x" key, i would like to terminate the program.

    This is the code which i tried:

    Code:
    /*Program to find the factorial of 6 */
    
    #include <stdio.h>
    
    int i,j;
    int value;
    
    int main()
    {
    
    
          char keypress;
          do
          {
           printf("Enter a value to which you want the factorial: ");
           scanf("%d", &value);
           j=1;
           for (i=1; i<=value; i++)
           j=j*i;
           printf("The factorial of %d is %d\n", value, j);
          }
          while (keypress != "x");
          return 0;
          system("PAUSE");
    }
    When i run the program, it works fine, however, as soon as i hit the "x" key the program will run into an endless loop giving me the factorial number of the last number which i checked for.

    Can someone tell me what i do have wrong please ?

    Many thanks in advance.

  2. #2
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    U have't accepted value for "keypress"
    Add statement scanf("%s",&keypress); after last printf(); then it should work

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    thx for your reply, i have added the line as you suggested, however, when i hit the x key i am still not getting the program to terminate, but instead when i hit the enter key, i will get the printf statement of the last inputted number ?

    Any ideas what i am doing wrong pls ?

    Thanks

  4. #4
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    Are u hitting enter key after pressing x???

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Post your new code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    well when i added the line you suggested me, after i type a number and get the factorial, i am not getting the printf statement to input another factorial, so whatever key i press (including x) nothing happens, so i have to hit enter, and then i get the printf statement to input another number.

    Thanks

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So I guess you just felt ignoring the "post your new code" comment would somehow help you get your answer, eh?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    Hi,

    this is the new amended code :

    Code:
    /*Program to find the factorial of 6 */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int i,j;
    int value;
    
    int main()
    {
    
    
          char keypress;
          while (keypress != "x")
          {
           printf("Enter a value to which you want the factorial: ");
           scanf("%d", &value);
           j=1;
           for (i=1; i<=value; i++)
           j=j*i;
           printf("The factorial of %d is %d\n", value, j);
           scanf("%s", &keypress);
          }
          return 0;
          system("PAUSE");
    }
    Kindly note that i tried putting the while statement at the beginning, but still to the same result

    Thanks

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You should initialize keypress to something.

    Use double quotes for a string, single quotes for a char.
    Code:
    while (keypress != 'x')
    your system("PAUSE") line will never execute since it's after return 0;
    (There are better alternatives to system("PAUSE")... see FAQ)

    Use %c to read a char (%s is for char arrays)

    When you make these changes you will still have to deal with the newline character being left over from your scanf calls
    Last edited by spydoor; 04-24-2006 at 08:15 AM.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    8
    spydoor, thank you very much for your reply. the %s was the culprit because as soon as i've changed it to %c, the program ran as expected.

    I really appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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