473,748 Members | 9,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Menu printed twice in calc prog

Hi

I am writing a small calculator program using switch() for a menu. The
menu is presented at first, selection read in via scanf(), and
calculation is executed. At the end of the operation, the menu is
presented again. When it is presented again however, it is printed
twice. I've been through the code and cannot spot any obvious errors,
but hey ... more eyes are better than one. I don't particularly want to
revamp my code too much nor use sophisticated hacks since I am a student
relatively new to C and programming and a lot of that advanced stuff
would be way over my head :)

I have copied the code for reference. Does it have something with not
flushing the buffers from the first stdin read by scanf()? Suggestions
please:

----------code begin--------------------

/*************** *************** *************** *************** ****
* *
* Program: Calk-U-l8r.c *
* *
* Written by: xxxxxxxxxxxx *
* *
* 30/10/03; <version number> *
* *
*************** *************** *************** *************** *****/
#include <stdio.h>

/* Menu-driven Calculator program: */
main()

{
float i, z, tmp;
char x;

tmp = 0;

puts("\n%%%%%%% %%%%%%%%%%%%%%% %%%%%%%\n");
puts("\n Calk-U-l8r\n");
puts("\n%%%%%%% %%%%%%%%%%%%%%% %%%%%%%\n");

puts("\nPlease select an operation. Enter 'q' to quit:"); /* menu */
printf("\nAdd: %10c\n", 'a');
printf("Divide: %7c\n", 'd');
printf("Multipl y: %5c\n", 'm');
printf("Subtrac t: %5c\n", 's');

scanf("%c", &x);

while ( x != 'q'){
switch(x){
case 'a': /* add function */
puts("\nAdditio n\n");
printf("\nEnter digit:>");
scanf("%f", &i);
tmp += i;
printf("\nEnter next digit:>");
scanf("%f", &i);
tmp += i;

printf("\nThe answer is: %.2f", tmp);
break;

case 'd': /* divide function */
puts("\nDivisio n\n");
puts("\nEnter a digit:");
scanf("%f", &i);
puts("\nEnter the dividend:");
scanf("%f", &z);

printf("\nThe answer is: %.2f", i / z);
break;

case 'm': /* multiplication */
puts("\nMultipl ication\n");
printf("\nEnter a digit:>");
scanf("%f", &i);
printf("\nEnter a second digit:>");
scanf("%f", &z);

printf("\nThe answer is: %.2f", i * z);
break;

case 's': /* subtraction */
puts("\nSubtrac tion\n");
puts("\nEnter a digit:");
scanf("%f", &i);
puts("\nEnter second digit:");
scanf("%f", &z);

printf("\nThe answer is: %.2f", i - z);
break;

}
puts("\nPlease select an operation. Enter 'q' to quit:");
printf("\nAdd: %10c\n", 'a');
printf("Divide: %7c\n", 'd');
printf("Multipl y: %5c\n", 'm');
printf("Subtrac t: %5c\n", 's');

scanf("%c", &x);

}

puts("\n\nThank-you for using Calk-U-l8r!\n");

return 0;

}
----------code end------------------------

Thanks

Andy

Nov 13 '05 #1
5 2401
Andy wrote:

Hi

I am writing a small calculator program using switch() for a menu. The
menu is presented at first, selection read in via scanf(), and
calculation is executed. At the end of the operation, the menu is
presented again. When it is presented again however, it is printed
twice. I've been through the code and cannot spot any obvious errors,
but hey ... more eyes are better than one. I don't particularly want to
revamp my code too much nor use sophisticated hacks since I am a student
relatively new to C and programming and a lot of that advanced stuff
would be way over my head :)

I have copied the code for reference. Does it have something with not
flushing the buffers from the first stdin read by scanf()? Suggestions
please:
[code snipped; see up-thread]


Your suspicion is close to the mark, and your problem
is pretty close to that of Question 12.18 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... but perhaps just enough different to warrant extra
explanation. Here's a synopsis of what's happening:

- You print the menu, and then call scanf() to get
the user's choice.

- scanf() converts one character, and you add or
subtract or whatever in response.

- You print the menu again, and call scanf() to get
the user's choice.

- scanf() again converts one character -- but what
character is that, exactly? Why, it's the newline
that ended the user's first line of input. This
'\n' character doesn't match anything in your big
switch() statement, so you take no action on it.

- After having done nothing, you print the menu
and call scanf() a third time.

- This time, scanf() consumes the character after
the newline, namely, the user's actual response,
and you process it.

- ... and then you print the menu, consume and ignore
the newline, print the menu again, consume and act
upon the user's choice, and so on.

See Questions 12.17 through 12.20 for some ideas of
what you might do differently. My preference is not to use
scanf() at all for interactive input; when data comes from
an "uncontroll ed" source, I prefer to read an entire line
at a time with fgets() or something similar, and then pick
the line apart without further I/O. fgets() plus sscanf()
makes a pretty good pairing, combining the apparent ease of
plain scanf() with a more predictable I/O behavior.

--
Er*********@sun .com
Nov 13 '05 #2
Eric Sosman wrote:


Your suspicion is close to the mark, and your problem
is pretty close to that of Question 12.18 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... but perhaps just enough different to warrant extra
explanation. Here's a synopsis of what's happening:

- You print the menu, and then call scanf() to get
the user's choice.

- scanf() converts one character, and you add or
subtract or whatever in response.

- You print the menu again, and call scanf() to get
the user's choice.

- scanf() again converts one character -- but what
character is that, exactly? Why, it's the newline
that ended the user's first line of input. This
'\n' character doesn't match anything in your big
switch() statement, so you take no action on it.

- After having done nothing, you print the menu
and call scanf() a third time.

- This time, scanf() consumes the character after
the newline, namely, the user's actual response,
and you process it.

- ... and then you print the menu, consume and ignore
the newline, print the menu again, consume and act
upon the user's choice, and so on.

See Questions 12.17 through 12.20 for some ideas of
what you might do differently. My preference is not to use
scanf() at all for interactive input; when data comes from
an "uncontroll ed" source, I prefer to read an entire line
at a time with fgets() or something similar, and then pick
the line apart without further I/O. fgets() plus sscanf()
makes a pretty good pairing, combining the apparent ease of
plain scanf() with a more predictable I/O behavior.

Eric
Thanks for your response - that was nicely explained and useful. I would
have googled for the solution but to be honest didn't really know what
to put in the search string over and above any generality (like scanf())
that would have generated x number of hits. I will look at the FAQ you
mention and see what I can come up with. I've never used fgets() before,
so that will be a useful technique to examine.

Much obliged.

- Andy

Nov 13 '05 #3

"Andy" <An******@home. com> wrote in message news:3f******@2 12.67.96.135...
Eric Sosman wrote:


Your suspicion is close to the mark, and your problem
is pretty close to that of Question 12.18 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... but perhaps just enough different to warrant extra
explanation. Here's a synopsis of what's happening:

- You print the menu, and then call scanf() to get
the user's choice.

- scanf() converts one character, and you add or
subtract or whatever in response.

- You print the menu again, and call scanf() to get
the user's choice.

- scanf() again converts one character -- but what
character is that, exactly? Why, it's the newline
that ended the user's first line of input. This
'\n' character doesn't match anything in your big
switch() statement, so you take no action on it.

- After having done nothing, you print the menu
and call scanf() a third time.

- This time, scanf() consumes the character after
the newline, namely, the user's actual response,
and you process it.

- ... and then you print the menu, consume and ignore
the newline, print the menu again, consume and act
upon the user's choice, and so on.

See Questions 12.17 through 12.20 for some ideas of
what you might do differently. My preference is not to use
scanf() at all for interactive input; when data comes from
an "uncontroll ed" source, I prefer to read an entire line
at a time with fgets() or something similar, and then pick
the line apart without further I/O. fgets() plus sscanf()
makes a pretty good pairing, combining the apparent ease of
plain scanf() with a more predictable I/O behavior.

Eric
Thanks for your response - that was nicely explained and useful. I would
have googled for the solution but to be honest didn't really know what
to put in the search string over and above any generality (like scanf())
that would have generated x number of hits. I will look at the FAQ you
mention and see what I can come up with. I've never used fgets() before,
so that will be a useful technique to examine.

Much obliged.

- Andy


You could use a 'default' case in the switch to get rid of printing the menu
twice.

something like this....

default:
scanf("%c", &x);
continue;

Thanks,
Praveen Kumar
Nov 13 '05 #4
On Fri, 07 Nov 2003 14:58:52 +0530, sahukar praveen wrote:
You could use a 'default' case in the switch to get rid of printing the menu
twice.

something like this....

default:
scanf("%c", &x);
continue;


What if the user types 'h' 'e' 'l' 'l' 'o' to the program?
How many menus would he see then?

It takes more than your suggestion to solve the real problem.

-Sheldon

Nov 13 '05 #5
On Fri, 07 Nov 2003 14:58:52 +0530, sahukar praveen wrote:
You could use a 'default' case in the switch to get rid of printing the menu
twice.

something like this....

default:
scanf("%c", &x);
continue;


Let me respond again, since the first response was bogus, although the
basic idea I was trying to get across is the same as what I want to say
now.

The problem with this suggestion is that it is designed to eliminate
the symptom without actually fixing the problem.

If you add this code to the sample program given, the extra menu isn't
printed, but the behavior of the program is still not what one would
expect.

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4685
by: Rob McLennan - ZETLAND | last post by:
Hi, I have set up an external stylesheet, named "print.css", to format the style of all pages printed from my company's website. I've been previewing my changes to the stylesheet by doing File\Print Preview in IE6 and noticed that occasionally the leftmost button of the javascript menu (CoolMenus Version 3.02) on our website is printed. (I have wrapped the javascript that generates this menu in a DIV so that it is concealed from...
1
1963
by: Veerle | last post by:
Hi, I have a menu at my page. This menu is at the top of my page and at the bottom of my page. The html for this menu is the same for the top menu and for the bottom menu, I only gave them a different id, so that could position them absolutely in my css: <div id="menu-top"> <a class="menuitem" href="index.html">menuitem 1</a> <a class="menuitem" href="page2.html">menuitem 2</a>
8
3250
by: Rob McLennan - ZETLAND | last post by:
Hi, I have set up an external stylesheet, named "print.css", to format the style of all pages printed from my company's website. I've been previewing my changes to the stylesheet by doing File\Print Preview in IE6 and noticed that occasionally the leftmost button of the javascript menu (CoolMenus Version 3.02) on our website is printed. (I have wrapped the javascript that generates this menu in a DIV so that it is concealed from...
1
1762
by: Mosher | last post by:
Hi all, I am looking for some event handler auto-calc help on a form that takes user input through text fields and dropdown menus. I would like some of the text fields to be auto populated when dropdown menu selections are made. Here is an abstract html/javascript form: <form> Enter how much money you are willing to spend:<input type=text name=amount size=10> Enter your home city: <select name=city>
1
1270
by: Frank J. Lagattuta | last post by:
I have a tray icon application that displays a context menu when it is right clicked. One of the menu items on this context menu is a submenu. The text of the menu items in this submenu get updated from time to time, however, when the submenu is popped up the old/original text always shows. Selecting one of these menu items copies its text to the clipboard. When I do a paste, the new updated text is pasted proving that the menu item...
2
4614
by: Michael Moreno | last post by:
Hello, In a Base Form I have a toolbar and have implemented the Click event as: protected virtual void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { if (e.Button == tbClose) Close();
3
2370
by: Kristijan Marin | last post by:
Hi, I need to print a report like document. So in report there are many articles and what I need when I get to the end, is to update page numbers on the second page where the Table of Content is .... or even update strings like Page 1 or 10 ... Page 2 of 10 etc .... When starting the report I don't know the total number of pages nor the page number on which each and every article starts ....
8
3153
by: adz1809 | last post by:
Here is the HTML - <div id="nav"> <ul class="level1"> <li class="submenu"><a href="../orderflow/control.aspx">Order Control</a></li> <ul class="level2"> <li><a href="#">Summary</a></li> </ul> <li class="submenu"><a href="#">Product Control</a></li>
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.