Say whaat

Mar 14, 2011 at 7:40pm
Hey guys and girls,

I was creating a program the other day and ran into a very weird problem.

This is my code:
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>
using namespace std;


int main(){
	int primary = 0;
	string strings[3];
	
	cout << "Enter the first string.\n";
	cin >> strings[0];
	cout << "Enter the second string.\n";
	cin >> strings[2];

	while(primary != 1){
		
		cout << "Enter the new first string.\n";
		cin >> strings[1];
			if(strings[1] == strings[0]){
				cout << "The old and the new string match.\n";
		}
			else{
				cout << "The first string has been changed.\n";
				strings[0] = strings[1];
		}

		cout << "Enter the new second string.\n";
		cin >> strings[3];
			if(strings[3] == strings[2]){ // CONFUSION
				cout << "The old and the new string match.\n";
		}
			else{
				cout << "The second string has been changed.\n";
				strings[2] = strings[3];
		}
	}
}


Now the thing is, that every time I compile the code, and I enter the new second string, it shows me this:
Access violation writing location 0xcccccccc.


I've looked through the code like a 1000 times and I can't find anything wrong with it.

Any help is appreciated.
Mar 14, 2011 at 7:56pm
I changed the strings from an array to multiple independent strings, string1, string2, string3 and string 4, and now it works flawlessly. It's impossible to use arrays with strings, isn't it?
Mar 14, 2011 at 7:56pm
It's simple, there is no strings[3]. When indexing arrays, we start from 0. So an array with 3 elements has elements 0, 1 and 2. If you want to use strings[3] you need to declare strings with 4 elements.
Mar 14, 2011 at 7:57pm
strings[3]
doesn't exist if the size of the array is 3 ( since it's the 4th element )
Mar 14, 2011 at 8:05pm
Aah, gotcha. Thanks!
Topic archived. No new replies allowed.