Solved

Ending a while loop but inputting a command

Posted on 2003-12-01
7
272 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:a_migdal
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:Gratch06
ID: 9853988
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
 
LVL 11

Expert Comment

by:bcladd
ID: 9853991
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
 

Author Comment

by:a_migdal
ID: 9854040
still very much a novice at c++ could you offer some more explanation into how the code works
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:bcladd
ID: 9854088
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
 

Author Comment

by:a_migdal
ID: 9854219
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
 
LVL 11

Accepted Solution

by:
bcladd earned 125 total points
ID: 9854240
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
 

Author Comment

by:a_migdal
ID: 9854275
thanks a **** load that was very helpful!!!!!!!!!!!!!!!!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now