Ending a while loop but inputting a command

how can i input multiple instructions to manipulate an array buy adding, subtracting etc, where 1 (adds), 2 (subtracts), my problem is that it loop the command i have inputed gets executed infantly, not just once then wait for the next command, until 6 (exit) is typed in, then it should close the program

e.g.
3 0 6 - which swaps s[0] with s[6]
4 7 5 - which puts 7 into array value 5
1 2 4 - which adds s[2] to s[4]
2 9 2 - which subtracts s[9] from [2]
5 0 3 - which outputs s[3]
6 0 0 - which EXITS


but exit on the command exit

this is our loop



      while ( operation != 6 ){

      if (operation == 1){
         s[secondarg] += s[firstarg];

           cout << s[secondarg] << "  ";

    } else if (operation == 2){
         s[secondarg] -= s[firstarg];

           cout << s[secondarg] << "  ";

     } else if (operation == 3){
         s[secondarg] = s[firstarg];

           cout << s[secondarg] << "  ";

     } else if (operation == 4){
         s[secondarg] = firstarg;

           cout << s[secondarg] << "  ";

     } else if (operation == 5){
        // s[secondarg] = firstarg;

           cout << s[secondarg] << "  ";
     } /*else if (operation == 6){

          //s[ 9 ] = 101;

     }*/
           else {
         cout << "Unknown operation." << endl;
    }

     //s[ 9 ] += 1;

      }//end of while

thanks
a_migdalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gratch06Commented:
iint operation(0);
cout << "Enter Operation to do, followed by the two arguments." << endl
  << "<menu here>" << endl;
cin >> operation >> firstarg >> secondarg;
while(operation != 6){
    ...//do usual calculations here
    cout << "Enter Operation to do, followed by the two arguments." << endl
        << "<menu>" << endl;
    cin >> operation >> firstarg >> secondarg;
}//end of while

Your problem is a lack of new input within the loop.  Doing this should fix it for you.
0
bcladdCommented:
Where does operation get set? That is, where do you read in firstarg, secondarg, and operation? Before the loop, for sure, but if you expect the loop to keep going with changing values you must read in new values inside the while loop.

Hope this helps, -bcl
0
a_migdalAuthor Commented:
still very much a novice at c++ could you offer some more explanation into how the code works
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

bcladdCommented:
What you have is something known as a "loop-and-a-half" problem. You want to prompt the user for some information (note that reading from a file is very similar but I will focus on interactive input) and it is possible that the first thing the user gives you is the exit command. You neeed to keep processing input so long as the user doesn't chose to exit. So, let't try a simple command processor. The loop can handle three strings: laugh, cry, exit:

std::string cmd;
// read in the command; this is the half of a loop
cout << "Command: ";
cin >> cmd;

while (cmd != "exit") {
  if (cmd == "laugh") {
    cout << "ha, ha" << endl;
  } else if (cmd == "cry") {
    cout << "boo hoo" << endl;
  } else {
    cout << "unknown command: " << cmd << endl;
  }

  // NOW: if we just go back to the top, cmd will contain whatever it had before and we will
  // print just the same thing. Need to read cmd AGAIN:
  cout << "Command: ";
  cin >> cmd;
}

Hope that helps
-bcl
0
a_migdalAuthor Commented:
cheers for the help, i have got the loop working perfectly, it keeps asking for commands until i hit the exit command, the problem i now have is that the first time the loop is executed it miss calculates the answer, how ever after the first time the loop had run through the calculations are perfect. It is only ever on the first execution of the loop. Below is the complete code that i am using.

Cheers





#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setw;

//using namespace



int main()
{
      int firstarg;

      int secondarg;

      int operation;



   // constant variable can be used to specify array size
   const int arraySize = 10;
   
   int s[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  // array s has 10 elements


   cout << "Element" << setw( 13 ) << "Value" << endl;

   // output contents of array s in tabular format
   for ( int j = 0; j < arraySize; j++ )  
      cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl;

     cout << "Enter Operation to do, followed by the two arguments." << endl
         << "Example: 1 2 5 is the same as array[5] = array[5] + array[2]" << endl;
     cin >> operation >> firstarg >> secondarg;
     firstarg--; secondarg--;  //adjust for array starting at 0.


       while ( operation != 6 ){

       if (operation == 1){
         s[secondarg] += s[firstarg];

             cout << s[secondarg] << "  ";

    } else if (operation == 2){
         s[secondarg] -= s[firstarg];

             cout << s[secondarg] << "  ";

      } else if (operation == 3){
         s[secondarg] = s[firstarg];

             cout << s[secondarg] << "  ";

      } else if (operation == 4){
         s[secondarg] = firstarg;

             cout << s[secondarg] << "  ";

      } else if (operation == 5){


             cout << s[secondarg] << "  ";
      }
             else {
         cout << "Unknown operation." << endl;
    }



                 cout << "Enter Operation to do, followed by the two arguments." << endl;

                       cin >> operation >> firstarg >> secondarg;

       }//end of while
      
return 0;  // indicates successful termination


} // end main




0
bcladdCommented:
What is different between the values of firstarg and secondarg from the half-loop and from the main loop?

What do you do  BEFORE the loop that you don't repeat at the bottom of the loop? I will give you two hints: -- and --

-bcl
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
a_migdalAuthor Commented:
thanks a **** load that was very helpful!!!!!!!!!!!!!!!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.