Solved

scanf problem

Posted on 2004-09-13
7
513 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 50 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

717 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