You are here:

C++/C++ Programming and Problem Solving


 I'm having problems writing a program with a switch statement. I'm not real clear on how to write the question so I hope you bear with me. I have to call a gas pump with the different types of gas. I have a little code if this might help you. I' not sure if this helps but I would appreciate it if you can help me. I'm using a C++ Compiler. Thank you for your time.

#include <iostream.h>

Fuel Type();

int main()
  cout << Your fuel type(regular, plus, super, preimum)."<<endl;    

for (i=1;i<=5; i++)
Fuel Type
case'87':  cout << "regular";

case'89':  cout << "plus";

case'93':  cout << "super";
default:   cout << fuel << " is premium fue

I'm not quite sure what you're asking. Are you having problems with the syntax? Here is an example that uses the return value of a menu function as the condition of the switch statement and loops five times:

#include <iostream>

using namespace std;

int fuel_type()
   int type;

   cout<<"1. Regular\n"
       <<"2. Plus\n"
       <<"3. Super\n"
       <<"4. Premium\n"
       <<"Selection: ";
   cin>> type;

   return type;

int main()
   for (int i = 0; i < 5; i++) {
       switch (fuel_type()) {
       case 1:
         cout<<"You chose Regular grade"<<endl;
       case 2:
         cout<<"You chose Plus grade"<<endl;
       case 3:
         cout<<"You chose Super grade"<<endl;
       case 4:
         cout<<"You chose Premium grade"<<endl;
         cerr<<"Unrecognized grade\n";

If that doesn't help, I'm afraid I'll need more detail as to what your problem is.



All Answers

Answers by Expert:

Ask Experts


Sandra Guth


I can help with any questions concerning ISO standard C++, object oriented programming and design as well as algorithms and data structures.


I have nine years of programming experience with C++.

©2016 All rights reserved.