Solved

scanf problem

Posted on 2004-09-13
7
502 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 83
How to organize data in excel ? 2 114
XCode crashes while uploading to app store. 5 262
Autosar OS Multicore Share Resources confusion ? 2 39
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

777 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