Notice: Undefined offset: 1886682 in /var/www/html/qa-external/qa-external-users.php on line 744
My code successfully run on codeblocks but not here in online gdb, why is it? - OnlineGDB Q&A
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.

My code successfully run on codeblocks but not here in online gdb, why is it?

0 votes
asked Sep 30, 2022 by JOSHUA REBUTA (120 points)
#include <iostream>
#include <string>
using namespace std;

void multiplication(int n = 1)
{
    double num1,num2,product;
    cout << "\tMultiplication\n";
    cout << "Enter 1st no: ";
    cin >> num1;
    cout << "Enter 2nd no: ";
    cin >> num2;
    product = num1*num2;
    cout << "The product of " << num1 << " and " << num2 << " = " << product;
}

void addition(int n = 2)
{
    double num1,num2,sum;
    cout << "\tAddition\n";
    cout << "Enter 1st no: ";
    cin >> num1;
    cout << "Enter 2nd no: ";
    cin >> num2;
    sum = num1+num2;
    cout << "The sum of " << num1 << " and " << num2 << " = " << sum;
}

void subtraction(int n = 3)
{
    double num1,num2,difference;
    cout << "\tSubtraction\n";
    cout << "Enter 1st no: ";
    cin >> num1;
    cout << "Enter 2nd no: ";
    cin >> num2;
    difference = num1 - num2;
    cout << "The difference of " << num1 << " and " << num2 << " = " << difference;
}

void division(int n = 4)
{
    double num1,num2,quotient;
    cout << "\tDivision\n";
    cout << "Enter 1st no: ";
    cin >> num1;
    cout << "Enter 2nd no: ";
    cin >> num2;
    quotient = num1/num2;
    cout << "The quotient of " << num1 << " and " << num2 << " = " << quotient;
}

void myname(int n = 5)
{
    string firstname,midname,lastname;
    cout << "\tFull Name\n";
    cout << "Enter your first name: ";
    cin >> firstname;
    cout << "Enter your middle name: ";
    cin >> midname;
    cout << "Enter your last name: ";
    cin >> lastname;
    cout << "My name is " << firstname << " " << midname << " " << lastname;
}

void menu()
{
    cout << "\tArithmetic Operators\n\n";
    cout << "1. Multiplication\n";
    cout << "2. Addition\n";
    cout << "3. Subtraction\n";
    cout << "4. Division\n";
    cout << "5. My Name\n";
    int n;
    cout << "Enter your choice: ";
    cin >> n;

    if (n==1){
       multiplication(1);
    } else if (n==2){
        addition(2);
    } else if (n==3){
        subtraction(3);
    } else if (n==4){
        division(4);
    } else if (n==5){
        myname(5);
    }
}

int main ()
{
    int i,answer;
    if(i==0){
    menu();
    cout << "\nWant to try again? Press 1 if yes or 0 if no: ";
    cin >> answer;
        if (answer==1){
            cout << endl;
            main();
        } else if (answer==0){
            cout << "End of the program";
            i++;
        }
    }
    return 0;
}

2 Answers

0 votes
answered Oct 2, 2022 by (10,560 points)

Line #93, change it to


int i=0;int answer=0;

Also invest some time to learn debugging tools like GDb, MS visual studio debugger .

0 votes
answered Oct 3, 2022 by Miss Arohi. (140 points)
#include <iostream>
#include <string>
using namespace std;

void multiplication(int n = 1)
{
    double num1,num2,product;
    cout << "\tMultiplication\n";
    cout << "Enter 1st no: ";
    cin >> num1;
    cout << "Enter 2nd no: ";
    cin >> num2;
    product = num1*num2;
    cout << "The product of " << num1 << " and " << num2 << " = " << product;
}

void addition(int n = 2)
{
    double num1,num2,sum;
    cout << "\tAddition\n";
    cout << "Enter 1st no: ";
    cin >> num1;
    cout << "Enter 2nd no: ";
    cin >> num2;
    sum = num1+num2;
    cout << "The sum of " << num1 << " and " << num2 << " = " << sum;
}

void subtraction(int n = 3)
{
    double num1,num2,difference;
    cout << "\tSubtraction\n";
    cout << "Enter 1st no: ";
    cin >> num1;
    cout << "Enter 2nd no: ";
    cin >> num2;
    difference = num1 - num2;
    cout << "The difference of " << num1 << " and " << num2 << " = " << difference;
}

void division(int n = 4)
{
    double num1,num2,quotient;
    cout << "\tDivision\n";
    cout << "Enter 1st no: ";
    cin >> num1;
    cout << "Enter 2nd no: ";
    cin >> num2;
    quotient = num1/num2;
    cout << "The quotient of " << num1 << " and " << num2 << " = " << quotient;
}

void myname(int n = 5)
{
    string firstname,midname,lastname;
    cout << "\tFull Name\n";
    cout << "Enter your first name: ";
    cin >> firstname;
    cout << "Enter your middle name: ";
    cin >> midname;
    cout << "Enter your last name: ";
    cin >> lastname;
    cout << "My name is " << firstname << " " << midname << " " << lastname;
}

void menu()
{
    cout << "\tArithmetic Operators\n\n";
    cout << "1. Multiplication\n";
    cout << "2. Addition\n";
    cout << "3. Subtraction\n";
    cout << "4. Division\n";
    cout << "5. My Name\n";
    int n;
    cout << "Enter your choice: ";
    cin >> n;

    if (n==1){
       multiplication(1);
    } else if (n==2){
        addition(2);
    } else if (n==3){
        subtraction(3);
    } else if (n==4){
        division(4);
    } else if (n==5){
        myname(5);
    }
}

int main ()
{
    int i=1,answer;
    if(i==1){
    menu();
    cout << "\nWant to try again? Press 1 if yes or 0 if no: ";
    cin >> answer;
        if (answer==1){
            cout << endl;
            main();
        } else if (answer==0){
            cout << "End of the program";
            i++;
        }
    }
    return 0;
}

I have changed the value of i=1; and also intializes the value of i to 1 which u have not done.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...