String Arrays will not work

I've been having trouble using string arrays.
After some experimenting, I've brought it down to the simplest possible code which should work.

1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<string>
using namespace std;
int main()
{
   string names[1];
   cin >> names[1];
   cout << names[1];
   return 0;
}


There is no actual output, as the program fails, resulting in a "Program has stopped working" popup - ya' know, the default Windows one with the never-ending loading bar.

This is displayed in the compiler window afterwards, if it is relevant.

----jGRASP wedge2: exit code for process is -1073741819.
----jGRASP: operation complete.


I suspect this has something to do with my compiler/environment settings?
I've checked around on other sites to make sure I have formatted correctly, and it should work.
Last edited on
Arrays are indexed starting from zero

 
string names[1];

creates an array of strings of size 1. You only have one string in your array. If you want to access the first element, you need to access names[0].

Your computer shut down your program because you tried to write out of bounds.
Topic archived. No new replies allowed.