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.

Help with my program 2

+1 vote
asked Nov 15, 2020 by Vicent Cotaina Bertó (390 points)
retagged Nov 15, 2020 by Vicent Cotaina Bertó
I need to make that when user introduces a number with ',' or '.' the number converts into decimal number. I have to do it with switch structure. For now I have the following code:

int main()
{
    int p=0,n=0,k=0,signo=1;
    float positivos[p] = {},negativos[n] = {};
    float valor, SumaPositivos=0, SumaNegativos=0,prueba=0;
    char caracter;
    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);
    cout << "Introduce your banking movement: " ;
    while (valor!=0)
    {
        cin.get(caracter);
        switch(caracter)
        {
            case '-':
                signo=-1;
                break;
            case ',':
            case '.':
                break;
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                prueba=prueba*10+signo*(float (caracter-'0'));
                valor=prueba;
                break;
            case '\n':
                if(prueba>0){
                    positivos[p] = prueba;
                    SumaPositivos+=prueba;
                    p++;
                    std::cout << prueba << "\n";
                }
                else if(prueba<0){
                    negativos[n] = prueba;
                    SumaNegativos+=prueba;
                    n++;
                    std::cout << prueba << "\n";
                }
                else if(prueba==0){
               
                }
                prueba=0;
                signo=1;
                break;
            default:
            {
                cout << "Invalid character, introduce numbers.";
                prueba=0;
                caracter=' ';
            }
        }
    }

1 Answer

0 votes
answered Nov 16, 2020 by Peter Minarik (86,040 points)
To me, this seems to be a duplicate  of http://question.onlinegdb.com/8595/help-with-my-program
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.
...