Solved

problem in reading  character

Posted on 2002-07-11
10
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

635 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