LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 09-17-2012, 04:06 PM   #1
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Rep: Reputation: Disabled
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 ;
}
 
Old 09-17-2012, 05:46 PM   #2
kindofabuzz
Member
 
Registered: Mar 2010
Location: There
Distribution: Linux Mint 17.1
Posts: 237

Rep: Reputation: 46
missing a few ;
 
Old 09-17-2012, 06:11 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 09-17-2012, 07:50 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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!
 
  


Closed Thread

Tags
c++, error, function, programming



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syslog time wrong - but date returns the correct time? falc410 Linux - Newbie 7 04-05-2020 10:04 PM
Trying out functions for the first time, what am I doing wrong? carlosk711 Programming 9 09-18-2012 09:16 AM
time functions in a c program mbacke Programming 3 04-18-2005 02:42 PM
my time is wrong and calender is also wrong Paxmaster Linux - General 6 12-16-2004 12:46 AM
Setting System Time: kernel in wrong time zone warrenweiss Linux - General 7 05-15-2004 03:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration