• Check out the results of the Techtree Contest #19!
  • Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

Help in C++ Calculator

Status
Not open for further replies.
Level 19
Joined
Oct 7, 2014
Messages
2,208
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;
   
}
 
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>
 
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. :)
 
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;
   
}
 
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.
Back
Top