-7
#include <iostream>
using namespace std;

int main()
{
   float a, b, result;
   char operation, response;
   cin >> a >> operation >> b;
   switch(operation)
   {
   case '+':
         result = a + b;
         break;

   case '-':
         result = a - b;
         break;

   case '*':
         result = a * b;
         break;

   case '/':
         result = a / b;
         break;

   default:
         cout << "Invalid operation. Program terminated." << endl;
         return -1;
   }

   // Output result
   cout << "The result is " << result << endl;
   system("sleep 200");
   cout << "Do you want to make another opertaion? (Y/N)" << endl;
   cin >> response;
   if(response=='Y')
   {
    here
   }
   else if(response=='N')
   {
    cout << "The program will now close" << endl;
    system("sleep 500");
    return -1;
   }
   return 0;
}

Where the word 'here' is written, I want to insert a code that gets you to the beggining of the code.

genpfault
  • 51,148
  • 11
  • 85
  • 139
SoLux
  • 162
  • 1
  • 9

2 Answers2

2

You can use the loop do... while(condition)

Like :

do{
    cin >> a >> operation >> b;
    switch(operation)
    {
...

    }

    // Output result
    cout << "The result is " << result << endl;
    system("sleep 200");
    cout << "Do you want to make another opertaion? (Y/N)" << endl;
    cin >> response;
}while(response=='Y');

If the response isn't 'Y' the loop ends, if it is the loop begin again

Hearner
  • 2,711
  • 3
  • 17
  • 34
2

All though goto is highly frowned upon in C++, it does exist. Much like assembly, your put a label in your code and then tell it to 'jump' there using goto. However, the same way jump works in assembly, this may break you out of all loops to the point that you jump to.

#include <iostream>
using namespace std;

int main()
{
   label: 
   float a, b, result;
   char operation, response;
   cin >> a >> operation >> b;
//condensed for neatness

   // Output result
   cout << "The result is " << result << endl;
   system("sleep 200");
   cout << "Do you want to make another opertaion? (Y/N)" << endl;
   cin >> response;
   if(response=='Y')
   {
    goto label;
   }
   else if(response=='N')
   {
    cout << "The program will no`enter code here`w close" << endl;
    system("sleep 500");
    return -1;
   }
   return 0;
}

What most people would do is use a do{}while(condition==true) loop or just an infinite loop while(true).

For this 'neater' code, what you would do is

#include <iostream>
using namespace std;

int main()
{
   do{
   float a, b, result;
   char operation, response;
   cin >> a >> operation >> b;
   //condensed for neatness 


// Output result


   cout << "The result is " << result << endl;
   system("sleep 200");
   cout << "Do you want to make another opertaion? (Y/N)" << endl;
   }while(cin.get() == 'Y');


    cout << "The program will no`enter code here`w close" << endl;
    system("sleep 500");
    return -1;

   return 0;
}

If this is being called from another location, I would highly recommend using do while rather than goto, as it can cause issues.

The only real problem with goto is that it's not elegant and makes code confusing to read. You should use loops and returns where possible, and only use goto if you see no other way to make this work optimally.

DarmaniLink
  • 126
  • 10