### using boolean with switch

Hi I'm new here, and I just started learning C++ programming. I've been looking everywhere on the internet to show me how to combine boolean values and switch() to get the test results. The program just goes to default and print out Grade 0. Am I using the bool wrong or switch wrong? Some pointers would be very appreciated, thank you.

//Test 1. Carbon content is below 0.67
//Test 2. Rockwell hardness is no less than 50
//Test 3. Tensile strength is greater than 70,000 psi

//Grade9 = Test1 and test2 pass
//Grade0 = for all other cases.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double c;
double R;
double T;
bool t1;
bool t2;
bool t3;
int total;

int main()
{
cout << "Please input the carbon content: "; cin >> c;
cout << "\nPlease input the Rockwell hardness: "; cin >> R;
cout << "\nPlease input the Tensile strength: "; cin >> T;

// definitions of tests
t1 = c < 0.67;
t2 = R >= 50;
t3 = T >= 70000;
total = t1 += t2 += t3;

switch( total )
{
case 111: cout << "The metal is Grade 10" << endl; break;
case 110: cout << "The metal is Grade 9" << endl; break;
case 100: cout << "The metal is Grade 8" << endl; break;
case 000: cout << "The metal is Grade 7" << endl; break;
default: cout << "The metal is Grade 0" << endl; break;
} // end switch
} //
Last edited on
I'm not sure how += works with bool values but it's not doing what you want.
`total = t1 += t2 += t3;`

Is this what you want?
`total = t1 * 100 + t2 * 10 + t3;`
yes, you are using the booleans wrong. instead of:

bool t1, t2, t3;

use

int t1, t2, t3;
It finally works, but I don't understand why it works... I used cout to see what the output will look like, but where is that 3 coming from??

int t1(true);
int t2(true);
int t3(true);
.
.
.
// definitions of tests
if(c < 0.67) t1 += 100;
if(R >= 50) t2 += 10;
if(T >= 70000) t3 += 1;
cout << t1 + t2 + t3 << endl;

switch( t1 + t2 + t3 )
{
case 114: cout << "The metal is Grade 10" << endl; break;
case 113: cout << "The metal is Grade 9" << endl; break;
case 103: cout << "The metal is Grade 8" << endl; break;
case 003: cout << "The metal is Grade 7" << endl; break;
default: cout << "The metal is Grade 0" << endl; break;
} // end switch
Last edited on
true converted to int is 1 so t1, t2 and t3 will all be initialized to 1. Now it shouldn't be hard to see why you get 3.
thanks for clarifying that.
Topic archived. No new replies allowed.