Comparing Arrays

I guess this has somehow slipped right by me but I cannot seem to figure out at all why when I compare these two arrays like so, it doesn't work:

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstring>

using namespace std;

int main ()
{

  const int STR_MAX = 80 //I set this to a random big #. is this the problem?
  
  char user_word[STR_MAX],
      firstword[STR_MAX] = "firstword",
      secondword[STR_MAX] = "secondword";

  
  cout << "Input your first word!" << endl;
  cin >> user_word;

  if (user_word == firstword)
    cout << "Yep! It's the same!" << endl;
  else
    cout << "Nope! Wrong word!" << endl;

  return(0);

}



This is an example of what I did to compare. I know arrays dont work like that with the "==" but i was to understand that when its a string, it does work.

I tried also adding a bool variable that will be set to false if it isnt the same word and i set it using a for loop that checks the entire array one by one. Still didnt work.

I know it has to be something simple but I keep overlooking it i guess. Any help? Thank you

PS - Yep I'm a newbie to the extreme i bet haha
PSS - EDIT - changed int to char, that was a typing mistake
Last edited on
Why are you using arrays of int?

Also I do believe that the name of an array is just a pointer to the first "block" of the array, so if I'm correct your doing nothing, but comparing the address of the arrays.

I tried also adding a bool variable that will be set to false if it isnt the same word and i set it using a for loop that checks the entire array one by one. Still didnt work.


That sounds like the right direction to take with this sort of thing.

This is an example of what I did to compare. I know arrays dont work like that with the "==" but i was to understand that when its a string, it does work.


You can use C++ strings like that yes, and I would recommend the string type for this yes.

There are two types of strings: C style strings, which use arrays to hold their chars and C++ strings which is actually a class, but might as well be a basic type(like an int, or char). The reason your porbley better off using a C++ string( declared like this:
string mystring= "hi") because of the equality operator being overloaded and... on second thought I guess thats the only reason...


EDIT: 100th

EDIT: sorry if I'm wrong about anything, and don't quote me on any of this...

Last edited on
that was my bad. in my program the arrays are chars

yeah thats what i thought. im gonna try figuring it out through a for loop by comparing each part of the array one by one. i did it before but it didnt work. im gonna try again and post new code. thanks
1
2
3
4
5
6
//line 22
if ( (string)user_word == firstword ) //turn (cast) the c string into a c++ string
//   ^^^^^^^^
    cout << "Yep! It's the same!" << endl;
  else
    cout << "Nope! Wrong word!" << endl;
okay here is another problem thats more relevant to my actual program rather than the example in the OP

i have five different words set like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

int main ()
{

    const int STR_MAX = 80;

    char  firstword[STR_MAX] = firstword,
            secondword[STR_MAX] = secondword,
            thirdword[STR_MAX] = thirdword,
            fourthword[STR_MAX] = fourthword,
            fifthword[STR_MAX] = fifthword,
            fileword[STR_MAX];


}



i have a code already that reads in a file which has the firstword, secondword etc in it and also has some different words. so that word goes into the fileword array and is compared to the other arrays. i want to write code that compares the arrays and then states if it is the same or not.

for example if the first word is "firstword" it is placed in fileword and then compared to the other arrays. since it is the same as the firstword array, it states they are the same. if the first word was sixthword for example , then that is placed into fileword and is compared. since there is no sixthword array, it states that the words are not the same.

i tried writing the for loop w/ bool variable but i cannot do it right i guess i confuse my && and || or a count variable is wrong idk. Can anyone just tell me where to start first of all to compare so many arrays to one other and stuff like that. thank you
1
2
3
4
5
6
bool isSame = true;
for( int i = 0; i < LENGTH; i++ ) //loop though the array
  if( input[i] != array[i] ) { //if the elements (char's in this case) are not equal 
    isSame = false;
    break; //we are done, no need to check the rest of the array
  }
Last edited on
Use std::string and operator==.
For char arrays, use std::equal.

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/algorithm/equal/
Topic archived. No new replies allowed.