LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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, 05: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, 06:46 PM   #2
kindofabuzz
Member
 
Registered: Mar 2010
Location: Memphis, TN
Distribution: Linux Mint 13, Ubuntu Server 10.04
Posts: 232

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

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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, 08:50 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 5 11-09-2013 05:54 AM
Trying out functions for the first time, what am I doing wrong? carlosk711 Programming 9 09-18-2012 10:16 AM
time functions in a c program mbacke Programming 3 04-18-2005 03:42 PM
my time is wrong and calender is also wrong Paxmaster Linux - General 6 12-16-2004 01:46 AM
Setting System Time: kernel in wrong time zone warrenweiss Linux - General 7 05-15-2004 04:25 PM


All times are GMT -5. The time now is 07:23 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration