2015-05-30 3 views
0

На самом деле я просто пытался нащупать руки на C++ и остановился на этой конкретной проблеме, где вместо закрытия программы она должна спросить пользователя, хочет ли он продолжать или хочет выйти. Теперь, что я понимаю, я написал код do-while в конечных строках, но не работал. Пожалуйста, дайте ему решение. Благодаря!!C++ Prompt User to continue or leave

#include<iostream> 
#include<stdio.h> 
#include<cstdlib> 
#include<string> 

using namespace std; 

class Cal 
{ 
    public: 
    int Add(int a, int b) 
    { 
     int res; 
     res=(a+b); 
     cout << "Answer is " << a << "+" << b << "=" << res << endl; 
    } 
    int Sub(int a,int b) 
    { 
     int res; 
     res=(a-b); 
     cout << "Answer is " << a << "-" << b << "=" << res << endl; 
    } 
    int Mul(int a,int b) 
    { 
     int res; 
     res=(a*b); 
     cout << "Answer is " << a << "*" << b << "=" << res << endl; 
    } 
    int Div(int a,int b) 
    { 
     int res; 
     res=(a/b); 
     cout << "Answer is " << a << "/" << b << "=" << res << endl; 
    } 
}; 

int main() 
{ 
    int first, second, res, operation; 

    cout<<"**********************************"<<endl; 
    cout<<"******* Simple Calculator ********"<<endl; 
    cout<<"**********************************"<<endl; 
    cout<<"Select the Operation: "<<endl; 
    cout<<"1. Addition"<<endl; 
    cout<<"2. Subtraction"<<endl; 
    cout<<"3. Multiplication"<<endl; 
    cout<<"4. Divison"<<endl; 
    cout<<"Choosen Operation is: "; 
    cin>>operation; 
    cout << "Enter the 1st Number: "; 
    cin>>first; 
    cout << "Enter the 2nd Number: "; 
    cin>>second; 

    switch(operation){ 
    case 1: 
     Cal a; 
     a.Add(first,second); 
     break; 
    case 2: 
     Cal b; 
     b.Sub(first,second); 
     break; 
    case 3: 
     Cal c; 
     c.Mul(first,second); 
     break; 
    case 4: 
     Cal d; 
     d.Div(first,second); 
     break; 
    default: 
     cout<< "Please Enter a Operation"; 
     break; 
    } 

    char ans; 
    do 
    { 
     cout<< "Do you want to continue (Y/N)?\n"; 
     cout<< "You must type a 'Y' or an 'N' :"; 
     cin >> ans; 
    } 
    while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n')); 
} 
+0

Ребята, я хочу полностью управляется код, его просто ОБРАБОТКА делать-то время цикла ... пожалуйста, помогите мне – hrishikeshps

ответ

1

Цикл do while не охватывает тело быть повторена, т.е. калькулятор части.

Состояние в do while не выглядит правильным.

Я бы попробовал это.

int main() { 
    char ans = 'N'; 
    do { 
    // calculator stuff, better to be in a separate function 

    cout << "Do you want to continue (Y/N)?\n"; 
    cout << "You must type a 'Y' or an 'N' :"; 
    cin >> ans; 
    } while ((ans == 'Y') || (ans == 'y')); 
} 
+0

, где я должен добавить этот код вашей? – hrishikeshps