On line 26, you assign name to curr->fname. Assuming curr->fname is an std::string, what you should do is curr->fname.append(&name) which will append the character name to the string curr->fname.
Just a shot in the dark but try flushing your cin buffer before you call person_add().
Also unless there is another conditional statement testing for decision being more the 3, you should delete that part of the while() loop, or just get rid of the while() loop and stick with consecutive if() statements. This is because jack***s like me waste 5 mins trying to see what you are doing when you really aren't accomplishing anything.
int main ()
{
int decision;
//Like a menu for what to do.
cout << "What do you want to do? Insert a number. " << endl << endl;
cout << "1. Add a person. " << endl;
cout << "2. Show content of the list. " << endl;
cout << "3. Removing content of the list. " << endl;
cout << "4. Exit. " << endl<<endl;
cin >> decision;
//while (decision >= 1 && decision <= 3) //Temporary commented.
{
if (decision == 1)
{
p_node* start = person_add();
}
if (decision == 2)
{
/*show_list(start);*/
}
if (decision == 3)
{
}
cout << "Choose a number. " << endl;
cin >> decision;
}
system ("Pause");
return 0;
};
Have not finished all of it and therefore I only writed the part I was on now, trying to figure out how to call my function and shouldn't I come to some input part?
Like putting in a name or something to the list? It only asks me for the number and then quit the program. Still have the same function definition as my first post.
Line 21 here makes me wish I could reach through the interwebs and slap you. I cringe when people declare variables in the middle of their code, but NEVER declare variables within a conditional statement.
Move Lines 32 and 33 so that they are above Line 19.
Line 21 here makes me wish I could reach through the interwebs and slap you. I cringe when people declare variables in the middle of their code, but NEVER declare variables within a conditional statement.
Assume that the pointer 'start' is going to be used elsewhere in the code. I choose '2' as my decision. The OP could redeclare it for every if statement but why let them get into that habit?
As for my issue with declarations in the middle of code. It's just a personal peev, no solid argument against it other then it bothers me.
Line 21 here makes me wish I could reach through the interwebs and slap you. I cringe when people declare variables in the middle of their code, but NEVER declare variables within a conditional statement.
I don't know if it's me or what? Thats not a declaration, not suppose to be. It is supposed to be a call to a function, and the function it is supposed to call is in my first post of this thread.
EDIT: See Line 25. Remove comments.
Its just a temporary comment, it should be code there later as I said. For example later on it should be another function call that calls the function to show the list.
From one beginner to another, why not use a switch statement here?
Thats a smart idea, but not really helping me with my main problem.
As I already said, my problem is that if I choose decision 1, it should call my function and somewhere in my function the user should input a firstname and a lastname who is gonna be added to a struct.
The problem is that cin >> does not read the newline when you first enter '1'. Therefore you should call cin.ignore(1) to get rid of the newline before calling cin.get, otherwise you will get '\n' as the first character.
The second potential problem you might have depending on what system you are on is buffered input. Some terminals might be line-buffered in which case cin.get will not return anything until you press ENTER, just so you know.