Thread: simple program 2

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    28

    simple program 2

    ok below is a sample program I used to
    multiple 2 numbers the numbers can be
    more than 1 digit,. I know its long winded
    and the functions are not needed but I was
    just trying to get to grips with different
    things as im used to delphi. the program
    has one problem when it asks if you want
    to try again the program ends and does not
    wait for input, im guessing there is still
    something in the input buffer. Can you
    have a look and tell me how this could be
    fixed and if theres anyhting else I should
    have done different? code exmaples would be
    great.

    Thanks.

    Code:
     
    #include <stdio.h>
    
    int  intAnswr(int,int);
    char chrRpt (int);
    
    int main ()
    
    {
       int intN1,intN2;
       int intFst;
    
       intN1 = 0;
       intN2 = 0;
       intFst = 0;
    
       while (chrRpt(intFst) == 'Y')
       {
          intFst = 1;
          printf ("\nEnter First No.  : ");
          scanf ("%d",&intN1);
          printf ("Enter Second No. : ");
          scanf ("%d",&intN2);
          printf ("\n%d * %d = %d\n",intN1,intN2,intAnswr(intN1,intN2));
       }
    }
    
    
    char chrRpt(int intRun)
    {
    char chrTmp='Y';
       if (intRun != 0)
       {
          printf("Try Again? Y/N");
          scanf ("%c",&chrTmp);
       }
       return (chrTmp);   
    }
    
    
    int intAnswr(int n1,int n2)
    {
       return(n1 * n2);
    }
    Last edited by Gav D; 08-05-2003 at 03:32 AM.

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    use:
    Code:
    fgetc(stdin,mychar);
    instead of scanf

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    13
    Do like this: U will understand the problem.

    Enter first no: Enter any thing here (let 2)
    Enter second no: Y /* Enter 'Y' Here*/

    2 * 0 = 0

    Try Again? Y/N
    Enter First No. :


    understood...scanf doent clear the buffer.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    the first 2 scanfs work fine if I enter 4 then 5 it comes out with 4*5 it just doesnt work on the try again one? can you show me in my code what I need to change? other than the scanfs does everything else look ok ? Im trying not to get into any bad habbits thats why im checking this with you lot.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    why use fgetc(stdin, varname) instead of getchar()?

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    i dunno, just because, no particular reason. I just like it. I mean how cool is that 'f' in front. You can post it on a C++ board and the guys who don't know C would be like, "oh, whats that?" LOL

    j/k

    or you could just use fflush(stdin) (freaky dissonant piano music plays and lightning strikes) Just kidding, DONT DO THIS.

    use whatever u want, I just like fgetc()

    PS: See C_Refrence_Card in my Signature

    -LC
    Last edited by Lynux-Penguin; 08-05-2003 at 01:08 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    ok so other than that the concepts look ok? also intFST was initialised just a bit further down

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    also sorry to sound stupid but can some explain this

    while ( getchar() != '\n' );

    is this clearing out the buffer? im guess getchar is taking the contents ofthe input buffer one by one until it reaches the end?

    also as its while with a semi colon at the end does that mean the next statement is not included in the loop?
    Last edited by Gav D; 08-06-2003 at 01:15 AM.

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, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM