How do you cin parameters?

This is a discussion on How do you cin parameters? within the C++ Programming forums, part of the General Programming Boards category; How do you create a function that takes user-defined parameters? For example, say I had a function: Code: int Example(int ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    6

    How do you cin parameters?

    How do you create a function that takes user-defined parameters? For example, say I had a function:

    Code:
    int Example(int x, int y){
    
    answer = x+y;
    
    return answer;
    
    }
    I understand I can cout << Example (4,6); to add 4 to 6; however, how do I allow a user to define the parameters before the function is called, i.e., make their inputs the parameters of the function?

    Is using a function that takes parameters defined by a user particularly useful in certain situations?

    PS. I am not trying to build a calculator; I was just using the above code as an example.

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Point your arrows the other way. You will need variables to store them to, and then call the function with those variables.

    (ETA: Or if you don't want variables in main, you can do the cin read in your function. But you'll still need variables to read into.)
    Last edited by tabstop; 07-15-2011 at 10:16 AM.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You may want to take a look here. There is a lot of good information.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Thanks for the link to the resources! I will be sure to go through the information; however, I just wanted to follow up on what tabstop said to see if I understood. This is what I came up with:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Abc{
    
    public:
        int Example(int x, int y){
    
        answer = x+y;
    
        return answer;
    }
    
    private:
        int answer;
    
    };
    
    int main()
    {
        int a;
        int b;
        Abc equation;
    
        cin >> a;
        cin >> b;
    
        cout << equation.Example(a,b);
    
    }
    Am I able to eliminate "int a" and "int b" from main somehow? I mean it works fine, but is it bad practice to have them there?

    Sorry for being noob

    Thanks again.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,266
    Quote Originally Posted by stariz View Post
    Thanks for the link to the resources! I will be sure to go through the information; however, I just wanted to follow up on what tabstop said to see if I understood. This is what I came up with:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Abc{
    
    public:
        int Example(int x, int y){
    
        answer = x+y;
    
        return answer;
    }
    
    private:
        int answer;
    
    };
    
    int main()
    {
        int a;
        int b;
        Abc equation;
    
        cin >> a;
        cin >> b;
    
        cout << equation.Example(a,b);
    
    }
    Am I able to eliminate "int a" and "int b" from main somehow? I mean it works fine, but is it bad practice to have them there?

    Sorry for being noob

    Thanks again.
    there's absolutely nothing wrong with having them there. in fact, it's where they belong in a program like yours. making them global (outside the scope of any class, namespace, or function) would be very bad. it is best to avoid global variables whenever possible. it is sometimes necessary to have them, but those situations are not nearly as common as the actual usage of global variables.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by stariz View Post
    Am I able to eliminate "int a" and "int b" from main somehow? I mean it works fine, but is it bad practice to have them there?

    Sorry for being noob

    Thanks again.
    What I meant was more on the lines of:
    Code:
    int Example() {
        int x, y;
        cin >> x >> y;
        int answer = x + y;
        return answer;
    }
    
    int main() {
        cout << Example() << endl;
    }
    where the variables move to the function instead.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    No, that's worse. You should have a function do either IO or computation, not both.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    However, the class Abc is total overkill.

    You want this:
    Code:
    int Example(int x, int y){
      answer = x+y;
      return answer;
    }
    
    int main()
    {
        int a;
        int b;
        cin >> a;
        cin >> b;
    
        cout << Example(a,b);
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Thanks everyone for the help; it is more useful than you know.

    Quote Originally Posted by CornedBee View Post
    However, the class Abc is total overkill.
    Yah, I was just trying to create a simple example that I could apply to my current project.

    I recently decided to take a second major in CS which will be in affect as of next semester (starting in 2 weeks) and couldn't wait to get started so I decided to learn C++ by creating a program that a friend could use for actuarial studies. I had originally just written it as a list of commands and although it worked quite well, I wanted to practice writing it properly. Although I suspect it's not a lot better, I have managed to make all the variables used in calculations private at least and all the calculations are organised into various classes in separate files. The only public variables are 5 strings that are used to navigate between the various menus with lists of equations.

    I asked about the function that could take user defined parameters as I was trying to separate the calculation and the interaction with the user somehow. For example, most of my functions look something like this:

    Code:
    void CompoundFunctions::PresentValue(){
    
        cout << "Size of regular payment (R): ";
    
        cin >> R;
    
        cout << endl << endl;
    
        cout << "Number of payments (n): ";
    
        cin >> n;
    
        cout << endl << endl;
    
        cout << "Interest rate (i) per period: ";
    
        cin >> i;
    
        cout << endl << endl;
    
        PV = R*((1 - pow((1 + i), -n) ) / i);
    
        cout.setf(ios::fixed);
    
        cout << "PV of annuity = " << setprecision(2) << PV << endl << endl;
    
    }
    My main() is pretty much a bunch of if statements within if statements within a do/while loop that call various functions such as the above.

    If anyones bored enough to take a look (or wants a laugh perhaps?), I would be happy to send the code to get your opinion on how to rewrite the program according to "proper" C++ OOP conventions.

    Or what would be a good type of program to attempt that would help me to learn OOP rules?

    Thanks again.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    OOP is not necessarily the best way to tackle any given program. And as a rough guideline, prefer function-local variables over member variables, even private ones.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parameters?
    By adam.morin in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2011, 06:01 AM
  2. How to get OS parameters
    By smboucou in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2008, 11:15 AM
  3. Help With Parameters
    By peckitt99 in forum C++ Programming
    Replies: 30
    Last Post: 10-29-2006, 02:18 PM
  4. Parameters
    By zach0616 in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2004, 03:26 PM
  5. Parameters
    By gvector1 in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21