LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying out functions for the first time, what am I doing wrong? (http://www.linuxquestions.org/questions/linux-newbie-8/trying-out-functions-for-the-first-time-what-am-i-doing-wrong-4175427707/)

carlosk711 09-17-2012 04:06 PM

Trying out functions for the first time, what am I doing wrong?
 
I appreciate any help that I can get:

Write a program that takes as input an arithmetic expression and verifies if it is correct. The user will input the expression in the following format:
operator num1 num2 num3
The program should now check if:
num1 operator num2 = num3

If it is correct the program should print:
num1 operator num2 = num3

If it is not correct the program should print:
num1 operator num2 != num3

I did it without using functions but now I'm trying to test them out
What's wrong with the code?

Code:

#include <iostream>
using namespace std;
void user ()
int output()
int main ()
{
    char op;
    int x, y, z ;
    user ()
    output ()
    return 0;
}

user ()
{
    char op;
    int x, y, z;
    cout << "Enter an arithmetic Expression:" ;
    cin >> op >> x >> y >> z ;
    return;
}

 output ()
{
    char op;
    int x, y, z ;
    if (op = '+' &&
    x + y == z)
    cout << x << "+" << y << "==" << z <<endl;
    else if ( op == '-' &&
    x - y == z)
    cout << x << "-" << y << "==" << z <<endl;
    else if ( op == '*' &&
    x * y == z)
    cout << x << "*" << y << "==" << z << endl;
    else if ( op == '/' &&
    x / y == z)
    cout << x << "/" << y <<"="<< z << endl;
    else if ( op == '%' &&
    x % y == z)
    cout << x << "%" << y << "==" << z << endl;
    else if ( op == '+' &&
    x + y != z)
    cout << x << "+" << y << "!=" << z << endl;
    else if ( op = '-' &&
    x - y != z)
    cout << x << "-" << y << "!=" << z << endl;
    else if ( op = '*' &&
    x * y != z)
    cout << x << "*" << y << "!=" << z << endl;
    x / y != z
    else if ( op = '/' &&
    x / y != z)
    cout << x << "/" << y << "!=" << z << endl;
    else if ( op = '%' &&
    x % y != z)
    cout << x << "%" << y << "!=" << z << endl;
    else
    cout << "You are an idiot. This operator clearly does not exist. Try again, and do better" << endl;

return int ;
}


kindofabuzz 09-17-2012 05:46 PM

missing a few ;

theNbomr 09-17-2012 06:11 PM

It would help us if you gave us at least as much as you already know about the problem. Does it compile? If no, what are the relevant error messages? If yes, then what does it do or not do that you consider incorrect?

One thing that jumps out at me is very unconventional formatting/indentation, but that does not affect function. Another thing that probably would make sense would be to convert the lengthy if-then-else clauses to a switch() structure. This is subject to style preferences, but in my opinion, this a place that is well suited to the switch() stucture.

As kindofabuzz points out, there are missing semi-colons.

--- rod.

Tinkster 09-17-2012 07:50 PM

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.


http://www.linuxquestions.org/questi...ng-4175427704/

Apart from the coding mistakes mentioned above you're double-posting. ;}
Big no-no, doing it wrong!


All times are GMT -5. The time now is 03:42 PM.