Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

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?
0
isolated_island
Asked:
isolated_island
  • 3
  • 2
  • 2
  • +3
1 Solution
 
weiccoCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now