trying to compair two strings

I'm trying to create a program that will compare two phrases. The requirement is that it has to use a loop to compare the two phrases character by character.
It then has to tell the user whether or not the phrases are equal. When I enter the phrase incorrectly it responds as expected, prints "The statements are not equal.". But, when I enter the phrase correctly it does not print anything.

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

string str1 = "Piece of Cake"; //assigns model phrase as string str1
string str2; // string class for user input


int i;

int main ()
{
    cout << "Please retype the following phrase: Piece of Cake" << endl;
    getline (cin, str2 );

    for ( int i=0; i<str1.size(); i++)
    {

        if (str1.at(i) == str2.at(i))
        {
        continue;
        }

        else if (str1.at(i) != str2.at(i))
        {
        cout << "The statements are not equal." << endl;
        break;
        }

        cout << "The statements are equal." << endl;

    }

return 0;
}
Start with a safety check. What if str1.size() != str2.size() ?

You cannot say that all are equal before you have seen all. In other words, the "are equal" must print after the loop.

When you break out of loop because the characters don't match, you will end up to "after the loop" too. What to do? How about a variable, where you record the good/bad, and use that to decide what to print?
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
#include <iostream>
#include <string>
#include <iomanip>

#include <algorithm>


using namespace std;

string str1 = "Piece of Cake"; //assigns model phrase as string str1
string str2; // string class for user input
//why global variables??

int i; // what is this i doing??

int main ()
{
    cout << "Please retype the following phrase: Piece of Cake" << endl;
    getline (cin, str2 );
    transform(str2.begin(), str2.end(), str2.begin(), ::tolower); //little something to make it understand capital and small letter both to be equal
    transform(str1.begin(), str1.end(), str1.begin(), ::tolower);

    for ( int i=0; i<str1.size(); i++)
    {

        if (str1.at(i) == str2.at(i))
        {
        cout << "The statements are equal." << endl;
        break;
        }

        else if (str1.at(i) != str2.at(i))
        {
        cout << "The statements are not equal." << endl;
        break;
        }

    }

return 0;
}


the problem was the continue statement. when the string were equal continue made it jump to the end at return 0;
so it didnt print anything. once you put that inside the loop it works out just fine.
Last edited on
@lonelylense: Consider input "Patrik". Your program will tell that it is equal to "Piece of Cake". That is IMO a bit worse than OP's version.

You are right though that the continue is part of the problem.


Another note:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if ( cond )
{
 // A
}
else if ( ! cond )
{
  // B
}

// is equivalent to:
if ( cond )
{
 // A
}
else
{
  // B
}

If cond on line 1 is true, then lines 5-8 will not be evaluated.
If cond on line 1 is false, then !cond is true. There is no reason to evaluate it again.
Topic archived. No new replies allowed.