mercredi 27 avril 2016

Adding a space to a linked list

I have been banging my head against a wall for the past few days figuring out how to add a space to the output after reading a file. The code reads from a file and outputs to the console "Ilikecomputers", when it should be printing out "I like computers". Any tips on how to add the space?

Thanks

The code is below

#include <iostream>
#include <list>
#include <ctype.h>
#include <fstream>

using namespace std;

void printList(const list<char> &myList);
void fillList(list<char> &myList);
void changeCase(list <char> &myList);

void printList(const list<char> &myList)
{
   list<char>::const_iterator itr;
   cout << "\nAfter Conversion: " << endl;
   for (itr = myList.begin(); itr != myList.end(); itr++ ) {
   cout <<*itr;
}
cout << '\n';
}

void fillList(list<char> &myList)
{
ifstream file("test.txt");
    string print;
    while(file >> print){
    for (int i = 0; i<print.length(); i++) {
    myList.push_back(print[i]);
  }
 }
}

int main ()
{
  list<char> myList;

  cout << "Before Conversion: " << endl;
  ifstream file("test.txt");
  string print;
  while(file >> print){    
  cout << print << " ";
 }

  fillList(myList);
  printList(myList);

 return 0;
}

Aucun commentaire:

Enregistrer un commentaire