Before anything else, I seriously suggest properly indenting your code, because I think it keeps causing you confusion.
Where does your for loop (the one that starts on line 17) end? (PS: Your code doesn't compile.)
Again, I don't think you need your stringstream, it seems to only be causing you errors.
On line 13, you create 'in' when your data string is blank.
So line 22 never loops.
I think we are at the (often encountered) stage of the proceedings where it would be a good idea to post a copy of the question being asked in your assignment.
Another good move would be to apply some planning to this exercise and write down some simple pseudocode to what is going on here.
A twenty-questions style of providing help, and everybody is keen to do it, is not the best way of going about this.
Please post a copy of the assignment question in its entirety :)
#include <iostream>
#include <map>
#include <string>
int main()
{
std::map<std::string, int> word_map;
std::map<std::string,int>::iterator iter;
std::cout << "Please enter number of words: ";
int no_words{0};
std::cin >> no_words;
std::string input;
for(int i = 0; i < no_words; i++)
{
std::cout << "Enter a fruit: ";
std::cin >> input;
iter = word_map.find( input );
if ( iter != word_map.end() )
{
iter->second++;
std::cout
<< input << " already encountered "
<< iter->second << " times\n";
}
else
word_map.insert({input,0});
}
// MANGLE REPEATED WORDS
int count{0};
std::string word;
for(auto it:word_map)
{
count = it.second;
if(count > 0)
{
it.second = 0; // RESET BASE WORD COUNT
for(int i = 0; i < count; i++)
{
word = it.first + std::to_string(i + 1);
word_map.insert({word, -1}); // -1 TO PREVENT RE-USE
}
}
}
// DISPLAY UNIQUE WORDS
std::cout << "The unique words are:\n";
int unique_count{0};
for(auto it:word_map)
{
if( it.second >= 0 )
{
std::cout << it.first << '\n';
unique_count ++;
}
}
std::cout << "No. of unique words: " << unique_count << '\n';
// DISPLAY MANGLED WORDS
std::cout << "The mangled words are:\n";
for(auto it:word_map)
{
if(it.second == -1)
{
std::cout << it.first << '\n';
}
}
return 0;
}
Please enter number of words: 5
Enter a fruit: Banana
Enter a fruit: Orange
Enter a fruit: Apple
Enter a fruit: Banana
Banana already encountered 1 times
Enter a fruit: Orange
Orange already encountered 1 times
The unique words are:
Apple
Banana
Orange
No. of unique words: 3
The mangled words are:
Banana1
Orange1
Program ended with exit code: 0
My bad. I assumed that you are not a copy-paste-programmer-sans-comprehension.
You had:
1 2 3
vector <string> sentence;
string input;
while ( cin >> input ) { // read from istream "std::cin"
I don't want to (re)type the test data, so I read the data from a different istream:
1 2 3 4 5 6
const string data = "Banana Mango Apple2 Apple Orange Apple Apple";
std::istringstream in( data );
vector<string> sentence;string input;
std::map<string, unsignedint> counter_of_each_word;
while ( in >> input ) { // read from istream "in"
If you want to read from cin, then do so.
1 2 3 4 5 6
const string data = "Banana Mango Apple2 Apple Orange Apple Apple";std::istringstream in( data );
vector<string> sentence;
string input;
std::map<string, unsignedint> counter_of_each_word;
while ( cin >> input ) {
That loop does not have to run till the end of input: while ( no_words < sentence.size() && cin >> input ) {
Your indentation (at least as I see your posts) is not systematic. When it is not systematic, it is hard to see scopes. againtry did mention Python, where systematic indentation is mandatory.
All I'm asking is correction on my code but all I get people arguing about something and it's really confusing sorry for my language I don't English that much.
Also I'm just new to this and I learn through example of other people codes
Seems there's no help here
Anyways thanks
nameslist is an array, it does not have a a "nameslist.length". Use no_of_names instead.
Line 24: What are you trying to do here?
1. You're trying to add nameslist[j], a string, with count, an int. Addition between a string and an int is not defined.
2. You're also using =, which is assignment. I assume you are trying to compare two things, so == should be used. But again, I'm not sure what you're trying to compare.
Did you mean to simply do if (nameslist[i] == nameslist[j])?
Arrays with non-compile-time lengths are not allowed in standard C++, although GCC has an extension for them. Use a vector or new[]/delete[].