pemarting
pemarting

Reputation: 1

I solved a thing, don't know the reason behind

I did a program that translates number into roman numbers, and I was about to show it to a friend of mine when the .exe file didn't work. It asked the user which number to put in, and after he put it the program closed. What I don't understand is why it did work after compiling on DevC++, but had a different behavior as a .exe

I found few solutions out there, and the one that worked for me was to add a:

int a;
cin>>a;

Before returning a cero. Now it works. I don't understand how the console doesn't execute the actions given without that. I'm leaving the code here.


#include <iostream>
using namespace std;

int main(){
    int numero, unidades, decenas, centenas, millares;
    int comprobante;
    
    cout<<"Este programa traduce un numero de 4 cifras a numeros romanos.\nDigite un numero de 
    maximo cuatro cifras: ";
    cin>>numero;
    
     
    unidades= numero%10; 
    numero /= 10; 
    decenas= numero%10; 
    numero/=10; 
    centenas= numero%10; 
    numero/=10; 
    millares=numero;
    comprobante=millares/10; 
        
    if(comprobante==0){
        
        cout<<"Su numero traducido a numeros romanos es: ";
        
        switch(millares){
            case 1: cout<<"M"; break;
            case 2: cout<<"MM"; break;
            case 3: cout<<"MMM"; break;

        }
        
        switch(centenas){
            case 1: cout<<"C"; break;
            case 2: cout<<"CC"; break;
            case 3: cout<<"CCC"; break;
            case 4: cout<<"CD"; break;
            case 5: cout<<"D"; break;
            case 6: cout<<"DC"; break;
            case 7: cout<<"DCC"; break;
            case 8: cout<<"DCCC"; break;
            case 9: cout<<"CM"; break;      
        }
        
        switch(decenas){
            case 1: cout<<"X"; break;
            case 2: cout<<"XX"; break;
            case 3: cout<<"XXX"; break;
            case 4: cout<<"XL"; break;
            case 5: cout<<"L"; break;
            case 6: cout<<"LX"; break;
            case 7: cout<<"LXX"; break;
            case 8: cout<<"LXXX"; break;
            case 9: cout<<"XC"; break;      
        }
        
        switch(unidades){
            case 1: cout<<"I"; break;
            case 2: cout<<"II"; break;
            case 3: cout<<"III"; break;
            case 4: cout<<"IV"; break;
            case 5: cout<<"V"; break;
            case 6: cout<<"VI"; break;
            case 7: cout<<"VII"; break;
            case 8: cout<<"VIII"; break;
            case 9: cout<<"IX"; break;  
        }
    }
    else{
        cout<<"Te dije de 4 cifras. Por desgraciado me cierro.";
    }
    
    int a;
    cin>>a;
    return 0;
}

Upvotes: 0

Views: 119

Answers (3)

Ranoiaetep
Ranoiaetep

Reputation: 6657

Your code was totally fine.

It is that when the main() reached return 0; that means it is done. And if you are running it as an .exe file, that means the program is done. And when a program is done, it would be closed.

There are couple ways to make it stay not closed. One that many have mentioned is to add a cin >> something; or getch(); with #include <conio.h> for windows at the end, so it would wait till you enter something.

Also you could delay the program before it reaches the return statement. You can check here How do you add a timed delay to a C++ program?

You could also wrap you entire program with a while loop, like:

while(true)
{
    // Your programs here
    if(someCondition) break;
};

where someCondition is a bool type, maybe char c; cin >> c; bool someCondition = (c == 'x'); So that the program only ends if someone input 'x', else it would run again.

Upvotes: 0

Marius Pantazi
Marius Pantazi

Reputation: 1

Include #include<conio.h>

And add getch(); at the end of your code. This will make your console to not close.

Upvotes: 0

Shane Reilly
Shane Reilly

Reputation: 251

The program closes immediately in the terminal when the program reaches completion. When you cin>>a it is waiting for input before closing. See this.

Upvotes: 2

Related Questions