Simple Program Help

This is a discussion on Simple Program Help within the C Programming forums, part of the General Programming Boards category; ok hopefully ive got these code tags right you lot seem fussy about them Im very new to c infact ...

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

    Simple Program Help

    ok hopefully ive got these code tags right you lot seem fussy about them Im very new to c infact only been looking at it the last few days Im used to delphi. can anyone tell me why the following program

    a) only runs through the for loop once
    b) how to clear the buffer from the last choice made. e.g te first time I ran this program I entered 10 and now no matter what I enter it remembers that 10.

    thanks in advance

    Code:
    #include <stdio.h>
    
    int main ()
    
    {
       int itst = 1;
       int cnt;
    
       for (cnt=0;cnt<5;cnt++);
          {
          printf ("Enter No. For Variable : ");
          while ((itst = getchar()) != '\n' && itst != EOF);
          printf ("%d is at %p  : ",itst,&itst);
          }
    }

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    thanks I have made that change and it now gives the following results...


    Enter No. For Variable : 3
    51 is at 6cffdc

    if I pass 4 into it it give result as 52 and so on. Any ideas?

    Taking the ; out after the For line has also made the loop run now.
    Last edited by Gav D; 07-30-2003 at 04:03 AM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    When you use:
    Code:
    itst = getchar();
    to read a character from the input, the computer stores the ascii code of the character in itst. So, when you type '3', the value stored in itst is the ascii value which in decimal notation is 51, '4' is 52, '\n' is 10, 'R' is 82 to quote some examples.
    Code:
    printf ("%d ",itst);
    will printf the value of itst in decimal notation. Which is exactly what your code is doing.

    To print the character use:
    Code:
    printf("%c ", itst)
    Hope that helps
    DavT
    -----------------------------------------------

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    OK I understand this now, but I now think all its storing in itst is \n or return as im now getting

    ' is at 6cffdc"

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Suggestions

    Hi Gav,
    My suggestions are here. I want to know really what you are doing. Is that while loop neccessary. Is EOF neccessary. EOF is for ctrl-z. I dont know why you used such things.Any way i modified your code.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main ()
    
    {
       int itst = 1;
       int cnt;
    
       for (cnt=0;cnt<5;cnt++)//comma removed.
          {
          printf ("Enter No. For Variable : ");
    	  //I got confused why you use while loop here.You are just getting a character and printing it and its 
    	  // address.
          while(((itst = getchar()) != '\n') && (itst != EOF));
          printf ("%d is at %p  : ",itst,&itst);
          }
    }
    Saravanan.T.S.
    Beginner.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    ok I rewrote the program and it now works can you just tell me if the following code is how you would have done it? You lot seem to not like the scanf function but getchar was only obviously taking the first character and if I entered 233 it only took the 2 whereas scanf seems to work fine.

    all its supposed to do is show the variable contents and pointer 5 times and take input for each one, its just to help me learn and serve no purpose.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main ()
    
    {
       int itst = 1;
       int cnt;
       for (cnt=0;cnt<5;cnt++)
          {
          printf ("Enter No. For Variable : ");
          scanf ("%d",&itst);
          printf ("%d is at %p  \n",itst,&itst);
          }
    }
    Last edited by Gav D; 07-30-2003 at 06:21 AM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how you would have done it?
    Well, using your code as a basis:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int itst, 
            cnt;
        for (cnt = 0; cnt < 5; cnt++)
        {
            printf("Enter No. For Variable : ");
            fflush(stdout);
            if (scanf("%d", &itst) == 1)
              printf("%d is at %p  \n", itst, (void*)&itst);
        }
        return 0;
    }
    But then I'd recommend you read this re getting numbers from the user.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Code:
    #include<stdio.h>
    #include<types.h>
    int main ()
    
    {
       int itst = 1;
       int cnt;
       char buf[11];
       for (cnt=0;cnt<5;cnt++)
          {
          printf ("Enter No. For Variable : ");
          fgets(buf, 10, stdin);
          itst = atoi(buf);
          printf ("%d is at %p  \n",itst,&itst);
          }
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    Code:
    #include<types.h>
    
       char buf[11];
    
          fgets(buf, 10, stdin);
          itst = atoi(buf);
    Why?

    atoi() is in 'stdlib.h', and 'fgets' is in 'stdio.h'. What's the purpose of 'types.h'?

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

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by WaltP
    Code:
    #include<stdio.h>
    #include<types.h>
    int main ()
    
    {
       int itst = 1;
       int cnt;
       char buf[11];
       for (cnt=0;cnt<5;cnt++)
          {
          printf ("Enter No. For Variable : ");
          fgets(buf, 10, stdin);
          itst = atoi(buf);
          printf ("%d is at %p  \n",itst,&itst);
          }
    }
    You've missed some points I implied in my code.

    fflush() is needed after printf to ensure the output is actually written when needed.

    The %p argument of printf requires a void* parameter.

    >>char buf[11];
    >>fgets(buf, 10, stdin);
    You're wasting 1 byte of memory, fgets() will write N-1 characters to the array, the Nth will be \0 if too much data is read.

    atoi() is not a particularly good choice of a replacement:
    From C99
    1 The functions atof, atoi, atol, and atoll need not affect the value of the integer
    expression errno on an error. If the value of the result cannot be represented, the
    behavior is undefined.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by quzah
    Why?

    atoi() is in 'stdlib.h', and 'fgets' is in 'stdio.h'. What's the purpose of 'types.h'?

    Quzah.
    I forgot
    So Solly
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

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