LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-17-2012, 03:58 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, 04:05 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by carlosk711 View Post
I did it without using functions but now I'm trying to test them out
What's wrong with the code?
Variables have scope.

Declaring the same variable name in two different scopes gives you two different variables with the same name.

Have you learned about structures yet? Have you learned about passing by reference yet?

I don't want to do your homework for you. But most methods I could suggest (rather than demonstrate) probably involve techniques you don't know yet.

The common way to do the operation you described is to define a structure type that contains all of the results of the user() function, which are also all of the inputs of the output function. Then declare one instance of that structure in the main function and pass it by reference to the other functions.

That is, of course, not the only possible way to do it.

The simple, working but bad way to do it is to declare the individual variables once outside of all the functions, so they are global and accessible by all functions. That pretty much defeats the point of splitting the work into functions. But it does split the work into functions and it does work. In a sufficiently beginner assignment in a poorly taught programming class, that might be the expected answer.

Last edited by johnsfine; 09-17-2012 at 04:14 PM.
 
1 members found this post helpful.
Old 09-17-2012, 04:07 PM   #3
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
So how should it look?
 
Old 09-17-2012, 04:17 PM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by johnsfine View Post
I don't want to do your homework for you. But most methods I could suggest (rather than demonstrate) probably involve techniques you don't know yet.
I was still editing my post when you asked ...

Quote:
Originally Posted by carlosk711 View Post
So how should it look?
Sorry. That is not the way we are supposed to provide homework help. We are supposed to provide explanations of why what you tried didn't work. Sometimes suggestions are also appropriate. But doing the assignment for you is not appropriate.
 
Old 09-17-2012, 05:09 PM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,089

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
this mite help:
http://www.linuxquestions.org/questi...8/#post2155086
 
Old 09-18-2012, 04:24 AM   #6
liquid paper
LQ Newbie
 
Registered: Apr 2012
Location: Monroe, Washington, USA
Distribution: Linux Mint
Posts: 17

Rep: Reputation: Disabled
variables are only accessible within the same context from which they were declared.

so if i have two functions, both these variables actually access a different piece of memory:
Code:
int main()
{
  int x = 3;
}

void add5tox()
{
  int x;
  x+=5;
}
so basically if i were to set x in one function, the x variable in the other function would not change.
In order to have a variable accessible in two functions i would need to declare it in a context that is accessible from both functions, like the following:
Code:
int x;

int main()
{
  x = 3;
}

void add5tox()
{
  x+=5;
}
if i declare the x variable OUTSIDE the two functions, at the same level that the functions themselves are defined, then i am able to use it from both, so that if i change it with one function, that value is available to the other function.

This method, however, is not generally a good practice, as it does not create modular code (i.e. code that can easily be re-used in different situations, like with different values). For example, does not allow you to easily use your function with different inputs, because it always acts on the x variable.

You want your code to be modular, think of a game cartridge being plugged into a game system. To do something like this, you'd pass the variables by reference as arguments to the function. Although it may be a little confusing at first, requiring an understand of pointers (which can be rather tricky at first) it actually makes a lot more sense than doing it like above, and allows you to use your functions with any input you wish to pass to it:
Code:
int main()
{
  int x = 3,
      y = 1;

  add5(&x);
  add5(&y);
}

void add5(int *n)
{
  *n += 5;
}
You'll notice this is much cleaner and easier to read and understand than if you tried to do it using the method above, and allows me to write a single function to act on both variables:
Code:
int main()
{
  int x = 3,
      y = 1;

  add5tox();
  add5toy();
}

void add5tox()
{
  x+=5;
}

void add5toy()
{
  y+=5;
}
Now imagine how unpractical this would be if you were to have an array of 100 values and you wanted to add 5 to each... you'd need 100 functions, each acting on a different position in the array. Definitely not a good idea:
Code:
int x[100];

int main()
{
  add5tox0();
  add5tox1();
  add5tox2();
  ...
}

void add5tox0()
{
  x[0]+=5;
}

void add5tox1()
{
  x[1]+=5;
}

void add5tox2()
{
  x[2]+=5;
}
...
Now that, is definitely not how successful programs are written.

Last edited by liquid paper; 09-18-2012 at 04:36 AM.
 
1 members found this post helpful.
Old 09-18-2012, 04:42 AM   #7
liquid paper
LQ Newbie
 
Registered: Apr 2012
Location: Monroe, Washington, USA
Distribution: Linux Mint
Posts: 17

Rep: Reputation: Disabled
Also, instead of using a giant if block, i would use a simple switch-case statement instead:
Code:
char c;
switch
{
  case 'a':
    // do something when c=='a'
    break;

  case 'b':
    // do something when c=='b'
    break;

  default:
    // do something when nothing matches
    break;
}
 
Old 09-18-2012, 07:37 AM   #8
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by liquid paper View Post
To do something like this, you'd pass the variables by reference
...
Code:
...
  add5(&x);
...
void add5(int *n)

The original question was about C++, not C.

Whether "pass by reference" is an appropriate name in C for the technique you showed could be argued either way. But it is certainly not an appropriate name for that technique in C++.

C++ has pass by reference as a language feature. So the technique one should call "pass by reference" in C++ is actual pass by reference, not passing the address to a pointer.
 
Old 09-18-2012, 07:39 AM   #9
LQParsons
LQ Newbie
 
Registered: Feb 2010
Location: North of Boston, Mass (North Shore/Cape Ann)
Distribution: CentOS 6.0
Posts: 21

Rep: Reputation: 0
Emotional Response

Hi.
Now that your problem is resolved, in oh so many ways, at oh so many levels, will you permit an emotional response ?

Why do programmer have to pound everything with the hammer of arrogance? It seems, by your post and example, that you've got a complete naivete about this programming language (and forums), yet it seems the purpose of your function is to get to this cout:

Quote:
cout << "You are an idiot. This operator clearly does not exist. Try again, and do better" << endl;
Having dealt with programmers, old and new, for decades, dealt with customers and partners for decades, this chip-on-the-shoulder and "I'm the smartest person in the room" non-sense just makes it harder to get things done -- especially when the attitude generally is so unmerited; and those that merit rarely exhibit the attitude.

Like I said, this is just a emotional response to your query especially contrasted with the gentleness and quality of the help and advice you received.

Best to you continuing to sharpen programming skills.
 
Old 09-18-2012, 09:16 AM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,494

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
@LQParsons: when you consider that a program like this will generally only be run by its programmer, it could be an example of humility rather than arrogance.
 
  


Reply

Tags
c++, functions, linux, programing


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 04:54 AM
running functions on the same time (bash programming) sharapchi Programming 1 11-12-2006 11:55 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


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

Main Menu
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