• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Help in C++ Calculator

Status
Not open for further replies.
Level 19
Joined
Oct 7, 2014
Messages
2,209
Hello guys, I'm at lost with my work right now. I don't know why the last part doesn't work in the Y/N to continue. The Y doesn't run the program again.

Code:
#include<iostream>
#include<math.h>
using namespace std;

int main()
{ 
    float num1,num2;
    char operation,cont,redo;
    cout<<"This program will allow you to calculate two numbers."<<endl;
   
    do
    {    
     P1: cout<<" Please enter two numbers";
        cout<<" 1st number:";
        cin>>num1;
        cout<<" 2nd number:" ;
        cin>>num2;
        cout<<endl;
        cout<<" Please enter an operation which you like to use (+, -, *, /, %)";
        cin>>operation ;
        cout<<endl<<endl;
   
        if (cin.fail())
           {
             cin.clear();
             cout<< "Input is not a number, please try again."<<endl;
             cin>>redo;
             cout<<endl;
           }
   
     P2: switch (operation)
         {
      
           case'+':           
                   cout<<"The addition of two numbers ("<<num1<<","<<num2<<"):";
                   cout<<num1+num2<<endl;
                   break;
            
           case'-':
                   cout<<"The substraction of two numbers ("<<num1<<","<<num2<<"):";
                   cout<<num1-num2<<endl;
                   break;
            
           case'*':
                   cout<<"The multiplication of two numbers ("<<num1<<","<<num2<<"):";
                   cout<<num1*num2<<endl;
                   break;
            
           case'/':
                   cout<<"The division of two numbers ("<<num1<<","<<num2<<"):";
                   if(num2==0)
                     {
                       cout<<"not valid"<<endl;
                     }
                   cout<<(num1/num2)<<endl;
                   break;
            
           case'%':
                   cout<<"The modulus of two numbers ("<<num1<<","<<num2<<"):";
                   cout<<fmod(num1,num2)<<endl;
                   break;
                  
           default:
                   cout<<"Input is not a valid operation, please try again."<<endl;       
                   cin>>operation;
                   goto P2;
                       
           }   
   
     P3: cout<<"enter Y/N to continue:";
         cin>>cont;
         switch (cont)
             {
               case 'Y':
                     cin>>redo;
                     goto P1;                
                    
              case 'N':
                     break;
                    
              default:
                    cout<<"Input is not a valid value, please try again."<<endl;
                    cin>>cont;
                    goto P3;                 
          }          
    }
    while(redo=='Y');
          
system("pause");
return 0;
   
}
 
Level 22
Joined
Feb 6, 2014
Messages
2,466
Why do you have cin>>redo; after case 'Y':?

Also, the char is case-sensitive, are you sure your input is uppercase 'Y'.

This is how I do my restarts:
Code:
    cout <<"Do you want to restart?Y/N?";
    restart = 'a'; //make sure while loop executes
    while (restart != 'y' && restart !='n')
    {
        restart = getch();
        restart = tolower(restart); //lowercase the char
        if (restart == 'y')
        goto menu;
        if (restart == 'n')
        return 0;
    }
getch() returns the char of the pressed button, you can change it to cin if you want (if that's the case, remove "restart = 'a'; //make sure while loop executes") .
It needs #include <conio.h>
 
Level 19
Joined
Oct 7, 2014
Messages
2,209
I change the restart into this and it works fine.

Code:
cout<<"Enter Y/N to continue:";
         cin>>redo;

Well any letter except Y also executes like N but I think it would be okay.

I haven't seen getch() function being used, thanks for the information. :)
 
Level 19
Joined
Oct 7, 2014
Messages
2,209
Oh okay I understand.

I've changed the code last night into this. Is this better than the one I first posted?

Code:
#include<iostream>
#include<math.h>
using namespace std;

int main()
{ 
    float num1,num2;
    char operation,redo;
    cout<<"This program will allow you to calculate two numbers."<<endl;
   
    do
    {    
         cout<<" Please enter two numbers"<<endl;
         cout<<" 1st number:";
         cin>>num1;
         cout<<" 2nd number:" ;
         cin>>num2;
         cout<<endl;
         cout<<" Please enter an operation which you like to use (+, -, *, /, %)";
         cin>>operation ;
         cout<<endl<<endl;
   
        if (cin.fail())
           {
             cin.clear();
             cout<< "Input is not a number, please try again."<<endl;
             cin>>redo;
             cout<<endl;
           }
   
        switch (operation)
         {
      
           case'+':           
                   cout<<"The addition of two numbers ("<<num1<<","<<num2<<"):";
                   cout<<num1+num2<<endl;
                   break;
            
           case'-':
                   cout<<"The substraction of two numbers ("<<num1<<","<<num2<<"):";
                   cout<<num1-num2<<endl;
                   break;
            
           case'*':
                   cout<<"The multiplication of two numbers ("<<num1<<","<<num2<<"):";
                   cout<<num1*num2<<endl;
                   break;
            
           case'/':
                   cout<<"The division of two numbers ("<<num1<<","<<num2<<"):";
                   if(num2==0)
                     {
                       cout<<"not valid"<<endl;
                     }
                   cout<<(num1/num2)<<endl;
                   break;
            
           case'%':
                   cout<<"The modulus of two numbers ("<<num1<<","<<num2<<"):";
                   cout<<fmod(num1,num2)<<endl;
                   break;
                  
           default:
                   cout<<"Input is not a valid operation, please try again."<<endl;
                   cout<<endl;                                            
         }   
   
         cout<<"Do you want to continue? Y/N"<<endl;
         cin>>redo;          
    }
    while (redo=='Y');
          
return 0;
   
}
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Definitely.

It's nice that you even thought about division by zero, but you didn't actually prevent it - while you write a warning, the code still runs it and will throw an exception.

In case you didn't see yet, you can set your build to Console mode (how to do this depends on what environment you use), which will automatically pause execution before quitting, so you don't need the system("pause") hack.
 
Status
Not open for further replies.
Top