-2

I am basically trying to store everything after a certain index in the array.

For example, I want to store a name which is declared as char name[10]. If the user inputs in say 15 characters, it will ignore the first five characters and store the rest in the char array, however, my program crashes.

This is my code

char name[10];
cout<< "Starting position:" << endl;
cin >> startPos;
for(int i= startPos; i< startPos+10; i++)
{
  cout << i << endl; // THIS WORKS
  cout << i-startPos << endl; // THIS WORKS
  name[i-startPos] = name[i]; // THIS CRASHES
}

For example, if my name was McStevesonse, I want the program to just store everything from the 3rd position, so the end result is Stevesonse

I would really appreciate it if someone could help me fix this crash.

Thanks

Daniel
  • 6,595
  • 9
  • 38
  • 70
Sam Thers
  • 67
  • 3
  • 12
  • If `name` declared as `name[10]`, and `startPos > 0`, then `i` will get larger than 10, and so `name[i]` will access beyond the allocated space. The code you posted is not very clear in that context, as you describe half of it by words, not in the code. – Misch Dec 02 '14 at 20:48
  • 2
    How do you store the 15 characters of the user input into the `char name[10]` variable? When you know the answer to this question, you will understand what the problem is. Unfortunately, this part of your program is not in your question, so I cannot answer this question. However, I can tell you that it is impossible to store 15 characters into an array of 10 `char`s without causing "undefined behavior" which often translates to crashes later on. – Frederic Lachasse Dec 02 '14 at 20:53
  • The code you've provided doesn't seem to pertain to the question you've asked. `If the user inputs in say 15 characters ...`; I don't see your program asking for the user's name anywhere. Please rephrase your question and/or include an [MCVE](http://stackoverflow.com/help/mcve). – Julian Dec 02 '14 at 21:13

3 Answers3

1

Suppose i is equal to 3. In the last iteration of the loop, i is now equal to 12, so substituting 12 in for i, your last line reads

name[12-startPos] = name[12];

name[12] is out of bounds of the array. Based on what you have shown so far, there is nothing but garbage stored in name anyway before you start doing this assignment, so all you're doing is reorganizing garbage in the array.

Daniel
  • 6,595
  • 9
  • 38
  • 70
0

Please in future: post full compilable example. A simple answer is that your array maybe is out of bound, since you don't provide full example its hard to know exactly.

Here is a working example:

#include <iostream>
using namespace std;

int main() {
int new_length, startPos;
int length = 15;
char name[15]= "McStevesonse";

cout<< "Starting position:" << endl;
cin >> startPos;
if(new_length <1){ // you need to check for negative or zero value!!!
    cout << "max starting point is " <<length-1 << endl;
    return -1;
}
new_length=length-startPos;
char newname[new_length];
for(int i= 0; i<new_length; i++){
  newname[i] = name[i+startPos]; // THIS CRASHES
}
cout << "old name: " <<  name << " new name: " << newname << endl;
return 0 ;
}
Califlower
  • 467
  • 4
  • 15
0

To put it simply, change this:

for(int i= startPos; i< startPos+10; i++)


To this:

for(int i= startPos; i<10; i++)


You should be fine with that.



Explanation:

At some point, when you use the your old loop, this name[i-startPos] = name[i] would eventually reach an array index out of bounds and causes the crash.

Don't forget to clean up/hide the garbage:
Doing so, would cause the output to produce some kind of garbage outputs. If you got a character array of 'ABCDEFGHIJ', and have chosen 3 as the starting position, the array would be arranged to 'DEFGHIJHIJ'. In your output, you should atleast hide the excess characters, or remove by placing \0's

ChinoCarlo
  • 28
  • 4