Problem when printing variables in structure

1

In case 2 I try to print the variables, but it shows me wrong values I tried to do it in a function and the same thing happened, with pointers and neither did the program in the enter function does everything right try to print from it and It showed what was due. The problem is showing everything in another function.

#include <iostream>
#include <math.h>
#include <windows.h>

using namespace std;
struct datos{
    int n;
    float m, b, sx, sy, sx2, sxy, X[20], Y[20];
};
typedef datos v;

void inicio();
void menu();
void ingresar_operaciones(struct datos d);
v mostrar(struct datos d);
int i;

int main() {
    int op;
    v D;
    inicio();
    do {
        menu();
        cin >> op;
        switch(op) {
            case 1:
                ingresar_operaciones(D);
            break;
            case 2:
                for(i = 0;i<D.n;i++) {
                    cout << D.X[i] << endl;
                    cout << D.Y[i] << endl;
                }
                cout << D.sx << endl;
                cout << D.sy << endl;
                cout << D.sx2 << endl;
                cout << D.sxy << endl;
                cout << D.m << endl;
                cout << D.b << endl;
            break;
            case 3:
                cout << "saliendo del programa" << endl;
                system("pause");
                return 0;
            default:
                cout << "opcion invalida" << endl;
            break;
        }
    } while(op! = 3);
}

void menu () {
    cout << "#1 ingresar" << endl;
    cout << "#2 mostrar" << endl;
    cout << "#3 salir" << endl;
}

void ingresar_operaciones(struct datos d) {
    float ms2, ms1, mi1, mi2, mbi, bs1, bs2;
    d.sx = 0,  d.sx2 = 0,  d.sxy = 0,  d.sy = 0;
    bool c = false;
    cout << "ingrese cantidad de datos" << endl;
    cin >> d.n;
    do {
        for(i = 0;i<d.n;i++) {
            cout << "ingrese el valor #" << i+1 << " de las X(variables independientes)"  << endl;
            cin >> d.X[i];
            cout << "ingrese el valor #" << i+1 << " de las Y(variables dependientes)" << endl;
            cin >> d.Y[i];
            d.sx = (d.sx+d.X[i]);
            d.sy = (d.sy+d.Y[i]);
            d.sx2 = (d.sx2+(pow(d.X[i], 2)));
            d.sxy = (d.sxy+(d.X[i]*d.Y[i]));
        }
        cout << "estan correctos los datos? S(1)/N(0)" << endl;
        cin >> c;
    } while(c =  = false);
    ms2 = (d.n*d.sxy);
    mi1 = (pow(d.sx, 2));
    mi2 = (d.n*d.sx2);
    mbi = (mi1-mi2);
    ms1 = (d.sx*d.sy);
    d.m = ((ms1-ms2)/mbi);
    bs1 = (d.sxy*d.sx);
    bs2 = (d.sy*d.sx2);
    d.b = ((bs1-bs2)/mbi);
    cout << d.m << endl;
    cout << d.b << endl;
    cout << "Y(x) = " << d.m << "x+" << d.b << endl;
    system("pause");
}

v mostrar(datos d) {
}

void inicio() {
    cout << "bienvenido" << endl;
}
    
asked by Joni 25.10.2018 в 22:53
source

2 answers

3

Before commenting on the failure you describe, I want to share some problems with your code:

  • The header <math.h> is from not from . This header has a version adapted to C ++ that has the prefix c and has no extension. If you really need to use the C headers (which will never be the case) you should use the C ++ equivalents <cmath> . Read this thread to find out why.
  • There is no obligation to use the using namespace std; clause since it is only an aid to the writing of code; If you decide to use this clause do not do it in the global scope, use it in the smallest possible scope. Read this thread to find out why.
  • Your variables do not have self-explanatory names: Single-letter names such as n , m , b , v , D , d , i , X o Y do not explain anything about the objective or function of the variables, names like sx , sy , sx2 , sxy , op , ms2 , ms1 , mi1 , mi2 , mbi , bs1 , bs2 do not provide any information. The name of a variable should allow you to know at a glance its purpose, making the code less prone to errors and making it easier to read and understand, which any person who works with you (including your future self) will be very grateful.
  • In C ++ the keyword struct is not part of the type, so it is not necessary to define variables of type struct .
  • Let your code breathe: in the 90 we had screens of 80 characters wide and 25 lines high (there was no choice but to compact the code), but for decades we do not have those limitations, put some spaces in the code It will make it easier to read and your future self will thank you.

Now, the bug you describe.

As SJuan76 already points out, you are copying your data to the ingresar_operaciones function, so a copy of datos is filled in which will be inaccessible outside the function.

Since the type datos lacks constructor or initializer, the member variables remain uninitialized and consequently obtain residual values; As you do not modify the values in the ingresar_operaciones function, you will see the erroneous values that you describe.

To solve this I advise you the following:

Proposal.

Add initializers to your structure:

struct datos{
    int n = 0;
    float
        m = .0f,
        b = .0f,
        sx = .0f,
        sy = .0f,
        sx2 = .0f,
        sxy = .0f,
        X[20]{},
        Y[20]{};
};

This will make all elements of your structure zero. You should also pass the structure by reference:

void ingresar_operaciones(datos &d) { // En C++ no necesitas 'struct'
    //         Referencia ----> ^

    // ...
}
    
answered by 26.10.2018 / 09:53
source
2
void ingresar_operaciones(struct datos d);
v mostrar(struct datos d);

ingresar_operaciones(D);

When calling these functions, within the function you work with a copy of the variable you originally sent. So in ingresar_operaciones you save data in a copy that disappears when you exit the method, the data of the original variable is not modified.

At least in the methods you want to use to modify the data of the original variable, you have to make one of these changes:

  • pass a pointer. In this way what is copied is the pointer , but the copied pointer still points to the same original variable.

    void ingresar_operaciones(struct datos *d);
    ingresaro_operaciones(&D); // Le pasas la dirección de la variable local D.
    

    Naturally, you will have to change the code of ingresar_operaciones to use -> instead of . , because now d is a pointer to a structure and not a structure.

  • return the resulting value and write it in the variable:

    struct datos ingresar_operaciones(struct datos d);
    

    and at the end of ingresar_operaciones :

    return d;
    

    and you invoke it:

    D = ingresar_operaciones(D);
    
answered by 26.10.2018 в 01:20