Input by function

I have this declaration:

p_node* person_add();

This is my function call:

1
2
3
4
5
6
while (decision >= 1 && decision <= 3)
{
   if (decision == 1)
   {
      person_add(); //function call.
   }


And here is my function definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
p_node* person_add()
{
   p_node* start = NULL,
	 * curr = NULL,
         * prev = NULL;

   int nodeNum = 1;
   char name;

   name = cin.get();

   while (name != '\n')
   {
	prev = curr;
	curr = new p_node;

	if (nodeNum == 1)
	{
		start = curr;
	}
	else
	{
		prev->next = curr;
	}

	curr->fname = name;
	curr->next = NULL;
	nodeNum++;
	name = cin.get();
    }
return start;

}


I can't make any input if I call the function? Its supposed to make an input there.
I see your mistake, and it's an easy one to make.

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.
But when I choose "decision" 1 in "the menu", it should call the function to input a name.

Now when I choose it, its just asking for what "decision-number" and then finishing the program without making any input for the name by the user?
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.
Flush my cin buffer? Im just a beginner, don't know what that means?

Here is my main code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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.

Compile run and post results.
Last edited on
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.


What exactly is the problem with that?
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.

EDIT: See Line 25. Remove comments.
Last edited on
From one beginner to another, why not use a switch statement here?

1
2
3
4
5
6
7
8
9
10
11
int decision;
switch(decision)
{
case 1:
//  code
break;
case 2:
// code
break;
// ....
}
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.
Topic archived. No new replies allowed.