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

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("Multiply: %5c\n", 'm');
printf("Subtract: %5c\n", 's');

scanf("%c", &x);

while ( x != 'q'){
switch(x){
case 'a': /* add function */
puts("\nAddition\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("\nDivision\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("\nMultiplication\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("\nSubtraction\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("Multiply: %5c\n", 'm');
printf("Subtract: %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 2381
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 "uncontrolled" 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 "uncontrolled" 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******@212.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 "uncontrolled" 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
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...
1
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...
8
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...
1
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...
1
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...
2
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)...
3
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...
8
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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,...

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.