STL print everything and swap 2 lines if there is 2 lines

my list doesnt print anything

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <list>
#include <iterator>

using namespace std;

const int NUM_LINES = 5;

int main()
{

	string input;
	list <string> myList;
	list <string>::iterator it = myList.begin();
	
	int i = 1;
	int repeat;
	do{
//input does the input store it into the list?
                if(repeat == 1)
		{
				cout << "Enter a line: ";
				cin >> input;
 			   	myList.push_back(input);
		}
//print, but doenst print anything
		if(repeat == 4)
		{
			while( it != myList.end() ) 
   			{
			   cout << *it << endl;
			   it++;
			}
		}
//swap
		if(repeat == 5)
		{ 
			if(myList.size() >= 2)
			{
				list<string>::iterator first = myList.begin();
				first++;
				list<string>::iterator second(first);
				second++;
				swap(*first, *second);
			}
			else
				{
					cout << "Not enough lines to swap." << endl;
				}
		}

	cout << endl;
	cout << "1 to INSERT, 2 to DELETE, 3 to DELETE whole list, 4 to PRINT, 5 to SWAP, or any number other "; 
	cout << "than the numbers that was initialized to quit: ";
	cin >> repeat;
	}while(repeat >= 1 && repeat <= 5);
	
	return 0;
}
Last edited on
i ahve added comments to the if statements for better understanding
ok i figured how to print.
now i need help on the swap
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <list>
#include <iterator>

using namespace std;

const int NUM_LINES = 5;

void inputf();
void deleteone();
void deleteall();
void print(list<string> &L);
void swap();

int main()
{
	string input;
	list <string> myList;
	list <string>::iterator it = myList.begin();
	
	int repeat;
	do{
		//input line block
		if(repeat == 1)
		{
                        int lines;
			cout << "How many lines do you want to enter: ";
			cin >> lines;
			for(int ins = 0; ins < lines; ins++)
			{
				cout << "Enter a line: ";
				cin >> input;
 			   	myList.push_back(input);
 		
		 		if (myList.size() > 5)
 		   		{
 		   			cout << "List is full. Cannot add more to list, please delete a line." << endl;
 			   		repeat = 2;
				}
			}
		}
		//print block
		if(repeat == 4)
		{
			print(myList);
		}
		//swap block
		if(repeat == 5)
		{
			int firstLine;
			int secondLine;
			
			if(myList.size() >= 2)
			{
				list<string>::iterator first = myList.begin();
				first++;
				list<string>::iterator second(first);
				second++;
				swap(*first, *second);
			}
			else
				{
					cout << "Not enough lines to swap, add a line." << endl;
				}
		}

	cout << endl;
	cout << "1 to INSERT, 2 to DELETE, 3 to DELETE whole list, 4 to PRINT, 5 to SWAP, or any number other "; 
	cout << "than the numbers that was initialized to quit: ";
	cin >> repeat;
	}while(repeat >= 1 && repeat <= 5);
	
	return 0;
}
void print(list<string> &L)
{
	cout << endl; 
    
	list<string>::iterator inx = L.begin();      //inx = 5; // Can only assign iterator values 
	while( inx != L.end() ) 
	{
	cout << *inx++ << endl;         // Prints the value the list is pointing to CURRENTLY
	}                    
}
Last edited on
i noticed that all my computations happen inside of the if statements

so it doesnt carry over
so if i use a global function passed by reference i can do all this computations

its like declaring a integer for 1 if statement and you want to use it for another statement

//don't do's
1
2
3
4
5
6
7
8
9
10
11
12
int x = 4;

if(x > 2)
{
int i;
cin >> i;
}
if(x > 2)
{
i +=2;
cout << i;
}
Topic archived. No new replies allowed.