string help

im trying to make a rpg game and im using stings so the user can enter his/her name...but when the user enters her name all it prints back is numbers when its suposed to print the name the user entered...how do i fix it?

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

using namespace std;

int main()
{
    int yourname [20];
    int strike;
    int shoot;
    int heal;
    int yourhealth;
    int comphealth;

    cout << "Enter your name: ";
    cin >> yourname [20];
    cout << yourname [20];

    cin.get();
    return 0;
}
int yourname[20] to char yourname[20]
yourname is an int array. yourname[20] is an int and it's out of bounds. I guess what you tried to do was
1
2
3
4
char yourname [20];
...
cin >> yourname;
cout << yourname;


Why not use std::string instead?
Last edited on
out of bounds
Good catch I didn't look past the declaration once I saw it was an int array!
Last edited on
Topic archived. No new replies allowed.