?
Solved

Ending a while loop but inputting a command

Posted on 2003-12-01
7
Medium Priority
?
292 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

612 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