I have to convert this program that I wrote to use switch case statements to handle user input (+, -, /, *, X). Any ideas?

#include <iostream>
#include <string>

using namespace std;

int main()
{
	double total = 0, counter = 0;
	char sign, X = 0;
	double value;

		do 
		{
			
			cout << "Current total is " << total << endl;
						
			cout << "Enter an operation: + - * / (or enter X to exit):";
			cin >> sign;

			if (sign != 'X')
			{
				cout << "Enter a number: ";
				cin >> value;
				cin.ignore();

				if (value <= 0)
				{
					cout << "Can not divide by zero! "; 
					cout << endl;
				}

				else
				{
				if (sign == '+')
				{
					total += value;
				}
				else
				{
				if (sign == '-')
				{
					total -= value;
				}
					
				else
				{
				if (sign == '*')
				{
					total *= value;
				}
						
				else 
				{
				if ((sign == '/') && (value != 0))
				{
					total /= value;
				}
							

				}
				}
				}
				}
			
		}
	}
		 
		while (sign != 'X');
		
		return (0);	
	
			
}

Recommended Answers

All 8 Replies

Well Its very easy, If you know how switch statements work.

char x;
cin>>x;
switch (x)
{
case: 'a'
cout<<"x==a";
break;
case: 'b'
cout<<"x==b";
break;
default:
cout<<"no match found";
break;

This is a small example of how you can use switch statements hope it helps.

Might be something like this

switch(sign)
{
  case 11: {
                  //lines here
                  break;
                }

  case 22: {
                  //lines here
                  break;
                }
  .
  .
  .

  default: {
               }
/*In the default's brackets, you should have what the control structure, switch(), will do if the variable, 'sign' in this case, doesn't match one of your cases values. you can leave it with no lines too if you don't want it to perform any actions*/
/* I've just used 11 and 22 as an example of the ASCII values for the input character you wanna evaluate, just look up the values for '*', '/', '+', '-', 'X', etc.  
don't forget about the break; at the end of each set of statements for the respective case. default: doesn't need a break;
}

Ok. I am getting there. If I do divide by 0, it does say can not divide by zero!, but then the running total is messed up.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	double total = 0, counter = 0;
	char sign, X = 0;
	double value;

	for (;;){

		do 
		{
			cout << "Current total is " << total << endl;
						
			cout << "Enter an operation: + - * / (or enter X to exit):";
			cin >> sign;

		}while ((sign != '+') && (sign != '-') && (sign != '*') && (sign != '/') && (sign != 'X'));


			switch (sign)
			{
			case 'X':
				{
				return 0;
				break;
				}

			case '+':
				{
				cout << "Enter a number: ";
				cin >> value;
				cin.ignore();
					total += value;
				}
				break;
				
			case '-':
				{
				cout << "Enter a number: ";
				cin >> value;
				cin.ignore();
					total -= value;
				}
				break;
					
			case '*':
				{
				cout << "Enter a number: ";
				cin >> value;
				cin.ignore();
					total *= value;
				}
				break;
						
			case '/':
				{
				if (value != 0)
				cout << "Enter a number: ";
				cin >> value;
				cin.ignore();
					total /= value;
				}
				
				if (value == 0)
				{
				cout << "Can not divide by zero! "; 
				cout << endl;
				}
				break;

			default:
				{
				return (0);
				}
		

				
		}
	}
			
}

This should work

#include <iostream>
#include <string>

using namespace std;

int main()
{
  double total = 0, counter = 0;
  char sign, X = 0;
  double value;

  for (;;)
  {
    do
    {
      cout << "Current total is " << total << endl;
      cout << "Enter an operation: + - * / (or enter X to exit):";
      cin >> sign;
    }
    while( (sign != '+') && (sign != '-') && (sign != '*') && (sign != '/') && (sign != 'X'));

    switch (sign)
    {
      case 'X':
        return 0;
      case '+':
        cout << "Enter a number: ";
        cin >> value;
        cin.ignore();
        total += value;
        break;
      case '-':
        cout << "Enter a number: ";
	cin >> value;
        cin.ignore();
        total -= value;
        break;
      case '*':
        cout << "Enter a number: ";
        cin >> value;
        cin.ignore();
        total *= value;
        break;
      case '/':
        if (value != 0)
        {
          cout << "Enter a number: ";
          cin >> value;
          cin.ignore();
          total /= value;
        }
        if (value == 0)
        {
          cout << "Can not divide by zero! ";
          cout << endl;
        }
        break;
      default:
        return (0);
    }
  }
}

You don't need to enclose your group of statements for each case with {}, the break; interrupts the switch() and it ends there, when it meets the break; That's why without the break;, it would go from where the matched case starts to last sentence of the last case or to the next break; it find in that switch().

Thanks emotionalone, but I still get the same thing.

I got it. It is:

case '/':
		cout << "Enter a number: ";
        cin >> value;
		cin.ignore();

        if (value != 0)
            total /= value;
        else
        {
             cout << "Can not divide by zero! " << endl;
        }
		break;

^_^

I had not tried dividing by zero.

I don't really know how iostream's functions work, sorry I couldn't be of help with that. =)

My teacher told us now that we can no longer use return (). in my program i need to enter X to exit the program. I did have it working correctly but how do i end the program if the user enters X without a return (0)?

#include <iostream>
#include <string>

using namespace std;

int main()
{
  double total = 0, counter = 0;
  char sign, X = 0;
  double value;

  for (;;)
  {
    do
    {
      cout << "Current total is " << total << endl;
      cout << "Enter an operation: + - * / (or enter X to exit):";
      cin >> sign;
    }
    while( (sign != '+') && (sign != '-') && (sign != '*') && (sign != '/') && (sign != 'X'));

	if (sign != 'X')
	{
		cout << "Enter a number: ";
		cin >> value;
		cin.ignore();
	}

    switch (sign)
    {
      case 'X':
        return 0;
		break;

      case '+':
		total += value;
        break;

		case '-':
		total -= value;
		break;

		case '*':
		total *= value;
		break;

		case '/':
		if (value != 0)
		total /= value;
		else
		{
			cout << "Can not divide by zero! " << endl;
		}
		break;

		default:
		return (0);
    }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.