how do i let he user input characters into a list

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <list>

using namespace std;

int main()
{
	char a[40];
	char charinput;
	//declare list
	list<char> myList(a[40]);
	list<char>::iterator it = myList.begin();
	
	cout << "Enter 5 lines: ";
	for (int i = 1;i<=5;i++)
	{
		cin >> charinput;
	}
		cout << i << ". " << endl;
		myList.insert(it,charinput);
		it++;
	
	return 0;
}


different sets of sentences
Hello seungyeon,

I would suggest that you compile your program before you post it here.

There are several problems with your program. Line 8 defines a char array but it is never used or initialized.

Line 11 defines a list, but I believe the end of the definition is incorrect. I looks like your idea is to define the list and set it equal to array "a". Not sure if that can be done. Either way the "[40]" is not needed, still have some testing to do.

Lines 19 - 21, just because they are indented does not make them part of the for loop. For that they need to inside the "{}" of the for loop. Since they are outside the for loop the variable "i" is undefined. Although "i" could be defined outside the for loop I am not sure what the point would be since lines 19 - 21 would work better inside the for loop.

I will have to play wit the program a bit to see if I can get it to work.

Hope that helps,

Andy
hi did a test

char only takes 1 character

changed char to string and my array
so my array

i know i can allow the user to input into arrays directly but how do i put it into my list into seperate nodes
Consider something like this:
1
2
3
std::copy(std::istream_iterator<char>{std::cin}, 
          std::istream_iterator<char>{},
          std::back_inserter(l));

Demo:
http://coliru.stacked-crooked.com/a/a7384e6411b53fca
Hello seungyeon,

Using what you started with I came up with this:

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
#include <iostream>
#include <string>
#include <list>

int main()
{
	std::string ary[40];  // <--- Changed to string.
	int len{ 0 };  // Length of what is stored in the ary.
	//declare list
	std::list<std::string> myList;
	//std::list < std::string > ::iterator it = myList.begin();  // <--- not needed right now.

	std::cout << "Enter 5 lines:\n" << std::endl;

	for (int i = 0; i < 5; i++)  // <--- Starting at 0 because arrys start at 0.
	{
		std::cout << i + 1 << ". ";  // <---Removed the endl.
		std::getline(std::cin, ary[i]); // <--- For using strings.
		len = i + 1;  // <--- + 1 because its a counter. Do not need 0.
		//it++;  // <--- Did not want work for me. Caused an error.
	}
	
	myList.assign(ary,ary + len);  // <--- Assign the arry to the list.

	// Prints outthelist/
	for (auto lp = myList.begin(); lp != myList.end(); lp++)
	{
		std::cout << " " << *lp << std::endl;
	}
}


I will admit my use of lists is limited, still have some learning to do. If you want to work with "char"s mbozzi's example looks good. See what you think, omeone else may have a better idea.

Hope that helps,

Andy
Hello seungyeon,

I do not use lists very often, so yesterday I became focused on the list member functions of ".insert: and ".assign", today I found that like vectors lists have a member function ".push_back". This will add to the end of the list just like a vector.

In my previous message replace lines 15 - 23 with this:

1
2
3
4
5
6
for (size_t lp = 0; lp < 5; lp++)
{
	std::cout <<lp + 1 << ". ";
	std::getline(std::cin, line);
	myList.push_back(line);
}


An easier way to populate "myList". This could also be used with a single character.

Hope that helps,

Andy
Topic archived. No new replies allowed.