Solved

problem in reading  character

Posted on 2002-07-11
10
211 Views
Last Modified: 2010-04-15
I think this should be a very simple question to all experts.Following is the part of my programme which give me problem:

     for (i=0;i<10;i++)
     {    
          printf("Enter a character:");
          scanf("%c", &c[i]);

          if(isupper(c[i]))
               upcount++;
          else if (islower(c[i]))
               lowcount++;
     }

I want the programme to read 10 character but once i press enter,it will consider "enter" as a character.How can i solve this problem?
0
Comment
Question by:isolated_island
  • 3
  • 2
  • 2
  • +3
10 Comments
 

Accepted Solution

by:
weicco earned 50 total points
Comment Utility
for (i=0;i<10;)
    {    
         printf("Enter a character:");
         scanf("%c", &c[i]);

         if (c[i] != '\n')
         {
             if(isupper(c[i]))
                  upcount++;
             else if (islower(c[i]))
                  lowcount++;
             i++;
         }
         fflush(stdin); // Just in case
    }
0
 

Author Comment

by:isolated_island
Comment Utility
I do not understand this line of code -->fflush(stdin);  
because i have not reach that level yet.Can weicco pls explain roughly to me what it mean?i know stdin mean standard input devices.In this case it mean the keyboard,am i right?but i really have no idea what fflush mean.thx.
0
 

Author Comment

by:isolated_island
Comment Utility
I do not understand this line of code -->fflush(stdin);  
because i have not reach that level yet.Can weicco pls explain roughly to me what it mean?i know stdin mean standard input devices.In this case it mean the keyboard,am i right?but i really have no idea what fflush mean.thx.
0
 

Author Comment

by:isolated_island
Comment Utility
I do not understand this line of code -->fflush(stdin);  
because i have not reach that level yet.Can weicco pls explain roughly to me what it mean?i know stdin mean standard input devices.In this case it mean the keyboard,am i right?but i really have no idea what fflush mean.thx.
0
 

Expert Comment

by:weicco
Comment Utility
fflush flushes everything that is left in stream, in this case stdin. stdin is standard input which points to keyboard by default.

When you read character with scanf from stdin, it returns you that character, but return-character is left in stdin. This means that when you next time call scanf, scanf thinks that there is data incoming (that return-character) and returns you that. But if you flush that return-character away from stdin, scanf should work properly.

I'm lousy to explain things...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:marcjb
Comment Utility
The line:
fflush (stdin);
should be removed from the program.  fflush has no defined effect on input streams in Standard C.  As Plauger says in his book, "You can't reliably discard input before a prompt, as you can under UNIX."  Basically, the behaviour of:
fflush(stdin);
is undefined.
0
 
LVL 6

Expert Comment

by:ebosscher
Comment Utility
wow, this is taking me back to my college days. C 101.  hmm... what i might suggest is a change in the code.

you're reprinting the prompt every time the user enters a character, do you want to do that?

what about something like this:

char cIn;
int iLoop, iUpper, iLower;

printf("Enter ten characters: ");
for (iLoop=0; iLoop < 10; ++iLoop)
{    
   cIn = getchar()

   if(cIn != '\n')
   {
      if(isupper(cIn))
         ++iUpper;
      else
         ++iLower;
   }
   else
   {
      putchar(7); /* make some noise */
      putchar(8); /* get rid of the backspace */
      --iLoop;    /* get the character again */
   }
}


I think that should work.

It's a little different than the solution you proposed...
0
 
LVL 6

Expert Comment

by:ebosscher
Comment Utility
ah dang, i missed some ;'s
0
 
LVL 1

Expert Comment

by:laax
Comment Utility
Change your scanf() like this
   scanf("%c\n", &c[i]);

Laax.
0
 

Expert Comment

by:vidya_s
Comment Utility
try using c[i] = getchar(); instead of scanf stmt!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now