First and Last Name

I've tried to create a program that asks you to enter your first and last name, it then enters your first name on one line and your last on the next one. If you didn't enter your first and last name it says that you didn't. To do that I used namn.find(' ') (namn is the name of my string) to find if there are any spaces in what you entered. If there aren't (if someone as an example entered their name as "PeterPan") it outputs a strange number, that seems to be the same every time: 4294967295. But is it really the same every time? Maybe it outputs another number on another computer, because it seems like a bit strange number for an "error". Because if it does, my program won't work on other computers. So is there any more effective way of doing this program?

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

int main(){
    string namn;
    int space;
    cout << "Enter your first and last name: "; getline(cin, namn);
    if(namn.find(' ') == 4294967295){
        cout << "You did not enter your first and/or last name!" << endl;
    space = namn.find(' ');
    string first(namn, 0, space);
    auto length = namn.size()-first.size();
    string last(namn, space+1, length);
    cout << "First name: " << first << endl;
    cout << "Last name: " << last << endl;
Last edited on
Check the explanation of string::find.

If no matches were found, the function returns string::npos.

if(namn.find(' ') == string::npos)

Thanks! I've also noted that if you do not enter your name the first time, but the second time the program surprisingly outputs this:

Enter your first and last name: Charlie
You did not enter your first and/or last name!
Enter your first and last name: Charlie Sheen
First name: Charlie
Last name: Sheen
First name:
Last name:

Does someone have any idea why it does that?
Last edited on
Because you call main() from within your if statement.

your code should never call main, unfortunately I see this more and more on these forums.

when i say never call main() , i really mean NEVER CALL MAIN!!!!

you are not only asking for trouble, you are asking for it recursively ;)

Fine. Do you have an explanation for why one never should call main()? I mean, why does shit like this happen when one does it?
Last edited on
firstly, you are calling the function that you are already in, it can take a bit of experience to fully appreciate what goes on when that happens, look up "recursion".

What is happening is that the first half of your code executes, then it executes main() all over again via the call to main, when that completes the original run is continued.

so it runs like...

run1 -> Enter your first and last name: Charlie
run1 ->You did not enter your first and/or last name!
run2 ->Enter your first and last name: Charlie Sheen
run2 ->First name: Charlie
run2 ->Last name: Sheen
run1 ->First name:
run1 ->Last name:

second, and most important with main(), it is the starting point for your program, this is the routine that your operating system will call when it loads your program. It was never intended for us devs to call it, it is a pre-known name that the system can find and execute in order to run the program.

As you no doubt noticed the second reason is really a good practice/bad practice decision, your code would have worked if you had a "return 0" after the call to main. but would have still been frowned upon by other devs regardless of OS or compiler.
You explained that really well, I understand now. So you shouldn't even call main() from other functions? I'm not even sure if they're called functions but what I mean is this:

void something(){
Last edited on
Simple answer: It's illegal. It's against the C++ standard because C++ was designed to use the function called main() as the defacto entry point for the program itself. Calling it AGAIN after it's already running will restart your program without the proper preparation of the heap, stack, etc. This will almost surely result in undefined behavior. The real question here is: "What reason would you possibly have for wanting to call main() once it's already running?" It's like trying to start your car again as you 're driving down the highway. There's just no point.
And the real answer is not to put any functionality in main(), just check for command line args etc, then call out to your real functions.

int main()

void something(){

this would cause something called mutual recursion, even worse than normal recursion.

because the OS called main() which called something() which called main() which calls something() which calls..... you get the picture ;)
Last edited on
Topic archived. No new replies allowed.