Hellp programmers!
I am working on a program that uses two objects of a custom class template List [which is a linked list, with each item pointing to the next] and concatenates them.
Initially, I need to input numbers into my main function, so I have two sets of while loops (while an EOF sequence is not entered) to enter a number into the loop.
Here's my code so far:
// Program that takes two linked list objects and concatenates
#include <iostream>
#include "List.h"
using namespace std;
int main()
{
//create two linked lists
List<int> firstList;
List<int> secondList;
//add values to each list
int input=0;
cout << "Created firstList and secondList." << endl;
cout << "\n\nEnter a number to put into firstList (EOF to quit): ";
while (cin >> input)
{
firstList.insertAtBack(input);
cin.ignore();
cout << "Enter a number to put into firstList (EOF to quit): ";
}
cout << "\n\nThe contents of firstList object of type List are: " << endl;
firstList.print();
cout << "\n\nEnter a number to be put into secondList (EOF to quit): ";
while (cin >> input)
{
secondList.insertAtBack(input);
cin.ignore();
cout << "Enter a number to be put into secondList (EOF to quit): ";
}
cout << "\n\nThe contents of secondList object of type List are: " << endl;
secondList.print();
cout << "\n\n\nConcatenating two lists together." << endl;
//concatenate using a function
}
The first loop works well, but after the statement cout << "\n\nEnter a number to be put into secondList (EOF to quit): ";
, the program skips the cin in the loop's decision, goes on without ever running it.
Here's a sample output of what happens:
Created firstList and secondList.
Enter a number to put into firstList (EOF to quit): 1
Enter a number to put into firstList (EOF to quit): 2
Enter a number to put into firstList (EOF to quit): 3
Enter a number to put into firstList (EOF to quit): 4
Enter a number to put into firstList (EOF to quit): 5
Enter a number to put into firstList (EOF to quit): 6
Enter a number to put into firstList (EOF to quit):
The contents of firstList object of type List are:
The list is: 1 2 3 4 5 6
Enter a number to be put into secondList (EOF to quit):
The contents of secondList object of type List are:
The list is empty.
I use cin.ignore() to flush the '\n' character from the buffer stream, but it seams that it is not working. Can you help?