Solved

Ending a while loop but inputting a command

Posted on 2003-12-01
7
271 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

706 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

19 Experts available now in Live!

Get 1:1 Help Now