title: Getting input from user with Switch C++
body: <p>I have a system where user can enter as many inputs as s/he wants and make some calculations.</p>
  
  <p>Here is the code for achieving this task:</p>
  
  <pre><code>int main() {
      char op = 's';
      float time=0, fuel_rate=0, start=0, end=0, pace=0;
  
  
      while(op != 'x'){
          cout &lt;&lt; "Please select: " &lt;&lt; endl;
          cout &lt;&lt; "1 ---&gt; A" &lt;&lt; endl;
          cout &lt;&lt; "2 ---&gt; B" &lt;&lt; endl;
          cout &lt;&lt; "3 ---&gt; Calculate" &lt;&lt; endl;
          cout &lt;&lt; "x ---&gt; Exit" &lt;&lt; endl;
  
          op = std::getchar();
  
          //remove the rest of the line from input stream
          int temp;
          while ( (temp = std::getchar()) != '\n' &amp;&amp; temp != EOF );
  
          switch(op){
          case '1':
              cout &lt;&lt; "Enter time: ";
              cin &gt;&gt; time;
              cout &lt;&lt; "Enter fuel rate: ";
              cin &gt;&gt; fuel_rate;
              break;
          case '2':
              cout &lt;&lt; "Enter start: ";
              cin &gt;&gt; start;
              cout &lt;&lt; "Enter end: ";
              cin &gt;&gt; end;
              cout &lt;&lt; "Enter pace: ";
              cin &gt;&gt; pace;
              cout &lt;&lt; "Enter fuel rate: ";
              cin &gt;&gt; fuel_rate;
              break;
          case '3':
              cout &lt;&lt; "Total value";
              break;
          case 'x':
              return 0;
          default:
              continue;
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>System works well for the first input. Sample console log looks like this:</p>
  
  <pre><code> Please select: 
  1 ---&gt; A
  2 ---&gt; B
  3 ---&gt; Calculate
  x ---&gt; Exit
  1
  Enter time: 2
  Enter fuel rate: 3
  Please select: 
  1 ---&gt; A
  2 ---&gt; B
  3 ---&gt; Calculate
  x ---&gt; Exit
  2
  Please select: 
  1 ---&gt; A
  2 ---&gt; B
  3 ---&gt; Calculate
  x ---&gt; Exit
  </code></pre>
  
  <p>First user enters the operation 1, system asks for time and fuel rate. When user enters the operation 2, system doesn't ask for start, end or pace. </p>
  
  <p>Any ideas on how to solve this?</p>