Is getline causing my program to crash why?

the user inputs two strings to compare. The first string is the matching string and the second string is the string that will compared with the first string. the first character in the second string could be anything; The rest have to be in order. for example, the matching string is net and the comparing string is xet. The program returns net.

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

using namespace std;

int main()
{
    string word1[81]={" "};
    string word2[81]={" "};

    cout << "Enter two words and I will compare them to see if there ";
    cout << "is a match. " << endl;

    cout << "*\nNote, the first character in the second word could be ";
    cout << "anything. " << endl;

    getline(cin, word1[81]);
    getline(cin, word2[81]);

    word2[0].swap(word1[0]);

    cout << word1 << endl;
    cout << word2 << endl;

    return 0;
}
Last edited on
if you are comparing two strings why have you declared 2 arrays that each hold 81 strings?
because remember, each element(or character) except the first has to match. So I want to step through each element in the string array to check if there is match. This is also why I'm swapping the first element in the second string with the first element in the matching string since the first character could be anything. Regardless, the problem that I have is that it's crashing.
Last edited on
because remember, each element(or character)


well that says it all then. You want to declare an array of characters rather than an array of strings then?

I think you're getting confused between an array of characters (i.e. ONE string), which I think you need, with an array of strings (which you've defined here).
If, leave my code like this, it crashes immediately. Why is that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string word1[81];

    cout << "Enter two words and I will compare them to see if there ";
    cout << "is a match. " << endl;

    cout << "\n*Note, the first character in the second word could be ";
    cout << "anything. " << endl;

    getline(cin, word1[81]);
    
    cout << word1;


    return 0;
}
string word1[81];
i'll say it again... why do you need 81 strings?

Unless i'm missing something, you just need this:

1
2
    string word1("");
    string word2("");


then can use
http://www.cplusplus.com/reference/string/string/compare/
and/or then you can use at()
http://www.cplusplus.com/reference/string/string/at/
and replace()
http://www.cplusplus.com/reference/string/string/replace/
to do what you want to do.
Last edited on
Alright mutexe I understand, but what I want to know is why does my program crash immediately.
Last edited on
Just use two strings, you don't need 81 strings, like mutexe said.

char hello[100]; is a character array ( single string) that is 100 characters long
string hello[100]; is 100 different strings
If, leave my code like this, it crashes immediately. Why is that?
On line 16: The index 81 is out of bounds. Allowed indexes: 0...80

As mutexe said string is an array of char. You don't want an array of strings

You can access the char like so
1
2
3
4
5
6
string word1; // Note: not an array of string
...
getline(cin, word1);
...

char ch = word1[0]
Last edited on
because of this line:

getline(cin, word1[81]);

you are asking the user to type in a string and put it in the 'last' element of your string array, but remember if you declare an array with 81 elements, you access them from word1[0] to word1[80]. word[81] does not exist.

in other words it's not liking it because you're trying to write to memory that's not your array.
Thank you
no worries dude anytime.
Topic archived. No new replies allowed.