need some help with while loop

This is a discussion on need some help with while loop within the C Programming forums, part of the General Programming Boards category; i got this program i gotta write that reads in characters one at a time and print them out and ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    9

    need some help with while loop

    i got this program i gotta write that reads in characters one at a time and print them out and i have to use the while loop. i tried all sorts of things but its not working. i think its the criteria im setting to get it matched is the problem. can any one help me. the code is below. by the way i hve to print out the characters in hex,octal,and decimal..
    also it has to be simple code like only using the while loop and not arrays. im currently working on this if anyone can help me il appreciate it,
    Code:
    #include <stdio.h>
          9 int main()
         10 {
         11 char ch;
         12 fprintf(stdout,"Enter a character: ");
         13 while (fscanf(stdin,"%c", &ch)==1);
         14 {
         15 fprintf(stdout,"Character     Hex     Octal   Decimal \n");
         16 fprintf(stdout,"%c           %5x     %5o      %5d  \n",ch, ch, ch, ch);
         17 }
         18   return 0;
         19   }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    > while (fscanf(stdin,"%c", &ch)==1);

    See that ";" over there at the end of the line? Take it out.

  3. #3
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    to get you started, how about getting rid of the semi-colon at the end of this?
    13 while (fscanf(stdin,"%c", &ch)==1);
    the semicolon causes nothing to be executed inside of the while loop

    instead, do something like
    Code:
        while( foo < bar){
            ...do some important stuff...
        }
        ... do more important stuff...
    straight off the heap

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    i got it after like 2 hours thanx for the suggestion i overlooked that too.lol heres what i got


    Code:
    #include <stdio.h>
    int main()
    {
      char ch;
      int count;
      count=0;
    fprintf(stdout,"character     hex     octal     decimal\n");
      
    while(fscanf(stdin,"%s", &ch)==1)
         {count++;
    
    fprintf(stdout,"%5c         %3x       %3o         %3d       \n",ch,ch,ch,ch);
          }
    fprintf(stdout,"You've read %d characters\n",count); 
      return 0;
      }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    fprintf(stdout,"You've read %d characters\n",count);
    ->
    Code:
    printf("You've read %d characters\n",count);
    Code:
    char ch;
    while(fscanf(stdin,"%s", &ch)==1)
    ch isn't a string! Use %c and scanf, or getchar():
    Code:
    while((ch = getchar()) != EOF)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And while you're at it, change ch to an int if you're going to be testing it for EOF.


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

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Just testing you, Quzah.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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