1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ function problem

  1. Oct 6, 2008 #1
    In our C++ class, we're learning to use functions. In this problem I am working on is to use 4 functions: 3 functions to get your first name, last name, middle initial, and one function to display the your full name.

    I wrote the program, but can't get it to run. I've looked over it several times and I don't see where I went wrong. Can anyone see where the error is?

    Oh, I wrote it using Visual C++ 2008 Express Ed.


    #include <iostream>
    #include <string>
    using namespace std;

    string GetFirstName();
    string GetLastName();
    char GetMiddleInitial();
    void DisplayFullName(string firstName, string lastName, char middleInitial);

    int main()
    {
    string firstName;
    string lastName;
    char middleInitial;

    firstName = GetFirstName();
    lastName = GetLastName();
    middleInitial = GetMiddleInitial();

    DisplayFullName(firstName, lastName, middleInitial);

    system("pause");
    return 0;
    }

    string GetFirstName()
    {
    string firstName;
    cout << "Enter your first name: ";
    cin >> firstName;
    return firstName;
    }

    string GetLastName()
    {
    string lastName;
    cout << "Enter your last name: ";
    cin >> lastName;
    return lastName;
    }

    char GetMiddleInitial()
    {
    char middleInitial;
    cout << "Enter your middle Initial: ";
    cin >> middleInitial;
    return middleInitial;
    }

    void DisplayFullName(string firstName, string lastName, char middleInitial)
    {
    cout << "Your full name is " << firstName << " "
    << middleInitial << ". "
    << lastName << ".";
    }
     
  2. jcsd
  3. Oct 6, 2008 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Posting the problem / error message might help.
    I suspect you want to look at std::getline since you are on windows
     
    Last edited: Oct 6, 2008
  4. Oct 6, 2008 #3
    Here's the what the output window says when I run it...

    1>Linking...
    1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
    1>C:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2008\Projects\a\Debug\a.exe : fatal error LNK1120: 1 unresolved externals
    1>Build log was saved at "file://c:\Documents and Settings\HP_Administrator\My Documents\Visual Studio 2008\Projects\a\a\Debug\BuildLog.htm"
    1>a - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
     
  5. Oct 6, 2008 #4
    Maybe you told Visual Studio to use the precompiled header, but then removed the references to it.

    With Visual Studio, you need to uncheck "use precomiled header" when you make a project, or ensure that you keep all the information that Visual Studio includes for you in the blank project.

    When I copy and pasted it, it ran fine. If you are using the precompiled header, ensure that you use #include "stdafx.h" and you should probably use int _tmain(int argc, _TCHAR* argv[]) for the main function.

    If you uncheck "include precompiled header" when you first make the project, you do not have to include the stdafx.h library.
     
  6. Oct 6, 2008 #5
    Yes...that's exactly what it was. When I made a new project for this I must have forgotten to check the 'Empty Project' box. I wouldn't have thought to check that, thank you.

    I kept going over the code many times, it was driving me all kinds of crazy.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ function problem
Loading...