Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I got error that this program will execute for all operation i.e Add,Sub etc but i want to execute it only for single operation.
How i can do it?
and
Why scanf("%c",&op) is not working anywhere in program except first place?


C++
#include<stdio.h>
int add(int x,int y)
{
    int z;
    z=x+y;
    return z;
}
int multi(int x,int y)
{
    int z;
    z=x*y;
    return z;
}
int divi(int x,int y)
{

    int z;
    z=x/y;
    return z;
}

int sub(int x,int y)
{

    int z;
    z=x-y;
    return z;
}
main()
{
    int z;
    int firstv,secv;
    char op;
    printf("Please enter operator\n");
    scanf("%c",&op);
    printf("Please enter first value to calculation\n");
    scanf("%d",&firstv);
    printf("Please enetr second number\n");
    scanf("%d",&secv);
        if (op="+")
        z= add(firstv,secv);
        printf("The addtion of %d and %d is %d\n",firstv,secv,z);
        if (op="-")
        z=sub(firstv,secv);
        printf("The substration of %d and %d is %d\n",firstv,secv,z);

        if (op="*")
        z= multi(firstv,secv);
        printf("the multiplication of %d and %d is %d\n",firstv,secv,z);

        if(op="/")
        z=divi(firstv,secv);
        printf("The division of %d and %d is %d",firstv,secv,z);

        getch();
}
Posted
Comments
vishal2592 22-Mar-13 1:05am    
Why scanf("%c",&op) is not working anywhere in program except first place?
actually i want to used "enter operator in second number during program execution but it is not working in second place why?
vishal2592 22-Mar-13 1:16am    
I assign the value of char in scanf("%c",&ope); section?
[no name] 21-Mar-13 17:48pm    
You are doing assignment operations in your if statements not comparison operations.
Matt T Heffron 21-Mar-13 18:38pm    
Both Maciej Los and ThePhantomUpvoter's comments are correct.

Also, you are attempting to compare a char with a string.

An easy way to prevent falling into the "assignment in the if statement condition" is to put the constant to the left of the equality comparison:

if ('+' == op) ...

If you accidentally used the single "=" assignment operator, the compilation will fail.
vishal2592 22-Mar-13 1:05am    
Why scanf("%c",&op) is not working anywhere in program except first place?
actually i want to used "enter operator in second number during program execution but it is not working in second place why?

I don't know about your scanf issue.
With Visual Studio 2010, I tried this and it worked:
C++
#include<conio.h>
int main()
{
    int z;
    int firstv,secv;
    char op;
    printf("Please enter operator\n");
    scanf("%c",&op);
    printf("Please enter first value to calculation\n");
    scanf("%d",&firstv);
    printf("Please enetr second number\n");
    scanf("%d",&secv);
    if (op=='+')
    {
        z= add(firstv,secv);
        printf("The addtion of %d and %d is %d\n",firstv,secv,z);
    }
    if (op=='-')
    {
        z=sub(firstv,secv);
        printf("The substration of %d and %d is %d\n",firstv,secv,z);
    }
    if (op=='*')
    {
        z= multi(firstv,secv);
        printf("the multiplication of %d and %d is %d\n",firstv,secv,z);
    }
    if(op=='/')
    {
        z=divi(firstv,secv);
        printf("The division of %d and %d is %d",firstv,secv,z);
}
    _getch();
}

I fixed the assignment/comparison problem and changed the comparisons to be char not string (the compiler wouldn't compile it with strings in comparisons).
Try this and see what happens.
 
Share this answer
 
For the first look, you forgot about { and }

C#
if (condition statement)
{
//code block to execute if condition is true
//line 2
}
else
{
//code block to execute if condition is false
//line 2
}


You don't need { and } if you execute single line

More about: if[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Mar-13 20:15pm    
5ed.
—SA
Maciej Los 22-Mar-13 2:34am    
Thank you, Sergey ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900