0

I am working on a homework problem. However, I am not sure how to correct the problem I am facing. You can ignore most of the code, I have gotten the first half to work, just trying to figure out how to make the map work.

I have tried this bad piece of coding.

 stringstream ss(str);
    map<string, int> words;
    int arrayLength;
    arrayLength = str.length();

    string userarray[arrayLength];

    for (int i=0;i<arrayLength;i++){
        string tempString;
        ss>>tempString;
        userarray[i] = tempString;
        words[userarray[i]]=i+1;
        if(words[userarray[i]]==1)
        {
            words.insert(make_pair(userarray[i],i++));
        }
        cout<< words.at(userarray[i]);
    }

This is all of my code

#include <algorithm>
#include <string>
#include <iostream>
#include <set>
#include <fstream>
#include <sstream>
#include <unordered_set>
#include <map>
#include <utility>
using namespace std;
int word()
{
   {
//str store the string
    string again = "y";
    string str= "",checkStr;
    cout<<"Enter the line : ";
    getline(cin,str);
    checkStr = str;
    checkStr[str.length()-1]=' ';

    while(again =="y")
    {

//store all the tokens
        set<char> tokens;
        unordered_set<char> token;
//if characters are not in range consider as token
         for (int i=0;i<str.length();i++)
        {
            char inChar = str[i];
            if(inChar>='A' && inChar<='Z' || inChar>='a' && inChar<='z' )
            {

            }
            else
            {
                if(str[i]!=' ' && str[i]!='\n' )
                tokens.insert(inChar);
                token.insert(inChar);
            str[i] = ' ';
            }
        }
//print the tokens
         set <char> :: iterator pointer;

        cout<<"tokens are : "<<endl;

        for (pointer = tokens.begin(); pointer != tokens.end(); ++pointer)
            {
            cout<<*pointer<<" ";
            }

        cout<<endl;

//print the tokens
        cout<<"unordered set: "<<endl;
        for(auto it = token.begin(); it != token.end();it++)
            cout<< *it<< " ";
        cout<< endl;

//store string to stream

        stringstream ss(str);
        map<string, int> words;
        int arrayLength;
        arrayLength = str.length();

        string userarray[arrayLength];

        for (int i=0;i<arrayLength;i++){
            string tempString;
            ss>>tempString;
            userarray[i] = tempString;
            words[userarray[i]]=i+1;
            if(words[userarray[i]]==1)
            {
                words.insert(make_pair(userarray[i],i++));
            }
            cout<< words.at(userarray[i]);
        }

    cout<<"Press 'y' to run again: "<<endl;
    getline(cin,again);
    if(again =="y")
    {
    return 1;
    }
    else
    {
    cout<<"Function terminated"<<endl;
    exit(2);
    }

    }
}
}
int main ()
{
    while (word()==1)

    {
    word();
    }
    while(word()==2)
    {
    break;
    }
}

My problem is that when I cout the code, I get a long list of numbers.

However, if I enter "Have a good day. Have a good class. “+” Have a good visit. Have fun!”

I want the output to look like

have 4 a 3 good 3 day 1 class 1 visit 1 fun 1

I have since gotten my code to work, my friend helped me with the last bit today. The code is...

stringstream ss(str);
    map <string,int> dict;
    map <string,int> dict_counted;
    string line;

    // create an array of strings
    string array[100];

    int counter = 0;
    while (getline(ss,line,' '))
    {
        array[counter] = line;
        counter++;
        if (dict.count(line))
        {
            dict[line]+=1;;
        }
        else
        {
        dict[line] = 1;
        }
    }


    for (int i=0;i<counter;i++)
    {
        if (dict_counted.count(array[i]))
        {

        }
        else
        {
            if (!(array[i].empty()))
            {
                cout<<"The frequency of "<<array[i]<<" is "<<dict[array[i]] <<endl;
                dict_counted[array[i]] = -1;
            }
        }

    }
Mitch Urn
  • 1
  • 1
  • this section makes compilor error: arrayLength = str.length();string userarray[arrayLength]; this link explains why https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard , you used variable length array. – samini Nov 05 '19 at 05:24

1 Answers1

0

I changed your third for a little

for(int i = 0; i<arrayLength; i++) 
{
    string tempString;
    ss >> tempString;
    if (tempString.empty())
        continue;
    words[tempString] = tempString.length();
    cout << tempString << " " <<words[tempString]<<" ";
}
samini
  • 195
  • 11