Solved

problem in reading  character

Posted on 2002-07-11
10
214 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
ID: 7147470
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
ID: 7147520
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
ID: 7147537
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:isolated_island
ID: 7147538
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
ID: 7148533
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
 
LVL 3

Expert Comment

by:marcjb
ID: 7148940
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
ID: 7149243
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
ID: 7149246
ah dang, i missed some ;'s
0
 
LVL 1

Expert Comment

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

Laax.
0
 

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Compile VxWorks Toronado project under Visual Studio 11 223
why "." vs "->" 23 120
Problem to scan all sheets 3 111
Secondary drive is failing... D Volume 11 17
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

830 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