-2
void adde(int& v, char array[5])
{

    if (v > 5) {
        v = -1;
    }

    for (int k = 0; k < 5; k++) {
        if (array[k] == 'C') {
            array[k] = '-';
        }
    }
    v++;
    array[v] = 'C';
}

this is my function

int mov = -1;
char item[5];
for (int i = 0; i < 5; i++) {
    item[i] = '-';
}
cout << "Initially " << endl;
for (int i = 0; i < 5; i++) {
    cout << "[" << i + 1 << " ] ";
}
cout << endl;
for (int j = 0; j < 5; j++) {
    cout << item[j] << "    ";
}
cout << endl;
cout << "After Item 1, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
    cout << item[j] << "    ";
}
cout << endl;
cout << "After Item 2, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
    cout << item[j] << "    ";
}
cout << endl;
cout << "After Item 3, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
    cout << item[j] << "    ";
}
cout << endl;
cout << "After Item 4, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
    cout << item[j] << "    ";
}
cout << endl;
cout << "After Item 5, " << endl;
adde(mov, item);
for (int j = 0; j < 5; j++) {
    cout << item[j] << "    ";
}
cout << endl;

My code works fine till here. After the last item i want my cursor to point back to first index but shows weird Library Run time error After it runs this highlighted part of code cout << "After Item 6, " << endl;

adde(mov, item);

for (int j = 0; j < 5; j++) { cout << item[j] << " "; } }

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • After "Item 5," it says Item 6 the highlighted part by mistake i pasted it at wrong place please ignore to support my logic for last item i used if>5 in my function defination – wannabeprogrammer Feb 15 '20 at 13:07
  • 2
    And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Feb 15 '20 at 13:09
  • the debugger doesnt tell how to fix that particular error so i would appreciate your help in telling me how to solve this – wannabeprogrammer Feb 15 '20 at 13:30
  • 4
    No debugger will tell anyone "how to fix that particular error", or some deep "meaning of error". This is not what debuggers are for. Debuggers tell you what the error is, and allow you to inspect the complete state of your program, what values all variables have, at this point, what the call stack is, etc... What exactly does your debugger show you? I see multiple bugs in the shown code, off-by-one errors, array overruns, that ends up scribbling over the stack, corrupting it. You need to use your debugger to identify them, one by one, and fix each one. – Sam Varshavchik Feb 15 '20 at 13:35
  • What happens when you call with v = 5? – stark Feb 15 '20 at 14:09
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Feb 15 '20 at 14:32

1 Answers1

0

Heading ##You did a simple mistake. No need for a debbugger here. Arrays in C/C++ start with index 0. So if you have an array of size 5, like in your example char item[5];, the valid indices are 0,1,2,3,4. But not 5.

In your function adde you simply have the wrong boundary check. In your very first statement you have written:

if (v > 5) {

then later

    v++;
    array[v] = 'C';

So, if v is >3, for example 4 or 5, you will increment it and access array[5] or array[6], which is out of bounds and produces an error.

You may simply correct it and use

if (v > 3) {

as you first statement. This will fix the problem:

#include <iostream>

void adde(int& v, char array[5])
{

    if (v > 3) {
        v = -1;
    }

    for (int k = 0; k < 5; k++) {
        if (array[k] == 'C') {
            array[k] = '-';
        }
    }
    v++;
    array[v] = 'C';
}

int main() {
    int mov = -1;
    char item[5];
    for (int i = 0; i < 5; i++) {
        item[i] = '-';
    }
    std::cout << "Initially " << std::endl;
    for (int i = 0; i < 5; i++) {
        std::cout << "[" << i + 1 << " ] ";
    }
    std::cout << std::endl;
    for (int j = 0; j < 5; j++) {
        std::cout << item[j] << "    ";
    }
    std::cout << std::endl;
    std::cout << "After Item 1, " << std::endl;
    adde(mov, item);
    for (int j = 0; j < 5; j++) {
        std::cout << item[j] << "    ";
    }
    std::cout << std::endl;
    std::cout << "After Item 2, " << std::endl;
    adde(mov, item);
    for (int j = 0; j < 5; j++) {
        std::cout << item[j] << "    ";
    }
    std::cout << std::endl;
    std::cout << "After Item 3, " << std::endl;
    adde(mov, item);
    for (int j = 0; j < 5; j++) {
        std::cout << item[j] << "    ";
    }
    std::cout << std::endl;
    std::cout << "After Item 4, " << std::endl;
    adde(mov, item);
    for (int j = 0; j < 5; j++) {
        std::cout << item[j] << "    ";
    }
    std::cout << std::endl;
    std::cout << "After Item 5, " << std::endl;
    adde(mov, item);
    for (int j = 0; j < 5; j++) {
        std::cout << item[j] << "    ";
    }

    std::cout << std::endl;
    std::cout << "After Item 6, " << std::endl; 
    adde(mov, item);
    for (int j = 0; j < 5; j++) {
        std::cout << item[j] << "    ";
    }
    std::cout << std::endl;
    return 0;
}

Please additionally note:

The design is not very good. Please do not use C-style arrays in C++. Please avoid pointer decays in calls to functions. Please rethink you complete design. I can see, what you are doing. This can be done easier in C++, but for a really good or approriate solution, we need to know the requriements.

A M
  • 14,694
  • 5
  • 19
  • 44