473,394 Members | 1,866 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,394 software developers and data experts.

How to control passes without stopping at scanf?

bajajv
152 100+
Hi,

This is my code :

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. const char arr1[] = {'+', '-', '*', '/', '>', '<'};
  6.  
  7. bool isOperator(char ch)
  8. {
  9.     bool res = false;
  10.  
  11.     if (ch == arr1[0])
  12.         res = true;
  13.     else if (ch == arr1[1])
  14.         res = true;
  15.     else if (ch == arr1[2])
  16.         res = true;
  17.     else if (ch == arr1[3])
  18.         res = true;
  19.     else if (ch == arr1[4])
  20.         res = true;
  21.     else if (ch == arr1[5])
  22.         res = true;
  23.  
  24.     return res;
  25. }
  26.  
  27. int main()
  28. {
  29.     char ch;
  30.     do {
  31.         //ch = '\0';
  32.         printf("Enter char.\n");
  33.         scanf("%c", &ch);
  34.         printf("%s\n", (isOperator(ch) ? "true" : "false"));
  35.     }while (ch != 'q');
  36.     return 0;
  37. }
  38.  
When I run this code, it asks for char value, prints output, but next time it runs thru whole code again with some junk value in ch, without stopping at scanf().
I tried to put NULL in ch, but that didn't changed the flow.

Can you please check what is going wrong?
Feb 4 '11 #1

✓ answered by horace1

when using scanf() you enter the character followed by '\n' (<RETURN> character) to terminate the line. The scanf() reads the character but '\n' is left in the input stream. when you call scanf() the next time it reads the '\n' as the next charcater.
You need to remove the '\n' from the input stream and you can do this with a getchar(), e.g.
Expand|Select|Wrap|Line Numbers
  1.     do {
  2.         //ch = '\0';
  3.         printf("Enter char.\n");
  4.         scanf("%c", &ch);
  5.         printf("%s\n", (isOperator(ch) ? "true" : "false"));
  6.         getchar();    // remove '\n'
  7.     }while (ch != 'q');
  8.  

1 1805
horace1
1,510 Expert 1GB
when using scanf() you enter the character followed by '\n' (<RETURN> character) to terminate the line. The scanf() reads the character but '\n' is left in the input stream. when you call scanf() the next time it reads the '\n' as the next charcater.
You need to remove the '\n' from the input stream and you can do this with a getchar(), e.g.
Expand|Select|Wrap|Line Numbers
  1.     do {
  2.         //ch = '\0';
  3.         printf("Enter char.\n");
  4.         scanf("%c", &ch);
  5.         printf("%s\n", (isOperator(ch) ? "true" : "false"));
  6.         getchar();    // remove '\n'
  7.     }while (ch != 'q');
  8.  
Feb 4 '11 #2

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

Similar topics

11
by: Larry Woods | last post by:
I have a "homegrown" dll that I am using in IIS (VBScript). I need to replace it on a production server, but I don't want to stop/restart server in order to replace the dll. Possible? TIA,...
1
by: Wavemaker | last post by:
I was wondering if there is a way to change the value of a control without triggering an event. The ComboBox control, for example, will trigger the SelectedIndexChanged event when its SelectedIndex...
10
by: Girish | last post by:
Ok, Ive been thinking about this problem for a while. I have 30 odd aspx pages (already built) on my website that I need to have some validation occur before the page should load. The validation...
1
by: Mike | last post by:
I have a bunch of validators set up to run on the client. They are all updating as expected on the client side. The summary is only updated when I click a button. Is it possible to get the...
2
by: Samuel R. Neff | last post by:
I'm trying to find a good way to handle Control.InvokeRequired without duplicating four lines of code in every function/event. Typically what I've seen in books is this: If InvokeRequired Then...
3
by: JamesB | last post by:
Hello I have a form which contains a Listview control that is filled with data as the program runs. This all works fine, but I want to also then do a certain process on this data at the same...
1
by: rajib4 | last post by:
How to install and apply fix pack without stopping the DB2 INSTANCE or DAS? I am working on a 24x7 environment, so cannot stop the instance or das. Please help
1
by: shajeela | last post by:
1.I create a datagridview control 2.connected using a oledb adapter 3.problem is if i run the program the old value is displayed 4.for viewing the old value i have to close the current window,and...
4
by: delorian0815 | last post by:
Hi, I programmed a windows forms application. it contains several pictureBoxes, each showing the first picture in a folder. When the mouse is over the pictureBox and you turn your mousewheel, it...
15
by: jbrumbau | last post by:
Hello, Right now I'm having some performance issues (forms taking 2 or so seconds to check) when trying to check for all control labels in a loop. Currently the code I have is this: Function...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.