Question

C++ problem where should I do overflow part? in this code do not write a new...

C++ problem

where should I do overflow part? in this code

do not write a new code for me please

///////////////////

// this program read two number from the user
// and display the sum of the number
 
#include <iostream>
#include <string>
 
using namespace std;
const int MAX_DIGITS = 10; //10 digits
void input_number(char num[MAX_DIGITS]);
void output_number(char num[MAX_DIGITS]);
void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base);
int main()
{
    // declare the array = {'0'}
    char num1[MAX_DIGITS] ={'0'};
    char num2[MAX_DIGITS] ={'0'};
    char result[MAX_DIGITS] ={'0'};
    int base;
    cout << " please enter 10 digits number for this program." << endl << endl;
    // base on 2-35
    cout << " what base for your numbers? (2-35)" << endl<< endl;
    cin >> base;
    cin.ignore(100,'\n');
    cout << " Enter the first number: " << MAX_DIGITS << " digits " << endl << endl;
    // call the input  number function to read the input
    input_number(num1);
    cout << "Enter the second number: "<< MAX_DIGITS << " digits" << endl << endl;
    // call the input  number function to read the input
    input_number(num2);
 
    // sum of the number
    add(num1,num2,result,base);
    // output
    cout << endl;
    cout << "Sum of ";
    output_number(num1);
    cout << " and ";
    output_number(num2);
    cout << " is ";
    output_number(result);
    cout << endl;
    return 0;
}
//input function
void input_number(char num[MAX_DIGITS])
{
    char ch = '0';
    int n = 0;
    // read the character from user
    while((ch = cin.peek())!=' ' && n < MAX_DIGITS)
    //store character into array
    {
        num[n]= ch;
        n++;
        cin.get(ch);
    }
        cin.ignore(100, '\n');
        //reverse digits in the array to do sum
        for (int i= 0; i <n/2 ; i++)
        {
            char temp = num[i];
            num[i]= num[n-1-i];
            num[n-1-i] = temp;
        }
 
}
//output function
void output_number(char num[MAX_DIGITS])
{
    for (int i=MAX_DIGITS -1 ; i>=0 ; i--)
    {
    if(num[i]!=0)
    {
    cout <<num[i];
    }
    }
}
// sum function
void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base)
{
    int temp_num1, temp_num2,sum;
    int carry1 =0;
    int carry2 =0;
    char temp;
    // repeat the loop for all digits of the array
    for (int i=0; i <MAX_DIGITS ; i++)
    {
        //check if char between 0-9
        if (num1[i]>='0'&& num1[i]<'0'+10) //'0'-'9'
        {
        // change maybe
            temp_num1 = num1[i]-'0';
        }
        else
        {
            //  to uppercase
            temp = toupper(num1[i]);
            // check if between A-Z
            if (temp >= 'A' && temp <='Z')
            {
                temp_num1 = temp-'A'+10;
            }
            else
            {
                cout << " input invaild " << endl;
            }
        }
        // same as num1
        if (num2[i] >= '0' && num2[i] <'0'+10)
        {
            temp_num2 = num2[i] - '0';
        }
        else
        {
            // to uppercase
            temp = toupper(num2[i]);
            // check if between A-Z
            if (temp >= 'A' && temp <= 'Z')
            {
                temp_num2 = temp -'A'+10;
            }
            else
            {
                cout << "input invaild" << endl;
            }
        }
 
        carry1 = carry2;
        //calculate
 
        sum = (temp_num1+temp_num2+carry1)% base;
        //carry 2
        carry2 = (temp_num1+temp_num2+carry1)/ base;
        // check sum between 0-10
        if (sum >= 0 && sum < 10)
        {
            result[i] = char('0' + sum);
        }
        // else between 10-35
        else if (sum >=10 && sum <35)
        {
            result[i] = char ('A' +sum -10);
        }
 
    }
 
}
 
 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

You must write overflow part in method add() at this code because it part is overflowing.

//calculate
 
        sum = (temp_num1+temp_num2+carry1)% base;
        //carry 2
        carry2 = (temp_num1+temp_num2+carry1)/ base;
        // check sum between 0-10
        if (sum >= 0 && sum < 10)
        {
            result[i] = char('0' + sum);
        }
        // else between 10-35
        else if (sum >=10 && sum <35)
        {
            result[i] = char ('A' +sum -10);
        }
Add a comment
Know the answer?
Add Answer to:
C++ problem where should I do overflow part? in this code do not write a new...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • May i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #...

    may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...

  • C++ I am using visual studio for it. Make a copy of the Rational class you...

    C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • Fix this code so only the function prototype comes before main. #include <iostream> using namespace std;...

    Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...

  • How to convert C++ code to C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() {...

    How to convert C++ code to C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int num1; int c, num2, ans; cout<<"Enter difficulty level(1/2)\n"; cin>>c; if(c==1) { num1= rand() % 10; num2= rand() % 10; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please try again.\n"; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; } } } else if(c==2) { num1= rand() % 10+90; num2= rand() % 10+90; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No....

  • This is a fill in the code type: // FILL IN THE CODE - Write a...

    This is a fill in the code type: // FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order. #include <iostream> using namespace std; int main() {   int num1;       // holds 1st number   int num2;       // holds 2nd number   int result;       // holds result of multiplication   int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number   int *num2Ptr...

  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

  • How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int...

    How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int num1; int c, num2, ans; cout<<"Enter difficulty level(1/2)\n"; cin>>c; if(c==1) { num1= rand() % 10; num2= rand() % 10; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please try again.\n"; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; } } } else if(c==2) { num1= rand() % 10+90; num2= rand() % 10+90; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT