Thread: simple functions

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    30

    simple functions

    we are starting to learn about functions now and we have a simple function that we have to do. i was looking through my book, but all the examples were using int and not string (which is what i think i need).
    the description of the problem is: Write a function called myName which returns (does not cout) your full name (e.g., "Bob Smith"). Write main to call the function and display the name on the screen. NOTE: Your name should not appear anywhere other than in the function myName.
    so far i have something looking like this:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string myName()
    {
        return "users name";
    }
    
    int main()
    {
        myName;
    }
    i know this is a very short and simple program but im not sure if im on the right track.

    thanks for the help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are on the right track, but you need to actually call the function and print the string returned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    what are you referring to when you say i need to 'call' the function? and how will i be able to print the string returned without cout? (i am assuming that the 'return' command has something to do with this).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To call a function, you must use paranthesises - myfunction().
    Then you need to assign the return to a variable in main to print it out later.
    It's the same way with integers.

    You are on the correct path.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    Here's what I would do, in pseudo code, so you can learn from it:

    Code:
    #include <iostream>
    #include <string> //Use this for strings
    
    using namespace std; //Makes it so you don't have to use std:: in front of a ton of commands
    
    //Declare function prototype
    string functionname(function arguments);
    //The arguments are what it brings in. You'll use void there, I think
    
    int main(){
        declare a string;
    
        string = functionname(); //That calls the function, and the string equals whatever it returns (a string)!
        
        cout their name;
        wait for a key to be pressed - Do not use system("pause")!!!
        
        return 0 to main();
    }
    
    string functionname(arguments){
        declare a string;
        
        Ask for their name;
        Get their name - getline(cin, stringname); //Where stringname is the string that the text goes into
        
        return the string to main()
    }
    Cheers
    Last edited by Lorgon Jortle; 03-31-2009 at 06:39 PM. Reason: I made my code a little more understandable.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    the only thing is that the user isnt entering their name. its a program that we have to write which when we run it, our name just automatically comes up. also, we arent supposed to use cout. the professor said that these next few assignments should be really quick if we understand what we are doing... and apparently i dont. hah.

    thanks again.

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    You're quite close. You just need to add a few things. "cout" will be used in main to display the return value of the function. I think your teacher meant don't use "cout" within the function that is returning your name. So I'm sure it's fine to use it in main().

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    string myName()
    {
        return "users name";
    }
    
    int main()
    {
        cout << myName();  //This will print the return value of the function. In this case, users name
    
        return 0;
    
    
    }
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread safety for tiny simple functions
    By CodeMonkey in forum C++ Programming
    Replies: 16
    Last Post: 12-31-2008, 12:20 AM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Second Program
    By cyberCLoWn in forum C++ Programming
    Replies: 28
    Last Post: 12-08-2003, 11:48 AM