Home > Mobile >  Reading invalid data from dynamic array, maybe its logical problem but i don`t see it
Reading invalid data from dynamic array, maybe its logical problem but i don`t see it

Time:12-04

pls help, thx for your time
Task is: create dynamic array, enter data in it and use data for calculation.
The error is in the condition declaration line of the second "for" loop. Error code:"С6385", error text:"reading invalid data"

#include <iostream>
using namespace std;
double s, u, h;
int i = 0;
s = 0;

int main(){
    int ar_size;
    cout << "Enter array size" << endl;
    cin >> ar_size;
    int* a = new int[ar_size];
    
    for (int i = 0; i < ar_size; i  ) {
        cout << "Enter array data" << endl;
        cin >> a[i];
        cout << "Value of " << i << " element is " << a[i] << endl; //check data
            for (i = 0; a[i] > 0 && a[i] < 3.14; i  ) { //problem here, error code:"С6385", error text:"reading invalid data"
             u = 2 * cos(a[i]);
                h = 1 - 2 * sin(a[i]);
                s = s   u / h;
                cout << "When i = " << i << "\t" << "s = " << s << endl;
            }
    }
}

CodePudding user response:

The error in your code is that you are using the same variable (i) for two different purposes: as the loop counter in the outer for loop and as a condition in the inner for loop. This can cause confusion and can lead to errors.

To fix this, you should use different variable names for the two different purposes. For example, you could use j as the loop counter in the inner for loop and i as the loop counter in the outer for loop, like this:

int ar_size;
cout << "Enter array size" << endl;
cin >> ar_size;
int* a = new int[ar_size];

for (int i = 0; i < ar_size; i  ) {
    cout << "Enter array data" << endl;
    cin >> a[i];
    cout << "Value of " << i << " element is " << a[i] << endl; //check data

    for (int j = 0; a[j] > 0 && a[j] < 3.14; j  ) {
        u = 2 * cos(a[j]);
        h = 1 - 2 * sin(a[j]);
        s = s   u / h;
        cout << "When j = " << j << "\t" << "s = " << s << endl;
    }
}

In this code, the outer for loop uses the variable i as the loop counter, and the inner for loop uses the variable j as the loop counter. This avoids the confusion and probably the error that you were experiencing.

CodePudding user response:

I changed it to doubles because that seems more like you application but you had to many i's going on and needed a if block not a for loop. hope this helps

#include<iostream>
#include<cmath>

using namespace std;

double u, h, s = {};

int main()
{
    int ar_size;
    cout << "Enter array size" << endl;
    cin >> ar_size;
    double* a = new double[ar_size];
    cin.clear();
    cin.ignore();
    
    for (int i = 0; i < ar_size; i  )
    {
        cout << "Enter array data" << endl;
        string input;
        getline(cin, input);
        a[i] = stod(input);
        cout << "Value of " << i << " element is " << a[i] << endl;
            if(a[i] > 0 && a[i] < 3.14)
            {
                u = 2 * cos(a[i]);
                h = 1 - 2 * sin(a[i]);
                s = s   u / h;
                cout << "When i = " << a[i] << "\t" << "s = " << s << endl;
            }
            else
            {
                cout << "out of bounds\n";
            }
    }
}
  • Related