Comparing two arrays

Ok, I highlighted my problem area at the end of my code in bold (line 103 on). What I'm trying to do with that function is to compare my two arrays. If the text entered (for option 2) matches the code in the originally entered text ('text' array), then I am supposed to output that there is a match and the location of the match (starting character # in the array).

My current code keeps giving me a positive output at line '0'. I have made multiple adjustments, but just can't seem to find a way to compare the two and get feedback on it. I tried test-running multiple parts of the code, but each individual piece seems to do what I want, so I'm at a loss as to what is going wrong here? If anyone can help, I would greatly appreciate it.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<fstream>
#include<cstring>

using namespace std;

void MainMenu ();
int findText (char t[], char s[]);

string options = "-1";
char text[141]={0};
char searchText[141];
int compare=1;
int location=0;
int searchLength=0;

int main()
{
    MainMenu();
    while (options!="0"){ // keeps loop running until user enters 0

        if ((options=="2" || options=="3" || options=="4") && text[0] == 0){  // need text first error
            cout << "Please enter text using option 1 first.\n\n";
            MainMenu();
        }

        if (options != "0" && options != "1" && options != "2" && options != "3" && options != "4"){  // invalid options error
            cout << "Wrong choice.\n\n";
            MainMenu();
        }

        if (options=="1" && text[0]==0){ // option 1 - enter text
            cout << "Please enter text (up to 140 characters): ";
            cin.ignore();
            cin.getline(text,140);
            MainMenu();
        }

        if (options=="1" && text[0]!=0){  // replace text confirmation
            cout << "You have already entered text.  Are you sure you want to change it?\n";
            cout << "Press Y (y) to continue: ";
            char confirm='f';
            cin >> confirm;
                if (confirm == 'y'){
                cin.ignore();
                cout << "Please enter text (up to 140 characters): ";
                cin.getline(text,140);
                cout << endl;
                MainMenu();
                }
                if (confirm != 'y')
                    cout << endl;
                    MainMenu ();
        }

        if (options=="2" && text[0]!=0){  // option 2 - searching for text   NOT FINISHED!!!!
            cout << "Please enter search text (up to 140 characters): ";
            cin.ignore ();
            cin.getline(searchText,141);
            findText (text, searchText);
            if (compare=0){
                cout << "The text was not found." << endl << endl;
                MainMenu();
            }
            if (compare=1){
                cout << "The text was found at location " << location << ".";
            }
            cout << endl << endl;
            MainMenu ();
        }

//  only worked on here above

        if (options=="3" && text[0]!=0){ // option 3 - appending text  NOT FINISHED!!!
            cout << "Please enter text to be appended: ";
            cout << searchText <<  endl;
            cout << searchText << endl;
            MainMenu ();
        }

        if (options=="4" && text[0]!=0){  // option 4 - letter counter  NOT FINISHED!!!
            // int j= strlen (searchText);
            char j;
            j=searchText[2];
            cout << j << endl << endl;
            MainMenu ();
        }
    }
}

void MainMenu (){  //  main menu function
    cout << "Main Menu\n" << "1. Enter text.\n" << "2. Search for text.\n" << "3. Append text.\n";
    cout << "4. Analysis of text.\n" << "Press 0 to quit.\n" << "Please make a choice: ";
    cin >> options;
    cout << endl;
}

int findText (char t[], char s[]){  //  find text function
    int size = strlen (searchText);
    int inc=0;
    char x=text[0];
    char y=searchText[0];
    while (location<140){
        for (int i=0; i<=size; i++){
            if (i==size){
                compare=1;
                return compare;
                return location;
                break;
            }
            if (x!=y){
                compare=0;
                return compare;
                inc=inc+1;
                x=text[inc];
                y=searchText[inc];
                location=location+1;
                return location;
            }

        }
    }
}
Last edited on
Line 112, 113, and 114 are your problems. You can't return multiple things from a function- once line 112 is called, the function breaks. 113 will never be called, and the break becomes useless.
Ok, so then what I would want to do instead of using return would be to use a cout that simply prints the value to the screen, right? I know my code needs some other tweaks too, but I want to make sure I'm headed in the right direction here.

To add... If I'm not mistaken, for any global values I work with in the function (outside of the values in the 1st line of the function), they would be changed everywhere in the program, correct?
Last edited on
closed account (48T7M4Gy)
Why not return a bool to indicate whether it was found or not? The location can be passed by reference back to main in this case. All you need is the location, if it exists.
 
bool findText (char t[], char s[], int& location)
Last edited on
While I appreciate the feedback, I'm still having issues with my code overall. Using a bool would definitely clean it up a bit, but it won't solve my problems I'm having right now.

This is what I put together, but I keep getting incorrect answers with it:

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
int findText (char t[], char s[]){  //  find text function
    int size = strlen (searchText);  // max to compare in order to have a match
    int incx=0;  // value used to compare place in text
    int incy=0;  // value used to compare place in searchText
    int incx1=0;  // value used to compare text after a comparison fails
    char x=text[0];
    char y=searchText[0];
    location=0;
    while (location<140){
        int i=0;
        for (int i=0; i<size; i++){
            if (i==size && x==y){
                compare=1;
                cout << "The text was found at location " << location << "." << endl << endl;
                return compare;
            }
            if (x!=y){
                compare=0;
                incx1=incx1+1;
                y=searchText[0];
                x=text[incx1];
                location=location+1;
            }
            if (x==y)
                compare=1;
                incx=incx+1;
                x=text[incx];
                incy=incy+1;
                y=searchText[incy];
        }
    }
    cout << "The text was not found." << endl << endl;
    return compare;
}
Last edited on
closed account (48T7M4Gy)
So, the best idea is now to show us the errors. The way to do this is give a sample input, then the actual output, and then ... what you expect

As another tip I'd suggest you write a special test driver. Instead of giving us all your main() code just a relevant extract to test the function helps isolate problems rather than wading through it all. :)
Good to know for future reference. Thank you.

I actually figured out my problem myself. I made a few different tweaks and got it working.
Topic archived. No new replies allowed.