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.

(C++) Why can't it runs? It didn't show me errors but a weird loop happens...

0 votes
asked Dec 5, 2019 by 刘子铭 (620 points)

This is a calculator that runs with a process.

Here is the code:

(I don't know if the program runs if I cut the code in parts)

*divs() function is unnecessary(for later or just in case).

#include <iostream>

using namespace std;

void mult(double);

int main()
{   
    int primerDenominador;
    int segonDenominador;
    int resultDenominador;
    
    int primerNumerador;
    int resultPrimerNumerador;
    int segonNumerador;
    int resultSegonNumerador;
    
    char inputOperation;
    char showOperation;
    int operation;
    
    while (1) 
    {
        cout << "================NEW===============" << endl << endl;
        
        cout << "Suma(s) o resta(r)? " << endl;
        cin >> operation;
        
        cout << "Primer denominador: ";
        cin >> primerDenominador;
        
        cout << "Segon denominador: ";
        cin >> segonDenominador;
        
        resultDenominador = primerDenominador * segonDenominador;
        
        cout << "Primer numerador: ";
        cin >> primerNumerador;
        cout << "                  ----------" << endl;
        cout << "                  " << primerDenominador << endl;
        resultPrimerNumerador = segonDenominador * primerNumerador;
        
        cout << "Segon numerador: ";
        cin >> segonNumerador;
        cout << "                 ----------" << endl;
        cout << "                 " << segonDenominador << endl;
        resultSegonNumerador = primerDenominador * segonNumerador;
        
        cout << "Presta ENTER per continuar.";
        cin.get();
        cin.get();
        cin.get();
        cin.get();
        system("clear");
        
        cout << "Resultat de la ("<< operation << "): " << endl;
        if (inputOperation = 's') {
            operation = resultSegonNumerador + resultPrimerNumerador;
            showOperation = '+';
        } else if (inputOperation = 'r') {
            operation = resultPrimerNumerador - resultSegonNumerador;
            showOperation = '-';
        }
        
        cout << primerNumerador << "\t   " << segonNumerador << endl;
        cout << "-------------" << "\t" << showOperation << "\t" << "-------------" << " =";
        cout << endl << resultDenominador << "\t   " << resultDenominador << endl;
        
        cout << endl;
        
        
    }
    
    return 0;
}

void divs(double x)
{
    cout << "3: " << x / 3 << endl;
    cout << "5: " << x / 5 << endl;
    cout << "7: " << x / 7 << endl;
    cout << "11: " << x / 11 << endl;
    cout << "13: " << x / 13 << endl;
    cout << "17: " << x / 17 << endl;
    cout << "19: " << x / 19 << endl;
}

3 Answers

0 votes
answered Dec 8, 2019 by Anmolnoor (240 points)
================NEW===============

Suma(s) o resta(r)?

5

Primer denominador: 6

Segon denominador: 5

Primer numerador: 4

                  ----------

                  6

Segon numerador: 6

                 ----------

                 5

Presta ENTER per continuar.4

Resultat de la (5):

4    6

------------- + ------------- =

30    30

================NEW===============

Suma(s) o resta(r)?

this is working well for me
commented Dec 10, 2019 by 刘子铭 (620 points)
So are you running it in OnlineGDB's compiler? Haven't you got any loops? So weird...
0 votes
answered Dec 8, 2019 by Donzy Botwe (440 points)
The code runs correctly.

This is a normal problem with compilers, I have experienced that before.

Just re-save the file into a new directory maybe you desktop where you can easily locate it.

Close the application you are using for the programming whether devcpp or codeblocks or visual studio and re-open the saved file.

It worked for me so it must work or you can can the IDE you are using.

Thank you.
commented Dec 10, 2019 by 刘子铭 (620 points)
Well, first, thank you for the answer. I tried it on Visual Studio 2019 for Windows, but the problem continue happening... ´_`
0 votes
answered Dec 13, 2019 by gameforcer (2,990 points)

        cout << "Suma(s) o resta(r)? " << endl;
        cin >> operation;

When I tried to run it this bit made me think I need to input "s" or "r" to decide the operation, meanwhile the "operation" variable is actually an integer. Then it made these weird loops which I have no idea why they're happening.

Maybe you did the same? Because otherwise these loops don't occur.

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.
...