473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

curious scanf problem

I've got the following function

void remDuplicate(FINDFILE filey, int sim, char *filePath)
{
char ans;
numFound++;

printf("Found file %s that is %d%% the same as %s. Delete (Y/N)??\n", filePath, sim, filey.filename);
scanf("%c", &ans);

ans = tolower(ans);
if (ans == 'y')
{
printf("Removing file %s\n", filePath);
unlink(filePath);
numRemoved++;
}
printf("Continuing.\n");
return;
}

which gets called each time two files are found that are dupilcates. Yet scanf only takes input each second time (1st, 3rd, 5th calls etc..) Any thoughts ehy??
Nov 25 '06 #1
3 1390
horace1
1,510 Expert 1GB
I've got the following function

void remDuplicate(FINDFILE filey, int sim, char *filePath)
{
char ans;
numFound++;

printf("Found file %s that is %d%% the same as %s. Delete (Y/N)??\n", filePath, sim, filey.filename);
scanf("%c", &ans);

ans = tolower(ans);
if (ans == 'y')
{
printf("Removing file %s\n", filePath);
unlink(filePath);
numRemoved++;
}
printf("Continuing.\n");
return;
}

which gets called each time two files are found that are dupilcates. Yet scanf only takes input each second time (1st, 3rd, 5th calls etc..) Any thoughts ehy??
scanf() with the conversion specification "%c" returns the next character in the input stream.

so when you enter Y followed by the <enter> key the first scanf() returns Y the next scanf() returns the newline character (put in the stream by the <enter> key).
The thing to do is to put a space before the %c to skip whitespace in the input, e.g.
Expand|Select|Wrap|Line Numbers
  1.     scanf(" %c", &ans);
  2.  
Nov 25 '06 #2
Thanks. Worked a treat!
Nov 26 '06 #3
well the alterntive for that can be soemthing like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7.     int ch;
  8.  
  9.     printf("Please enter [Y/N]\n?");
  10.     ch = getchar();
  11.  
  12.     if('Y' == toupper(ch))
  13.         printf("You eneter \"YES\"");
  14.     else
  15.         printf("You eneter \"NO\"");
  16.  
  17.     getchar();
  18.     return 0;
  19. }
  20.  
  21. /* my output
  22. Please enter [Y/N]
  23. ?Y
  24. You eneter "YES"
  25.  
  26. Please enter [Y/N]
  27. ?n
  28. You eneter "NO"
  29. */        
  30.  
ssharish
Nov 26 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Intaek LIM | last post by:
Hello. I always used cin in C++ for standard input. But, with C, I must use scanf. Integers, chars are all fine with scanf. The problem is... I cannot read floating point value like 'double'...
39
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please...
12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
5
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
3
by: CoreyWhite | last post by:
I wrote a program that shows some incredibly curious probabilities. It is a simple game where you guess a number between 0 and 2 that the computer thinks up. What is origonal about this game is...
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
68
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread...
13
by: FerrisUML | last post by:
Hello everyone! I new to C and am having the following problem. In the below program, the last scanf is being ignored and the program exits. Can anyone see anything that im doing wrong? Thanks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.