B
Buck Rogers
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 "unreachable code" warning for line
32(return 0. The
program runs fine. Why does the compiler(Borland 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
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 "unreachable code" warning for line
32(return 0. The
program runs fine. Why does the compiler(Borland 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