Improve company productivity with a Business Account.Sign Up

x
?
Solved

scanf problem

Posted on 2004-09-13
7
Medium Priority
?
518 Views
Last Modified: 2010-04-15
I'm having trouble with a scanf portion of code.  The program asks for a single character A, B, or C.  If the user enters anything other than those three choices, the program is supposed to ask them to reenter the correct character.  I used an if statement to check the character entered but the segment of code isn't working right.  It prints out the code asking for the new character twice and then goes onto the next segment of code.  Here is the portion of code causing the trouble.

printf("Enter the character>");
      scanf("%c", &dcChar);
      
      dChar = toupper(dChar);
      
      if ((dChar != 'A') || (dChar != 'B') || (dChar != 'C'))
      {
            printf("Please enter in either an A, B, or C>");
            scanf("%c", &dChar);
      }
Could you tell me what the problem is please?
0
Comment
Question by:macabra
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:danielsonchris
ID: 12050980
//change the following line:
dChar = toupper(dChar);
//TO:
dChar = toupper(dcChar);

//Now remove the OR clauses in the following line:
 if ((dChar != 'A') || (dChar != 'B') || (dChar != 'C'))
//TO:
 if ((dChar != 'A') && (dChar != 'B') && (dChar != 'C'))

Peace,
Chris
0
 

Author Comment

by:macabra
ID: 12051014
Okay I did the change and the second line is gone but it doesn't wait for me to enter in a new dChar.  It goes right to the next line in the program.
0
 
LVL 11

Expert Comment

by:avizit
ID: 12051051
To let you enter a new dChar you have to code the whole thinginto a while loop

something like this logic

int done = 0;

while(!done) {
   print - enter a char
   if char satisfies  
       done = 1
  }



0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 

Author Comment

by:macabra
ID: 12051089
Okay the loop is working but now the double printf line is back...
0
 
LVL 11

Expert Comment

by:avizit
ID: 12051108
paste your current code here
0
 

Author Comment

by:macabra
ID: 12051165
while(done == 0)
{
     if ((dChar != 'A') && (dChar != 'B') && (dChar != 'C'))
     {
          printf("Please enter a A, B, or C>")
          scanf("%c", dChar)
     }
     else
     {
          done = 1;
     }
}
0
 
LVL 11

Accepted Solution

by:
avizit earned 200 total points
ID: 12051398
try

scanf(" %c", &dChar) ;

note there is a "space"  in front of the %c  and a & in fromt of dChar


from http://webcourse.cs.technion.ac.il/234117/Spring2004/ar/faq_Homework.html

I'm having a problem using scanf. After I enter the username, the following scanf reads the "enter" I clicked. What should I do?


Instead of writing scanf("%c") you can write scanf(" %c") (with a space before %c), and it should work better. The space before the "%c" will read all white spaces (newline, tab and space) and the first non-white space will be read into the "%c". Using the scanf this way will prevent the scanf from reading the "enter".
            
      

0

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

607 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