how to compare input with array element

hey guys;
i have program that i should get string input from the user check if it is in the array if not store the string in it and if it already in the array i should count the number times the string. for example in this string:
"i'm the one, i'm the man "

i should print i'm= 2
the = 2
one = 1
man = 1

here is my codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

struct Word
{
    string words;
    int amounts;
};

void readData(Word wordsarray[], int& size);
int main () {
    int max = 100;
    int size = 0;
    Word wordsarray [max];
    readData( wordsarray,size );
        
        int j= 0;
        while(j < size)
        {
             cout<< wordsarray[j].words <<" :" ;
            cout<< wordsarray[j].amounts<< endl;
            ++j;
        }
    
    
}
// read data, store a new word into an array if 
//the word is already in the array i increase the 
void readData(Word wordsarray[], int& size)
{
    Word temp;
    cout << "enter a word"<< endl;
    
    while(temp.words != "#")
    {
       
        cin >> temp.words;
        temp.amounts = 0; 
        // check if there is the given word in the array
        if( temp.words == wordsarray[size].words.find(temp.words))
        {
             ++temp.amounts; 
             cout<<"i m in the if cond"<< endl; 
        }  
        
         wordsarray[size]= temp;// if there is not we stored the given word 
         ++size;
         cout<<"i m out the if cond"<< endl; 
        
    }

}


i m problem is at the line 43 the if condition i m stuck here i need some help
thank for your help
Last edited on
You should use char[] instead of string for the userinput, and then use
someting as find() to find the spaces and seperate the input in different words.
When the userinput is stored in a string, only the first word is stored:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

int main()
{
    
string input;
cin>>input;
cout<<input;

return 0;
}


This would return "Hello" by an input of "Hello world!"
Uhhhhh....WHAT??? You DO realize that string has a .find() function >.> If you want to store an entire string of input, just use get(cin, string);
thank you guy for answer but i need to work with string and i thing i solve it already. thank you for your answer

have a nice day
@firdraco
Yes, sorry, you're right. A stuppid beginnersmistake from my side.
Topic archived. No new replies allowed.