• C

problem in reading character

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?
isolated_islandAsked:
Who is Participating?
 
weiccoConnect With a Mentor Commented:
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
 
isolated_islandAuthor Commented:
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
 
isolated_islandAuthor Commented:
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
Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  

 
isolated_islandAuthor Commented:
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
 
weiccoCommented:
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
 
marcjbCommented:
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
 
ebosscherCommented:
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
 
ebosscherCommented:
ah dang, i missed some ;'s
0
 
laaxCommented:
Change your scanf() like this
   scanf("%c\n", &c[i]);

Laax.
0
 
vidya_sCommented:
try using c[i] = getchar(); instead of scanf stmt!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.