Thread: Rewrite using functions.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Rewrite using functions.

    Rewrite the following program using two functions, one for input and one for output. Use the function declarations given.

    Code:
    #include <iostream>
    
    using namespace std;
    
    string input ();
    void output ( string value );
    
    int main ()
    {
        string name;
    
        cout << "Enter your name\n";
    
        cin >> name;
    
        cout << "Hello " << name << endl;
    
        system ("PAUSE");
        return 0;
    }

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I did your first one. We won't do all your homework. Take the initiative and read your book/tutorials. We aren't here to do everything.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Then why are you here?

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    To help those who show effort. I already did one of these for him, I'm not doing all his homework. In other words, I already helped him. This is just a plea for help without any effort shown.
    EDIT: In the other one you included string and didn't use it, but in this one you use string but don't include string. If you had compiled this you would understand this.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Well if you could help that'd be awesome. I've been going around this problem for a bit now..I just don't know how to use the declarations that are setup. You dont have to tell me the exact code, just tell me what to do. I just want to know how to work with the declarations that are given..I don't know if that made much sense.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    It did, and I'm fine with helping you. You want to have input cout the message and get the variable, returning the cin'ed messaged into a string. For output you want to cout "Hello" and then the function argument. Hope that helped!
    EDIT: Add this line at the top:
    Code:
    #include <string>
    Also, sorry about being mad. I guess you're different from the other newbie whiners that seem to frequent these forums.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    So I tried something like this but it didn't work. We just started learning funcitons so I'm not 100% with them.

    But heres the sh**ty code I came up with:
    Code:
    #include <iostream>
    
    using namespace std;
    
    string input ();
    void output ( string value );
    
    int main ()
    {
        string input()
        {
        string name;
    
        cout << "Enter your name\n";
    
        cin >> name;
        }
        system ("PAUSE");
        return 0;
    }
    
    void output(){
    cout << "Hello " << name << endl;
    }
    I don't want you guys to do the problem for me because I wont learn jack. Just try and explain like you did above.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    #include <string> under iostream. At the end put string value in the parentheses, as your function declarations don't match. Then cout value. Actually, you'll probably learn more if I show you how to do this:
    Code:
    #include <iostream>
    #include <string>          //Need this for string
    
    using namespace std;
    
    string input ();
    void output (string value);
    
    int main ()
    {
        string thename = input();
        output(thename);              //Need this to get name. Notice the placement of input.
        system ("PAUSE");
        return 0;
    }
    
    string input()          //Move outside main
    {
        string name;
        cout << "Enter your name" << endl;
        cin >> name;
        return name;
    }
    
    void output(string value)          //Get the value in a parameter.
    {
        cout << "Hello " << value << endl;
    }
    Now look over this and try to understand the changes I made. You'll learn more this way. Good luck
    Last edited by manutd; 11-15-2006 at 08:09 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM