I wrote a program that compares two strings and tests to see if they are the same. Case difference doesn't matter. The problem is this: When the program is run it works only half the time. The other half the program doesn't wait for the second string to be entered, it just skips that part and compares the strings. I can't figure out why it is doing this.

Please help.

Also any comments to tighten up my code would be appreciated.

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char str_arr1[80], str_arr2[80];
    int x;
    
    cout << "Please enter your first string\n";
    cin >> str_arr1;
    
    cout << "Please enter your second string\n";
    cin >> str_arr2; 
    
    //Converts all the characters in the first array to lower case
    for(x=0; str_arr1[x]; x++) str_arr1[x] = tolower(str_arr1[x]);
    
    //Converts all the characters in the second array to lower case
    for(x=0; str_arr2[x]; x++) str_arr2[x] = tolower(str_arr2[x]);
    
    if(!strcmp(str_arr1, str_arr2)) 
       cout << "The strings MATCH\n";
    else 
       cout << "The strings DO NOT MATCH\n";
       
    system("pause");
    return 0;
}

What happens when you compile it?

What happens when you compile it?

It complies completely. The only problem is when it runs it sometimes skips the second request for the string.

What type of string are you trying to enter? If the first string has whitespace in it (eg, Jim Jones instead of Sally) then the behavior you describe could occur.

What type of string are you trying to enter? If the first string has whitespace in it (eg, Jim Jones instead of Sally) then the behavior you describe could occur.

I want the user to be able to enter and string up to 79 characters long including white spaces. I store the strings in and array. Here is that piece of code.

char str_arr1[80], str_arr2[80];
    int x;
    
    cout << "Please enter your first string\n";
    cin >> str_arr1;
    
    cout << "Please enter your second string\n";
    cin >> str_arr2;

When i compiled it, it works fine, you just have to match the strings

The problem seem to occur when I enter a string with a space in it. Do you know why this would cause my program to not run properly?

I have fixed your problem, you can now have spaces!

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char str_arr1[80], str_arr2[80];
    int x;

    cout << "Please enter your first string\n";
    cin >> str_arr1;
    char a[50];
    std::cin.getline(a, 50);


    cout << "Please enter your second string\n";
    cin >> str_arr2;

    //Converts all the characters in the first array to lower case
    for(x=0; str_arr1[x]; x++) str_arr1[x] = tolower(str_arr1[x]);

    //Converts all the characters in the second array to lower case
    for(x=0; str_arr2[x]; x++) str_arr2[x] = tolower(str_arr2[x]);

    if(!strcmp(str_arr1, str_arr2))
       cout << "The strings MATCH\n";
    else
       cout << "The strings DO NOT MATCH\n";

    system("pause");
    return 0;
}

Compile this and see if it works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.