Hello all, first post (well second really but the first never materialized on the board).
I want to have calculations take place inside a switch statement that calls the appropriate function. The menu portion of the program works well, but I can't figure out how to let the user actually input 2 different numbers to the functions. My questions are:
1. If I use scanf to assign values to the variables, what determines end of input to each variable? (ie.. scanf("%d%d", &a, &b) what is the end of a, what is the end of b?)
2. Should I assign the variables to user input inside the switch, or try to do it in the functions below?
3. Is there something I haven't thought to ask that will screw me over? I'm really new to this.
Code:
#include<stdio.h>
int add(int b, int a);
int mult(int b, int a);
main()
{
int preass1, preass2, a, c; /*want preass1, and 2 to both be user input, stands for preassigned*/
printf("Press [1] for Addition, or Multiplication");
while ((c=getchar()) !=EOF) {
if (c=='0' || c<='9' && c>='3'){
printf("didn't enter menu");
} /*for when you can't get into the main menu of calculator*/
else if (c=='1') {
printf("Hit 1 for addition \nHit 2 for multiply\n");
scanf("%d", &a);
switch (a) {
case 1:
printf("%d", add(preass1,preass2));
break;
case 2:
printf("%d", mult(preass1,preass2));
break;
default:
printf("Ruh Roh\n");
} /*end switch, end of menu for calculator*/
} /*end else if*/
} /*end while*/
return 0;
}
int add(int a, int b)
{
a=5; /*should I assign user input to variables here, or above?*/
b=10;
return a + b;
}
int mult(int a, int b)
{
a=5;
b=10;
return a * b;
}
p.s. This really was a test of multilayer menu, but I want to add functionality if I can. Thanks for any help!
edit: Changed a variable before posting and didn't change all the conditions testing it.