Calculator

Pages: 12
Can Someone Give me a general idea how to go about starting this program.

Please and thank you.




1. Let the user enter two integers from 1 and 12. Verify that the values are within
the given range.
2. Let the user choose an operation by entering a number between 1 and 5.
Verify that the entry is within the given range.
1 for addition
2 for subtraction
3 for multiplication
4 for division
5 for modulus
3. If the user chooses addition, add the numbers. If the user chooses
subtraction, subtract the second number from the first. If the uses choose
multiplication, multiply the two numbers. If the user chooses division, divide the
first number by the second, showing only the quotient, but not the remainder. If
the user chooses modulus, divide the first number by the second, showing the
remainder, but not the quotient
For example:
If the user enters 12, 5 and 2, the program should display:
12 – 5 = 7
If the user enters 11, 4 and 4, the program should display:
11 / 4 = 2
If the user enters 12, 5 and 5, the program should display:
12 % 5 = 2
Use the following functions in your program:
int getnum(int min, int max) to get and validate user input
int sum(int num1, int num2) to do addition
int difference(int num1, int num2)to do subtraction
int product(int num1, int num2)to do multiplication
int quotient(int num1, int num2) to do division
int remainder(int num1, int num2) to do modulus
I would say that the assignment has given a detailed idea of how to do the program.

So what is the problem?

Usually, schools teach enough for the students to be able complete the assignment without too much effort.

You must have some idea - show us the code you have now, now matter how bad you think it is. And post the compiler output. Don't forget to use code tags - the <> button on the right.
Am happy to help, however it is your job to write the code.

Good luck - cheers !!!
Last edited on
I'm struggling with how to use Return with multiple inputs for the first funtion.
You don't need to return multiple inputs from the function. You only need to return a single int.
The other functions are simple but how would approach validating the user inputs without using bool.
You can't validate user input with a bool in the first place, so, problem solved.

The general form of user-input-validation is like this:
1
2
3
4
5
while(!(std::cin >> MyVariable) || MyVariable < TheMinimum || MyVariable > TheMaximum)
{
    std::cout << "Try again: ";
    std::cin.clear();
}
And that's pretty much it.
Last edited on
The other functions are simple but how would approach validating the user inputs without using bool.


Welcome to programming. Programming is not memorising syntax. It's thinking about problems in a way that the answer lends itself to a programmatic solution. There are so many ways to do this. You just have to think.
How do I return these values.



int getnum(int min, int max) // Gets and Validates Users input
{


do{
cout <<"\nEnter first number between 1 and 12: ";
cin >>min;
}while(!(min>=1 && min<=12));

do{
cout <<"\nEnter second number between 1 and 12: ";
cin >>max;
}while(!(max>=1 && max<=12));

}

So you have a function that can give you ONE value, but you want TWO values. Perhaps you could use the function TWICE.

1
2
3
4
// get first value
int valueone = getnum(min,max);
// get second value
int valuetwo= getnum(min,max);


Note that min and max are not the values you are trying to get. You already know what the min and max value is. Storing the input values into min and max wrecks it.
Last edited on
How should i go about checking the inputs individually, if the function
int getnum(int min, int max) has to take in two inputs?
The function takes two parameters, aka arguments. It should take one user input with std::cin and ensure it is between min and max, then finally return it. Moschops showed how you can use it to input two variables.
Last edited on
How should i go about checking the inputs individually, if the function
int getnum(int min, int max) has to take in two inputs?


You do the checking inside the function. The inputs you pass to the functions are NOT the user inputs. They are used by your function when you check the user input. They are the max and min values that you are willing to accept from the user.

It sounds like this level of programming (i.e. thinking about problem solving) is beyond you. You need to go back to simpler tasks.
The assignment requires me to use int getnum(int min, int max) and im not sure how to go about it.
We've explained how thoroughly. Reread our posts and [quote]quote specific things[/quote] that you don't understand.
I'm not exactly sure where I make these declarations.

"// get first value
int valueone = getnum(min,max);
// get second value
int valuetwo= getnum(min,max);"


Also the function that i just demonstrated for int getnum(int min, int max) should i disregard that?

I'm not exactly sure if i should be prompting the user for a correct input with in this funtion "int getnum(int min, int max)" I know I have to check the range but we never learned about

"std::cout << "Try again: ";
std::cin.clear();"
I'm not sure how to return two inputs without out addressing.
I'm not sure how to return two inputs without out addressing.


Do NOT try to return two inputs. DO NOT. Instead, get one input and then get another input. Do you see the difference?

You can call the function once, and then, when that has finished and you have one of the user's values, you can call the function AGAIN and get another input. One input PLUS one input makes TWO inputs. You can get both inputs by using the function TWICE.

I already explained this above and even gave you the code to do it.

If you don't understand this, you are way, way out of your depth here.
Last edited on
My code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>        //c++ streaming I/O
#include <cstdio>          //c standard I/O
using namespace std;       //names reference
//declare functions

int getnum(int min, int max); 			//gets and Validates Users input
int sum (int num1, int num2);			//adds two numbers, return sum
int difference(int num1, int num2);		//subracts two numbers, return difference
int product(int num1, int num2)	;		//multiplys two numbers, return product
int quotient(int num1, int num2);		//divides two numbers, return quotient
int remainder(int num1, int num2);		//divides two numbers, return remainder


int main ()
{
//varible declarations
int input1;                             //user entry 
int input2;                             //user entry 


//describe program and get input
cout<<"\nThis program Adds, Subtracts, Multiplys & Divides #'s between 1 & 12\n";
getnum(input1,input2);

sum(input1, input2);


    
cout << endl<<endl;        //blank line    
system("pause");           //keep command window open
return 0;                  //end program
}

//Functions

int getnum(int min, int max) 		// Gets and Validates Users input  
{


do{
cout <<"\nEnter first number between 1 and 12: ";
cin >>min;
}while(!(min>=1 && min<=12));

do{
cout <<"\nEnter second number between 1 and 12: ";
cin >>max; 
}while(!(max>=1 && max<=12));

}

int sum(int num1, int num2)   		//adds two numbers, return sum
{
  return num1+num2;
}

int difference(int num1, int num2)  //subracts two numbers, return difference
{
  return num1-num2;
}

int product(int num1, int num2)   	// multiplys two numbers, return product
{
  return num1*num2;
}

int quotient(int num1, int num2) 	//divides two numbers, return quotient
{
  return num1/num2;
}

int remainder(int num1, int num2)	//divides two numbers, return remainder 
{
  return num1%num2;
}









1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream.h>
#include <conio.h>
int checknum(int,int min=-1000,int max=1000); //Am using Default arg for min and max range
int sum(int,int);
int difference(int,int);
int product(int,int);
int quotient(int,int);
int remainder(int,int);
void main ()
{
  int a,b;
  cout<<"Enter two numbers : ";
  cin>>a>>b;
  if(checknum(a)==0)
  cout<<"Number is not within range...";
/* NOTE: If u want to change the range, just pass its values in the func call itself! eg : checknum(a,-100,100); checks if a is between -100 and 100 */
  if(checknum(b)==0)
  cout<<"Number is not within range...";
  cout<<"LIFT OF OPERATIONS : "<<endl;
  cout<<"1. Addition"<<endl<<"2.Subtraction"<<endl<<"3.Multiplication"<<endl<<"4.Division"<<endl<<"5.Ramainder";
  int ch;
  cin>>ch;
  switch(ch)
  {
  case 1 : cout<<"Addition of given numbers is : "<<sum(a,b);
               break;
  case 2 : cout<<"Difference of given numbers is : "<<difference(a,b);
               break;
  case 3 : cout<<"Product of the given numbers is : "<<product(a,b);
               break;
  case 4 : cout<<"Quotient of the given numbers is : "<<quotient(a,b);
               break;
  case 5 : cout<<"Modulus of the given numbers is : "<<remainder(a,b);  
               break;
  default : cout<<"Invalid option...";
  }
  getch();
int checknum(int a,int min,int max)
{
  if(a>min&&a<max)
  return 1;
  else
  return 0;
}
int sum(int num1,int num2)
{
  return num1+num2;
}
int difference(int num1,int num2)
{
  return num1-num2;
}
int product(int num1,int num2)
{
  return num1*num2;
}
int quotient(int num1,int num2)
{
return num1/num2;
}
int remainder(int num1,int num2)
{
  return num1%num2;
}
 

I use checknum() function instead of getnum() to just check if the entered number is within range... the reason i didnt get the input inside the function is that once the control shifts from the function to main, the number is destroyed! so i rather get the number in main, link it to the function thro another arg, and process it...
if u like to, u can modify the code accordingly to use getnum()...
Last edited on
Sorry guys, if u think its hindering momo3o3's thinkings! if u think u wud try the program urselves, i wud rather delete my above post... cause the other people were trying to bring out t code from u... but i posted the code... pretty offensive rite? so do tell me if i want to delete the above post
@SwatSid

Jumping Juniper Berries!!! You gave the OP the whole answer. Much better to give clues, so he might learn better.

I guess a lot of clues have been given already, but now the OP can submit his assignment and get reasonably good marks.
Pages: 12