Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Need help with my code

+1 vote
asked Nov 12, 2020 by Vicent Cotaina Bertó (390 points)

I need to make that when the user introduces characters in the char var that is going to convert in float appears a message and solicitate a new "number" my code is the following one.
int main()
{
    int p=0,n=0,k=0;
    float positivos[p] = {},negativos[n] = {};
    char cad[80];
    float valor=1, SumaPositivos=0, SumaNegativos=0;
    std::cout.setf(std::ios::fixed);
    std::cout.setf(std::ios::showpoint);
    std::cout.setf(std::ios::showpos); //ios está en el espacio de nombres std y por eso hay que ponerle std antes.
    std::cout.precision(2);
    while (valor!=0){
        std::cout << "Escribe tu movimiento bancario: ";
        std::cin >> cad;
        valor=atof(cad);
        if (valor==0){
           
        }
        else if (valor>0){
            positivos[p] = valor;
            SumaPositivos+=valor;
            p++;
            std::cout << valor << "\n";
        }
        else if (valor<0){
            negativos[n] = valor;
            SumaNegativos+=valor;
            n++;
            std::cout << valor << "\n";
        }
    }
    std::cout << "Tus ingresos son (";
    while(k<p){
        std::cout << positivos[k] << "|";
        k++;
    }
    std::cout << ")";
    k=0;
    std::cout << "\nTus gastos son (";
    while(k<n){
        std::cout << negativos[k] << "|";
        k++;
    }
    std::cout << ")";
    return 0;
}

1 Answer

–1 vote
answered Nov 12, 2020 by Peter Minarik (84,180 points)
edited Nov 12, 2020 by Peter Minarik

Use a common language

Please, use English for identifiers and comments in the code. It makes understanding much easier. (Note: I'm not a native English speaker either.)

This is not C, it's C++

I've noticed in the tags you added "C", but this code cannot be compiled as C, only as a C++ code.

Arrays are not correctly initialised

int p=0,n=0,k=0;
float positivos[p] = {},negativos[n] = {};

These lines create arrays of 0 size. They contain zero elements. Arrays are fixed size, they cannot be resized. So you can never add more elements to them than their initial capacity. So zero sized arrays are practically useless. Later, when you try to do

positivos[p] = valor;
SumaPositivos+=valor;
p++;

you will have a memory violation, trying to write positivos's element that does not exist.

So, to solve this problem, you should initialize your arrays correctly in the beginning. So instead of 0, set the expected (maximum) size you want to allow.

Even better, since you need C++ compiler anyway due to including iostream, you should use std::vector instead of array:

std::vector<float> positiveNumbers;
std::vector<float> negativeNumbers;
// ...
positiveNumbers.push_back(1.23); // Example of how to add a number to the vector

Please, read the documentation of std::vector here.

The vector can be looked at as an array that can dynamically change its size on demand.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...