In great need for help of c++programing :) thanks vey much!! SIXPENCE.txt: Sing
ID: 3738539 • Letter: I
Question
In great need for help of c++programing :) thanks vey much!!
SIXPENCE.txt:
Sing a song of sixpence,
A pocket full of rye;
Four and twenty blackbirds
Baked in a pie.
When the pie was opened,
They all began to sing.
Now, wasn't that a dainty dish to set before the King?
The King was in the countinghouse,
Counting out his money;
The Queen was in the parlor
Eating bread and honey.
The maid was in the garden,
Hanging out the clothes.
Along there came a big black bird
And snipped off her nose!
Explanation / Answer
here is your program : ----------->>>>>>>>>>>
#include<iostream>
#include<fstream>
#include<vector>
#include<sstream>
using namespace std;
int locateWord(string word,int n,vector<string> words){
for(int i = 0;i<words.size();i++){
if(word == words[i]){
if(n == 0){
return i+1;
}else{
n--;
}
}
}
return -1;
}
int loadFile(vector<string> &words,string file){
words.clear();
ifstream ifs;
ifs.open(file.c_str());
if(ifs.is_open()){
string str;
while(!ifs.eof()){
ifs>>str;
words.push_back(str);
}
ifs.close();
return 1;
}else{
cout<<" File Loading Error !!!";
return -1;
}
}
void newWords(vector<string> &words){
words.clear();
}
int main(){
vector<string> words;
stringstream ss;
string load = "load";
string newcom = "new";
string locate = "locate";
string end = "end";
string command;
string com1;
int para = 0;
int ch = 0;
while(ch != 9){
cout<<" > ";
getline(cin,command);
ss.clear();
ss<<command;
ss>>com1;
if(com1 == load){
ch = 1;
}else if(com1 == newcom){
ch = 2;
}else if(com1 == locate){
ch = 3;
}else if(com1 == end){
ch = 9;
}
switch(ch){
case 1:
{
com1 = "";
ss>>com1;
if(com1 == ""){
cout<<" ERROR:Invalid Command";
break;
}
int ch1 = loadFile(words,com1);
if(ch1 == -1){
cout<<" command failed !!! ";
}
break;
}
case 2:
{
newWords(words);
break;
}
case 3:
{
char *p;
string word;
ss>>word;
ss>>com1;
int nt = strtol(com1.c_str(),&p,10);
if(*p == 0 && nt > 0){
int count = locateWord(word,nt,words);
if(count == -1){
cout<<" No matching entry";
break;
}else{
cout<<" "<<count;
}
}else{
cout<<" ERROR:Invalid Command";
break;
}
}
}
}
}