2016-04-10 5 views
-4

C schließt in Hallo Ich erstelle einen Rechner und mein Code hält nur einen Sprung an das Ende mein Code hierWie beende ich meine Befehlszeile ++

// Calculator.cpp : Defines the entry point for the console application.// 

#include "stdafx.h" 
#include <cstdio> 
#include <limits> 
#include <iostream> 


int num1, num2, ans, oper(0); 



int main() 
{ 
    std::cout << "Put in your first number to be calculated" <<std::endl; 
    std::cin >> num1; 
    std::cout << "Put in your second number to be calculated"<<std::endl; 
    std::cin >> num2; 
    std::cout << "The next bit is abit complicated, blame the code writer for that he cant use char yet but anyway." << std::endl; 
std::cout << "If you want multiplication press 1 then enter." << std::endl; 
std::cout << "If you want division prees 2 then enter." << std::endl; 
std::cout << "If you want addition press 3 then enter." << std::endl; 
std::cout << "If you want subtraction press 4 then enter." << std::endl; 
std::cin.get(); 
if (oper == 1) { 
    ans = num1*num2; 
    std::cout << ans; 
} 
while (oper == 2) { 
    ans = num1/num2; 
    std::cin.get(); 
    std::cout << ans; 
    std::cin.get(); 
} 
while (oper == 3) { 
    ans = num1 + num2; 
    std::cout << ans; 
    std::cin.get(); 
} 
    while (oper == 4) { 
     ans = num1 - num2; 
     std::cout << ans; 
     std::cin.get(); 
     } 
return 0; 
} 

ich nach Lösungen für diese Suche kann jemand helfen mir, es tut mir leid, wenn dies eine einfache Frage, die ich bin neu in C++

+2

Der Code springt nicht zum Ende. Laden Sie Ihren Debugger hoch und beobachten Sie, wie Ihre Zeilen ausgeführt werden und was ihre Ergebnisse sind. Und vielleicht wollen Sie herausfinden, wie man Fehlerflags löscht und über den Zeilenumbruch in Ihrem Input-Stream hinausspringt ... –

+0

Haben Sie 'oper' mit' 0' initialisiert, um eine Compiler-Warnung loszuwerden? Dann suchen Sie nach einer anderen Lösung, um diese Warnung zu beheben. – wimh

+0

Nein, ich habe nicht ... –

Antwort

1

Versuchen Sie folgendes:

std::cin.get(); 
if (oper == 1) 
    ans = num1*num2; 
else if(oper == 2) 
    ans = num1/num2; 
else if(oper == 3) 
    ans = num1 + num2; 
else if(oper == 4) 
    ans = num1 - num2; 

std::cout << ans;  
std::cin.get();//this will block and prevent the console from closing until you press a key 
return 0; 
2

Try System ("Pause") zu schreiben; vor zurück 0; am Ende Ihres Programms und drücken Sie Strg + F5.

Verwandte Themen