[quote]
Write a programme which reads in text a line at a time, splits e
ach line into
words, and puts words in a vector of queues where there is one q
ueue for each
word length and one for excessively long words, if any. Take l
engths to be
between 1 and 12, inclusive. Print the words from each queue.[/quote]
main.cpp
[code]#include "split.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
int main(){
std::ifstream infile;
infile.open("readthis.txt", std::ifstream::in); //opening a input file stream
std::vector<std::queue<std::string>> VQS(13);
std::string getter, gotter; // gets the contents of the text file
std::vector<string> wds;
std::vector<string> nonwds;
const std::string delimiters= " - / . ? ! \n"; //the things we don't want in our new stack/queue/priority queue/et c...
while(infile.good()){ //reads a line into getter, adds that line to gotter
getline(infile, getter);
gotter+= getter;
}
wds=split(gotter, delimiters);
for(unsigned int i=0; i < wds.size(); ++i)
{
if (wds[i].size() < 13)
VQS[wds[i].size() - 1].push(wds[i]); //should push word of size x into queue number x
else
VQS[12].push(wds[i]); //pushes the word into queue cell 13 if the length is greater than 12
}
std::cout <<"The number of each queue is also how long the words are in that that queue. \n" << '\n';
for(unsigned int i=0; i < VQS.size(); ++i) //this determines which queue is being shown
{
unsigned int qsize= VQS[i].size(); // cant use size in the for loop since it will be decreasing at it is popped.
std::cout << "The contents of queue "<< i+1 <<" are as follows: ";
for (unsigned int j=0; j < qsize; j++) //this displays the element j of the ith queue
{
std::cout << VQS[i].back() <<' ';
VQS[i].pop();
}
std::cout << '\n' << '\n';
}
return 0;
}[/code]
split.h
[code]#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <stack>
#include <queue>
using std::string;
using std::cout;
std::vector<string> split (const string & s, const string & delimiters);[/code]
split.cpp
[code]
#include "split.h"
using std::string;
std::vector<string> split (const string & s, const string & delimiters)
{
std::vector<string> v;
string::size_type pos1, pos2=0;
string word;
pos1=s.find_first_not_of(delimiters, pos2);
while(pos1 != string::npos)
{
if((pos2=s.find_first_of(delimiters, pos1)) == string::npos)
{
word=s.substr(pos1);
pos1=pos2;
}
else
{
word=s.substr(pos1,pos2-pos1);
pos1=s.find_first_not_of(delimiters, pos2);
}
v.push_back(word);
}
return v;
}[/code]
readthis.txt
[code]
The quick brown .fox jumps over the lazy !dog.[/code]
Ok, so this time it's supposed take a word, and put that word in a queue according to its length. If it had the word brown, it should add brown to the queue of 5. However, this is my output.
[code]The number of each queue is also how long the words are in that that queue.
The contents of queue 1 are as follows:
The contents of queue 2 are as follows:
The contents of queue 3 are as follows: dog dog dog dog
The contents of queue 4 are as follows: lazy lazy
The contents of queue 5 are as follows: jumps jumps jumps
The contents of queue 6 are as follows:
The contents of queue 7 are as follows:
The contents of queue 8 are as follows:
The contents of queue 9 are as follows:
The contents of queue 10 are as follows:
The contents of queue 11 are as follows:
The contents of queue 12 are as follows:
The contents of queue 13 are as follows:
Press any key to continue . . .
[/code]
any idea on whats causing this?
There is a help thread. No need to create two separate threads.
[QUOTE=IliekBoxes;42240606][code] std::cout << VQS[i].back() <<' ';
VQS[i].pop();[/code][/QUOTE]
The back of the queue is the newest element (so the element that will be popped by the last call to pop()) and the top of the queue is the oldest element (so the element that will be popped by the next call to pop()).
The suggestions I made in the other thread of yours also mostly apply :)
Sorry, you need to Log In to post a reply to this thread.