Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

C++ How to include negative numbers and fractions and decimals in simple calculator

–2 votes
asked Apr 6, 2018 by SavageMango (310 points)
This is my code please tell me if there is a way to do what I listed in the title. Do i need to download and include a class? What do I have to do?

#include <iostream>

using namespace std;

int divide(int arr[],int size){
    
    size=2;
    cout<<"Please enter your dividend"<<endl;
    cin >>arr[0];
    cout<<"Please enter your divisor"<<endl;
    cin >>arr[1];
    int quotient = arr[0] / arr[1];
    int remiander = arr[0] % arr[1];
    cout<<quotient<<"R"<<remiander<<endl;
}

int multiply(int arr[], int size){
    cout<<"How many numbers do you wish to multiply together?"<<endl;
    cin>>size;
    cout<<"Enter the base number that is being multiplied"<<endl;
    cin>>arr[0];
    int product=arr[0];
    for(int x=1;x<size;x++){
    cout<<"Please enter a number that you wish to muliply by"<<endl;
    cin>>arr[x];
    product*= arr[x];
    }
    return product;
}

int add(int arr[], int size) {
    int sum = 0 ;
    cout<<"How many numbers do you want to add?"<<endl;
    cin>>size ;
    for(int x=0;x<size;x++){
    cout<<"Please enter a number that you wish to have in your sum"<<endl;
    cin>>arr[x] ;
    sum+=arr[x] ;
    }
    return sum ;

}

int sub(int arr[], int size){
    cout<<"How many numbers do you wish to subtract?"<< endl;
    cin>>size;
    cout<<"Enter the number you wish to subtract from"<< endl;
    cin>>arr[0];
    int diff = arr[0];
    for(int x=1;x<size;x++){
    cout<<"Please enter a number that you would like to subtract"<< endl;
    cin>>arr[x];
    diff-=arr[x];
    }
}

int main(){
int operation = 0;
cout<<"Which operation do you want? Enter 1 for addition, 2 for subtraction, 3 for division, or 4 for multiplication"<<endl;
cin>>operation;
if (operation == 1){
    int A[5];
    int r = add(A,sizeof A);
    cout<<"The sum is "<<r<<endl;}
if (operation == 2){
    int A[5];
    int r = sub(A,sizeof A);
    cout<<"The difference is "<<r<<endl;}
if (operation == 3){
    int A[5];
    divide(A,sizeof A);}
if (operation == 4){
    int A[5];
    int r = multiply(A,sizeof A);
    cout<<"The product is "<<r<<endl;}

}

1 Answer

0 votes
answered Apr 10, 2018 by femrost
for fractions, you could either store the fraction as 2 numbers, or you could convert the fraction to decimal form and just use a double instead of an integer. for negative you should be able to just have it be a negative.
commented Apr 12, 2018 by SavageMango (310 points)
In my subtraction section when I subtract it by 8 from 3 it just says the answer is 0
Here is some updated code
#include <iostream>

using namespace std;

signed int divide(signed int arr[],signed int size){
    
    size=2;
    cout<<"Please enter your dividend"<<endl;
    cin >>arr[0];
    cout<<"Please enter your divisor"<<endl;
    cin >>arr[1];
    signed int quotient = arr[0] / arr[1];
    signed int remiander = arr[0] % arr[1];
    cout<<quotient<<"R"<<remiander<<endl;
}

signed int multiply(signed int arr[], signed int size){
    cout<<"How many numbers do you wish to multiply together?"<<endl;
    cin>>size;
    cout<<"Enter the base number that is being multiplied"<<endl;
    cin>>arr[0];
    signed int product=arr[0];
    for(int x=1;x<size;x++){
    cout<<"Please enter a number that you wish to muliply by"<<endl;
    cin>>arr[x];
    product*= arr[x];
    }
    return product;
}

signed int add(signed int arr[], signed int size) {
    signed int sum = 0 ;
    cout<<"How many numbers do you want to add?"<<endl;
    cin>>size ;
    for(int x=0;x<size;x++){
    cout<<"Please enter a number that you wish to have in your sum"<<endl;
    cin>>arr[x] ;
    sum+=arr[x] ;
    }
    return sum ;

}

signed int sub(signed int arr[], signed int size){
    cout<<"How many numbers do you wish to subtract?"<< endl;
    cin>>size;
    cout<<"Enter the number you wish to subtract from"<< endl;
    cin>>arr[0];
    signed int diff = arr[0];
    for(int x=1;x<size;x++){
    cout<<"Please enter a number that you would like to subtract"<< endl;
    cin>>arr[x];
    diff-=arr[x];
    }
}

int main(){
int operation = 0;
cout<<"Which operation do you want? Enter 1 for addition, 2 for subtraction, 3 for division, or 4 for multiplication"<<endl;
cin>>operation;
if (operation == 1){
    signed int A[5];
    signed int r = add(A,sizeof A);
    cout<<"The sum is "<<r<<endl;}
if (operation == 2){
    signed int A[5];
    signed int r = sub(A,sizeof A);
    cout<<"The difference is "<<r<<endl;}
if (operation == 3){
    signed int A[5];
    divide(A,sizeof A);}
if (operation == 4){
    signed int A[5];
    signed int r = multiply(A,sizeof A);
    cout<<"The product is "<<r<<endl;}

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...