Solving C++ Function Problem in Visual C++ 2008 Express Ed.

In summary: Can you tell me where I went wrong?In summary, the program does not seem to be working as it should. Can anyone see where the error is?
  • #1
rezeew
1,257
0
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 << ".";
}
 
Physics news on Phys.org
  • #2
Posting the problem / error message might help.
I suspect you want to look at std::getline since you are on windows
 
Last edited:
  • #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 ==========
 
  • #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.
 
  • #5
vociferous said:
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.

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.
 

1. How do I create a C++ function in Visual C++ 2008 Express Edition?

To create a function in Visual C++ 2008 Express Edition, you need to follow these steps:

  1. Open your project in Visual C++
  2. Right-click on the Source Files folder and select "Add" > "New Item"
  3. Select "C++ File (.cpp)" and name your file
  4. In the new file, type your function definition, including the return type, name, and parameters
  5. Save the file and your function will now be available for use in your project

2. How do I call a C++ function in my code?

To call a C++ function in your code, you need to follow these steps:

  1. Make sure the function is declared in the same file or a header file that is included in your code
  2. Use the function's name followed by parentheses and any required arguments
  3. If the function returns a value, assign it to a variable or use it in an expression

3. How do I debug a C++ function in Visual C++ 2008 Express Edition?

To debug a C++ function in Visual C++ 2008 Express Edition, you can use the built-in debugger by following these steps:

  1. Set a breakpoint in the function you want to debug by clicking on the line number in the code editor
  2. Run your program in debug mode by pressing F5
  3. The debugger will pause at your breakpoint and you can step through the function using the toolbar or keyboard shortcuts
  4. You can also inspect variables and watch expressions to track the values in your function

4. How do I pass arguments to a C++ function?

To pass arguments to a C++ function, you need to include the parameters in the function definition and provide corresponding values when calling the function. For example:

void printSum(int num1, int num2) {
  int sum = num1 + num2;
  cout << "The sum of " << num1 << " and " << num2 << " is " << sum;
}

int main() {
  int x = 5;
  int y = 10;
  // call the function and pass x and y as arguments
  printSum(x, y);
  return 0;
}

5. How do I return a value from a C++ function?

To return a value from a C++ function, you need to specify the return type in the function definition and use the return keyword to return a value. For example:

int add(int num1, int num2) {
  return num1 + num2;
}

int main() {
  int x = 5;
  int y = 10;
  // call the function and assign the returned value to a variable
  int sum = add(x, y);
  cout << "The sum of " << x << " and " << y << " is " << sum;
  return 0;
}

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
949
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
30
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
758
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
Back
Top