473,756 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help needed please!

Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
=============== =============== =============== ======
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}

int menu( void )
{
int option;

puts("1. List directory");
puts("2. help");
puts("3. time");
puts("4. type menu.c");
puts("5. Quit");
scanf("%d", &option);

return option;
}
=============== =============== =============== =============
Problem: My compiler is giving me an "unreachabl e code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borlan d C++ Builder 6) give this
warning?

Task 2: Create a calculator program.

Below is my humble offering.
=============== =============== =============== =============== =======
#include <stdio.h>

void add(int a, int b);
void subtract(int a, int b);
void multiply(int a, int b);
void divide(int a, int b);

int main( void )
{
int a, b;
char c;

puts("Enter an integer");
scanf("%d", &a);
puts("Enter an operator");
scanf("%c", &c);
puts("Enter another integer");
scanf("%d", &b);
switch( c ) {
case '*':
multiply(a, b);
break;
case '+':
add(a, b);
break;
case '-':
subtract(a, b);
break;
case '/':
divide(a, b);
break;
default:
puts("Operator is not valid");
}
return 0;
}

void add(int a, int b)
{
printf("Sum is: %d", a+b);
}

void subtract(int a, int b)
{
printf("Answer is: %d", a-b);
}

void multiply(int a, int b)
{
printf("Product is: %d", a*b);
}

void divide(int a, int b)
{
printf("Answer is: %d", a/b);
}
=============== =============== =============== ========
Output:

Enter an integer
2
Enter an operator
Enter another integer

Problem: Why does the program not allow the user to input the operator
before
jumping straight to "Enter another integer"? I think I am missing
something very
fundamental here.

Thanks in advance guys! Love your work!

Buck
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 14 '05 #1
15 2752
# while(1) {
# option = menu();
# switch(option) {
# }
# }
# return 0;

# Problem: My compiler is giving me an "unreachabl e code" warning for line
# 32(return 0;). The
# program runs fine. Why does the compiler(Borlan d C++ Builder 6) give this
# warning?

Because it's true? The while condition is always true, and loop is devoid
of breaks or gotos out of loop. The only one to exit the loop is to exit
the program; whether it loops forever or exits the program, no code after
the loop can be reached and executed.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
So basically, you just trace.
Nov 14 '05 #2
nrk
Buck Rogers wrote:
Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
=============== =============== =============== ======
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}

int menu( void )
{
int option;

puts("1. List directory");
puts("2. help");
puts("3. time");
puts("4. type menu.c");
puts("5. Quit");
scanf("%d", &option);

return option;
}
=============== =============== =============== =============
Problem: My compiler is giving me an "unreachabl e code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borlan d C++ Builder 6) give this
warning?

The only way out of your while ( 1 ) loop is through the exit(0) call, and
that doesn't bring you to the return 0; statement either. Hence the
complaint. Ideally, you should be able to replace the exit(0) call with
return 0; get rid of the existing return statements and the compiler should
be happy.
Task 2: Create a calculator program.

Below is my humble offering.
=============== =============== =============== =============== =======
#include <stdio.h>

void add(int a, int b);
void subtract(int a, int b);
void multiply(int a, int b);
void divide(int a, int b);

int main( void )
{
int a, b;
char c;

puts("Enter an integer");
scanf("%d", &a);
Read the fine FAQ. Specifically Q12.18:
http://www.eskimo.com/~scs/C-faq/q12.18.html

One fix is:
int ret;
/* read an int and discard rest of line */
ret = scanf("%d%*[^\n]", &a);
if ( ret < 1 ) {
/* ret == 0 => invalid input */
/* ret == EOF => end of input or error */
/* handle appropriately */
}
/* read and discard newline */
if ( !feof(stdin) ) getchar();
puts("Enter an operator");
scanf("%c", &c);
Similar problem as above. Similar fix can be applied.
puts("Enter another integer");
scanf("%d", &b);
switch( c ) {
case '*':
multiply(a, b);
break;
case '+':
add(a, b);
break;
case '-':
subtract(a, b);
break;
case '/':
divide(a, b);
break;
default:
puts("Operator is not valid");
}
return 0;
}

void add(int a, int b)
{
printf("Sum is: %d", a+b);
}

void subtract(int a, int b)
{
printf("Answer is: %d", a-b);
}

void multiply(int a, int b)
{
printf("Product is: %d", a*b);
}

void divide(int a, int b)
{
printf("Answer is: %d", a/b);
}
=============== =============== =============== ========
Output:

Enter an integer
2
Enter an operator
Enter another integer

Problem: Why does the program not allow the user to input the operator
before
jumping straight to "Enter another integer"? I think I am missing
something very
fundamental here.

What you're missing is knowledge of how scanf works. A trip to the scanf
pages in your C book could come in handy.

-nrk.
Thanks in advance guys! Love your work!

Buck


--
Remove devnull for email
Nov 14 '05 #3


Buck Rogers wrote:
Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
=============== =============== =============== ======
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}

int menu( void )
{
int option;

puts("1. List directory");
puts("2. help");
puts("3. time");
puts("4. type menu.c");
puts("5. Quit");
scanf("%d", &option);

return option;
}
=============== =============== =============== =============
Problem: My compiler is giving me an "unreachabl e code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borlan d C++ Builder 6) give
this warning?

It gives the warning because the expression is unreachable. You can
simply ignore the warning or you change the logic. One way would be
change the while loop into a do-while loop.

#include <stdio.h>

int menu( void );

int main( void )
{
int option;

do {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
break;

default:
puts("Not a valid option, try again");
}
}while(option != 5);
return 0;
}

int menu( void )
{
int option;

puts("1. List directory");
puts("2. help");
puts("3. time");
puts("4. type menu.c");
puts("5. Quit");
scanf("%d", &option);

return option;
}


Task 2: Create a calculator program.

Below is my humble offering.
=============== =============== =============== =============== =======
#include <stdio.h>

void add(int a, int b);
void subtract(int a, int b);
void multiply(int a, int b);
void divide(int a, int b);

int main( void )
{
int a, b;
char c;

puts("Enter an integer");
scanf("%d", &a);
Add here:
getchar();
puts("Enter an operator");
scanf("%c", &c);
puts("Enter another integer");
scanf("%d", &b);
switch( c ) {
case '*':
multiply(a, b);
break;
case '+':
add(a, b);
break;
case '-':
subtract(a, b);
break;
case '/':
divide(a, b);
break;
default:
puts("Operator is not valid");
}
return 0;
}

void add(int a, int b)
{
printf("Sum is: %d", a+b);
printf("Sum is: %d\n", a+b);
}

void subtract(int a, int b)
{
printf("Answer is: %d", a-b);
printf("Answer is: %d\n", a-b); }

void multiply(int a, int b)
{
printf("Product is: %d", a*b);
printf("Product is: %d\n", a*b); }

void divide(int a, int b)
{
printf("Answer is: %d", a/b);
printf("Answer is: %d\n", a/b); }
=============== =============== =============== ========
Output:

Enter an integer
2
Enter an operator
Enter another integer

Problem: Why does the program not allow the user to input the operator
before
jumping straight to "Enter another integer"? I think I am missing
something very
fundamental here.


When you enter the first number, your input includes the digits for
the number and the newline char, '\n', because you preesed the
enter key. In the first scanf statement, the %d specifier will
stop reading the input buffer when it incounters the '\n' character.
This character remains in the input buffer. In the next scanf
statement you are using the %c specifier. This specifier will not
remove leading whitespace, so the value of the newline char is
read into variable c. The operator,*,/,+ or -, and a newline character
remain in the input buffer. Your next scanf statement, using
the %d, reads the operator. Seeing the operator is not a digit,
it aborts scanning.

The easy solution is to put getchar() after the first scanf
statement which will remove the newline char from the input
buffer and enables you to read the operator into variable c.

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4


Buck Rogers wrote:
Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
=============== =============== =============== ======
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}
<snip> =============== =============== =============== =============
Problem: My compiler is giving me an "unreachabl e code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borlan d C++ Builder 6) give
this warning?


As others have already pointed out, you can't reach that line. Sticking
an "exit" in the middle of your code is pretty nasty in terms of code
structure (it's like having a "goto return;"), and having a loop that
you claim is infinite when it's not, isn't great either. If you have a
loop that termintes under some condition (or the negation of some
condition), then that condition is important enough to be explicitly
named by you so that anyone in future who has to enhance this code
doesn't have to waste time trying to work out the abstraction for that
condition.

To make your code clearer for the next guy who has to work on it (i.e.
more easily maintainable), write it this way:

int main( void )
{
int option;
int prompting = 1;

while(prompting ) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
prompting = 0;
break;
default:
puts("Not a valid option, try again");
}
}
return 0;
}

That will co-incidentally get rid of the compiler warning.

Ed.

Nov 14 '05 #5
Ed Morton wrote:
.... snip ...
To make your code clearer for the next guy who has to work on it
(i.e. more easily maintainable), write it this way:

int main( void )
{
int option;
int prompting = 1;

while(prompting ) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
prompting = 0;
break;
default:
puts("Not a valid option, try again");
}
}
return 0;
}

That will co-incidentally get rid of the compiler warning.


My solution for the same problem:

int main(void)
{
int option;

while(5 != (option = menu())) {
switch(option) {
case 1: system("dir"); break;
case 2: system("help"); break;
case 3: system("time"); break;
case 4: system("type menu.c"); break;
default: puts("Not a valid option, try again"); break;
}
}
puts("Quitting program");
return 0;
}

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #6
nrk wrote:
Buck Rogers wrote:

Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
============= =============== =============== ======== (snip code)
Problem: My compiler is giving me an "unreachabl e code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borlan d C++ Builder 6) give this
warning?


The only way out of your while ( 1 ) loop is through the exit(0) call, and
that doesn't bring you to the return 0; statement either. Hence the
complaint. Ideally, you should be able to replace the exit(0) call with
return 0;


Or (better IMHO) replace the exit(0) with a break, so 1/code after the
loop may be executed and 2/ the function has a single point of exit
located at the end of the function (which usually makes a more readable
code).

Bruno

Nov 14 '05 #7
Buck Rogers wrote:

do {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
break;
default:
puts("Not a valid option, try again");
}
}


while (option != 5);

--
pete
Nov 14 '05 #8
On Sat, 17 Jan 2004 12:19:41 GMT, nrk
<ra*********@de vnull.verizon.n et> wrote:
Buck Rogers wrote:
Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
=============== =============== =============== ======
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}
snip Problem: My compiler is giving me an "unreachabl e code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borlan d C++ Builder 6) give this
warning?

The only way out of your while ( 1 ) loop is through the exit(0) call, and
that doesn't bring you to the return 0; statement either. Hence the
complaint. Ideally, you should be able to replace the exit(0) call with


Why? The exit(0) works fine.
return 0; get rid of the existing return statements and the compiler should
be happy.


<<Remove the del for email>>
Nov 14 '05 #9
nrk
Barry Schwarz wrote:
On Sat, 17 Jan 2004 12:19:41 GMT, nrk
<ra*********@de vnull.verizon.n et> wrote:
Buck Rogers wrote:
Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
=============== =============== =============== ======
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}
snip Problem: My compiler is giving me an "unreachabl e code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borlan d C++ Builder 6) give
this warning?


The only way out of your while ( 1 ) loop is through the exit(0) call, and
that doesn't bring you to the return 0; statement either. Hence the
complaint. Ideally, you should be able to replace the exit(0) call with


Why? The exit(0) works fine.


Except for the complaint from OP's compiler. Simply getting rid of the
return statment may not be an option as the compiler may complain about no
value being returned from main. Of course, you could just happily co-exist
with the compiler diagnostic as well.

-nrk.
return 0; get rid of the existing return statements and the compiler
should be happy.


<<Remove the del for email>>


--
Remove devnull for email
Nov 14 '05 #10

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

Similar topics

0
1583
by: Durham Writer | last post by:
Hi all, I'm currently working on a resource directory for the homeless, needy and those dealing with mental illness. The site is located at: http://triangle-resources.no-ip.org Triangle Resources denotes that this is for the Triangle area of North Carolina (Raleigh, Durham, Chapel Hill). Right now, I just list the information with html markup. I'm thinking there is a better way to present this and make it more useful, especially as...
5
1436
by: Raj.SB | last post by:
I have a reg exp pattern as below works fine for the string : <param name="url" value="contents/cf_intro.htm" valuetype="ref"> mObjRegExp.pattern="<param.+?value=""(+)"" But the same is not working for <param name="url" valuetype="ref" value="../fmmain/fmindex.htm" note that, i want to capture the url that comes after value=". if the value=" comes in the second place in the html tag it works. but if it comes in the 3rd position,...
2
293
by: Stephen | last post by:
I am trying to compare the tables in two similar environments using the SQLDMO object. I am able to use this object to access different SQL servers and choose two different databases. The versions of the databases are slightly different so I would like to be able to compare them for things like datatypes, primary and foreign keys, number of records etc. I know that there are packages out there that do this kind of thing but I have to...
0
1243
by: Stephen | last post by:
I was wondering if someone could please help me with an array I'd like to create in an asp.net page. I have to design an array which stores the values of addresses manually entered into textboxes (txtAdd,txtCity&txtPostcode). The array must hold the values of these three textboxes. I would like the array to also display these address values in an asp:Lable within a asp:TableCell inside a asp:Table. I want all this to happen on the...
2
1721
by: | last post by:
internal struct ConstantValue { internal DateTime EffectiveFrom; internal DateTime EffectiveThrough; internal DateTime DateValue; internal double DoubleValue; } internal struct Constant {
9
1669
by: silverchrono | last post by:
this is my first semester in C and im trying to figure out how to reset a counter. heres why im trying to do. void text() 59 printf("You can end entering the text by using '#'\n"); 60 int i=0; 61 int j=0;
4
1364
by: inspiretechnologies | last post by:
Hi all, I'm creating a Php page with connection to a MySql database. In this page, I get all the articles (text) of a member, and when the article's length exceeds 500 characters, a link "read more" is shown. I must implement with Ajax a function that allows, when the link "read more" is clicked, to show the rest of the article. It's a sort of slide down function but more complicated I think. I'm a beginner so please can someone help me...
3
1213
by: meddow | last post by:
Ive been asked to make a function to Convert Fahrenheit to Celsuis, via the on screen interface of the web page, nothing seems to be working for me, and i can not see whats wrong, please help <<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Temp</title> <meta...
4
1670
by: Dave G | last post by:
I've just been informed that an Access 2003 based system in use at one of my customers now needs to send data in the form of an XML file to a webservice. They told me it was simple - but I don't know anything about it and a googling session yesterday made it look very complicated. So can anyone help with this question - I don't need anything fancy and I haven't got the experience to work it all out for myself from first principles (even...
0
9455
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
9271
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
10031
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
9869
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
9838
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
9708
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
8709
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3805
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
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.