A
agent349
First off, I know arrays can't be compared directly (ie: if (arrary1
== array2)). However, I've been trying to compare two arrays using
pointers with no success. Basically, I want to take three sets of
character strings from the user. Then I want to run through each
element and compare the two strings. If they match I print they
match... I'm having a bit of trouble with the actual loop through each
array using the pointers and comparing the characters.
Here's what I have so far... Thanks for any tips you can provide!
______________________________________________________
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
char s[10]; //array 1
char *sPtr; //pointer 1
char ss[10]; //array 2
char *ssPtr; //pointer 2
for (int c=1; c<=3; c++) { //looping 3 times
cout << "Enter first string of 10 characters:";
cin >> s;
cout << "Enter second string of 10 characters:";
cin >> ss;
for (int i=0; i<10; ++i) { //loop through each element
if ( *sPtr == *ssPtr ) { //here's the problem...
cout << "The entered strings are equal.\n\n";
}
else if (s != ss) {
cout << "The entered strings are not equal.\n";
}
}
}
return 0;
}
== array2)). However, I've been trying to compare two arrays using
pointers with no success. Basically, I want to take three sets of
character strings from the user. Then I want to run through each
element and compare the two strings. If they match I print they
match... I'm having a bit of trouble with the actual loop through each
array using the pointers and comparing the characters.
Here's what I have so far... Thanks for any tips you can provide!
______________________________________________________
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
char s[10]; //array 1
char *sPtr; //pointer 1
char ss[10]; //array 2
char *ssPtr; //pointer 2
for (int c=1; c<=3; c++) { //looping 3 times
cout << "Enter first string of 10 characters:";
cin >> s;
cout << "Enter second string of 10 characters:";
cin >> ss;
for (int i=0; i<10; ++i) { //loop through each element
if ( *sPtr == *ssPtr ) { //here's the problem...
cout << "The entered strings are equal.\n\n";
}
else if (s != ss) {
cout << "The entered strings are not equal.\n";
}
}
}
return 0;
}