#include <iostream>

using namespace std;

int main()
{
    int a,b,c, ope;
    cout<<"enter a value" << endl;
    cin >> a;
    cout<<"enter b value" <<endl;
    cin >> b;
    cout<<"enter operation (+ - * / and %)";
    cin>> ope;
    if (ope == +)
    {
        c=a+b;
        cout<< c;
        break;
    }
    if (ope == -)
     {
        c=a-b;
        cout<< c;
        break;
     }
      else if (ope == *)
      {
          c=a*b;
      cout<< c;
        break;
      }
      else if (ope == /)
      {
       c= a/b;
       cout<< c;
       break;
      }
      else if (ope == %)
      {
        c=a%b;
        cout<< c;

      }
    else cout<<"entered wrong input";
    return 0;
}

whats wrong with the code its giving me so many errors.:confused:

Subhrapratim commented: #include<iostream> using namespace std; int main() {float a,b,c; cout <<"Enter First No: "<<endl; cin>>a; cout<<"Enter Second No: "<<endl +0

Recommended Answers

All 12 Replies

ope should be a char

ope should be a char

still getting so many errors

#include <iostream>

using namespace std; :'(

int main()
{
    int a,b,c;
    char ope;
    cout<<"enter a value" << endl;
    cin >> a;
    cout<<"enter b value" <<endl;
    cin >> b;
    cout<<"enter operation (+ - * / and %)";
    cin>> ope;
    if (ope == +)
    {
        c=a+b;
        cout<< c;
        break;
    }
    if (ope == -)
     {
        c=a-b;
        cout<< c;
        break;
     }
      else if (ope == *)
      {
          c=a*b;
      cout<< c;
        break;
      }
      else if (ope == /)
      {
       c= a/b;
       cout<< c;
       break;
      }
      else if (ope == %)
      {
        c=a%b;
        cout<< c;

      }
    else cout<<"entered wrong input";
    return 0;
}

if (ope == '+')

its working only if i remove the break;. if i include it its showing break statement not within loop or switch.

Break is used for stopping a loop. You are not using a loop.

oh ok thx.

//Instead of diclaring int a, b; rather declare float a, b; and yes for the operation ope declare as char ope; Please note that the compiler will give an error if you you put the modulus (%) operator in your code

at operator "-" put else if not just if ...

This code is wrong from some point the right code is here.

include
include

using namespace std;

int main(){
    int num1, num2, c;
    char opr;
    cout<<"Enter the first number: ";
    cin>>num1;
    cout<<"Enter the second number: ";
    cin>>num2;
    cout<<"Enter operator (+, -, *, /, %)";
    cin>>opr;
    if(opr== '+')
    {
        c = num1+num2;
        cout<<c;

    }
    if(opr== '-'){
        c = num1-num2;
        cout<<c;

    }
    else if (opr== '*'){
        c = num1*num2;
        cout<<c;

    }
    else if (opr== '/'){
        c = num1/num2;
        cout<<c;

    }
    else if (opr== '%'){
        c = num1%num2;
        cout<<c;

    }
    else 
    cout<<"You entered wrong operator";

    return 0;
}

In order to respect your if...else request I've come up with this solution. I hope this helps.:

include

void sayHello( ) {

printf("Hello\n"); }

// to say hello to the user
int add( int num1, int num2) {

    num1 = num1 + num2;
    return num1;}

int minus( int num1, int num2) {

  num1 = num1 - num2;
    return num1;}

int times( int num1, int num2) {

    num1 = num1 * num2;
    return num1;}

int divide( int num1, int num2) {

num1 = num1 / num2;

   return num1;}

// This is to declare the calculations
void flush_input(){

int ch;
while ((ch = getchar()) != '\n' && ch != EOF); }
// This is to flush the scanf values
// Kudos to Huw Collingbourne, Udemy Teacher
int main(int argc, char **argv) {

char c;
    char f;
    int n1;
    int n2;
    int total;

    // n1 = ' ';
    // n2 = ' ';

sayHello();

do {

c = ' ';

printf("Insert the type of Calculation you want to make:\n");

printf("A(d)dition, Subs(t)raction, Mu(l)tiplication, Di(v)ision: ");
    c = getchar();

if(c == 'd') {

printf("\nInsert the first number:");
scanf("%d", &n1);
printf("Insert the second number:");
scanf("%d", &n2);
total = add( n1, n2 );
printf("%d plus %d equals to %d\n", n1, n2, total );
flush_input(); } else { 
    if( c == 't') {
    printf("insert the base number:");
    scanf("%d", &n1);
    printf("Insert the subtracting number:");
    scanf("%d", &n2);
    total = minus( n1, n2 );
    printf("The difference between %d and %d is %d\n", n1, n2, total );
    flush_input(); } else {
        if( c == 'l') {
        printf("insert the first number:");
        scanf("%d", &n1);
        printf("Insert second number:");
        scanf("%d", &n2);
        total = times( n1, n2 );
        printf("%d times %d equals %d\n", n1, n2, total );
        flush_input(); } else {
            if( c == 'v') {
            printf("insert the first number:");
            scanf("%d", &n1);
            printf("Insert second number:");
            scanf("%d", &n2);
            total = divide( n1, n2 );
            printf("%d divided by %d equals %d\n", n1, n2, total );
            flush_input();
            } else {
    printf("I couln't understand the instruction\n Reseting program\n");
            } 
        }
    }
}
    f = ' ';
    printf("\nDo you wish to make another calculation?\n");

        printf("Choose (y)es or (n)ot: ");
        f = getchar();
        // scanf("%c", &c);
        getchar();
    } while( f != 'n' ); 
    printf("\nThat's all folks!\n");
return 0;}
#include <iostream>
using namespace std;
int main(){
    float num1, num2, c;
    char opr;
    cout<<"Enter the first number: ";
    cin>>num1;
    cout<<"Enter the second number: ";
    cin>>num2;
    cout<<"Enter operator (+, -, *, /)";
    cin>>opr;
    if(opr== '+')
    {
        c = num1+num2;
        cout<<c;
    }
    else if(opr== '-'){
        c = num1-num2;
        cout<<c;
    }
    else if (opr== '*'){
        c = num1*num2;
        cout<<c;
    }
    else if (opr== '/'){
        c = num1/num2;
        cout<<c;
    }
    //not sure why adding '+' works here 
    else if (opr != '+')
    {
      cout<<"You entered wrong operator";
    }
    return 0;
    }
#include <iostream>
using namespace std;
void calculator() {
    int a,b,sum;
    char oper;
    cout << "Enter the number:";
    cin >> a;
    cout << "Enter the operator like (+,-,*,/,%):";
    cin >> oper;
    cout << "Enter the number:";
    cin >> b;

    if (oper == '+')
    {
        sum = a + b;
        cout <<"Answer: " <<sum;

    }
    else if(oper == '-')
    {
        sum = a - b;
        cout << "Answer: " << sum;
    }
    else if(oper == '*')
    {
        sum = a * b;
        cout << "Answer: " << sum;
    }
    else if (oper == '/')
    {
        if (b==0)
        {
            cout << " Division is not Possible";
        }

        else
        {
            sum = a / b;
            cout << "Answer: " << sum;
        }

    }
    else if(oper == '%')
    {
        sum = a % b;
        cout << "Answer: " << sum;
    }
    else cout << "entered wrong input";

}
int main()
calculator();
    return 0;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.