Changing String to bool?

Hi I want to change string to bool but I'm having trouble doing this. Any help is much appreciated.





#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int check_Pythag(int DIMA, int DIMB, int DIMC)
{
if (DIMA*DIMA == DIMB*DIMB + DIMC*DIMC) // This where the the Pythagoaras equation is that calculates whether or not it is a right angled triangle.
{
return 'Yes'; // In this section if it is a right angle the program will then return the answer "Batch no. " " Yes". If it isn't then it will reportback that the batch code was not right angled. But if DIMA is the biggest/longest then the message "Invalid Data" will appear.
}
else
{
return 'No';
}
}

int main()
{
int DIMA; int DIMB; int DIMC;
string batch;

cout << "Pythagoras Theorum" << endl << endl; // Title at the top of the program.

cout << "Enter Batch No. :- "; // This is were we enter the Batch code no.
cin >> batch;
while (batch != "0")

{

cout << "Enter Dimension A :-"; // At this stage we obtain Dimension A, B & C from the user.
cin >> DIMA;

cout << "Enter Dimension B :-";
cin >> DIMB;

cout << "Enter Dimension C :-";
cin >> DIMC;

if (DIMA <= DIMB || DIMA <= DIMC) // This calculates if Dimension A is indeed the longest (or biggest).
{
cout << "Invalid Data"; // If DimensionA is not longest report error to user - "Invalid Data".
std::cin.ignore(INT_MAX);
cin.get(); // This code is here as once you input Dimension C the program would close.
}
else

if (check_Pythag(DIMA, DIMB, DIMC) == 'Yes')
{
cout << endl << "Batch No. " << batch << " Yes"; // This message will be displayed if the Batch code No. entered equals a right angle.
cout << endl << endl;
}
else
{
cout << endl << "Batch No. " << batch << " No"; // This message will be displayed if the Batch code No. entered doe's not equal a right angle.
cout << endl << endl;
}
cout << "Enter Another Batch No. (0 to Exit) :- "; // This message prompts the user for another Batch of numbers.
cin >> batch;
}
}
Change check_Pythag() to return bool. Then print yes/no when you call it.
> I want to change string to bool but I'm having trouble doing this.

You want one of three possible results: Yes, No or Invalid_Data.
Varaibles of type bool can hold only one of two distinct values; they are either true or false.

Use an enumeration with three distinct values. http://en.cppreference.com/w/cpp/language/enum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <utility> // std::swap

enum pythag_result_type { YES, NO, INVALID_DATA };

// this overloads the stream insertion operator for pythag_result_type
// you may want to ignore this for the present
// we can get equivalent output by writing the switch-case in main
std::ostream& operator << ( std::ostream& stm, pythag_result_type result ) ;

pythag_result_type check_pythag( int a, int b, int c )
{
    // invalid if any side is non-positive
    if( (a<=0) || (b<=0) || (c<=0) ) return INVALID_DATA ;

    // make a the largest of the three sides
    if( a < b ) std::swap(a,b) ; // a is now that larger of(a,b), and b is the smaller of of(a,b)
    if( a < c ) std::swap(a,c) ; // a is now that largest of(a,b,c)

    if( a >= (b+c) ) return INVALID_DATA ; // not sides of a triangle

    return a*a == b*b + c*c ? YES : NO ;
}

int main()
{
    const pythag_result_type result = check_pythag( 20, 15, 25 ) ;
    switch(result) // switch on result
    {
        case YES: std::cout << "Yes\n" ; break ;
        case NO:  std::cout << "No\n" ; break ;
        default: std::cout << "Invalid data\n" ;
    } // Yes

    std::cout << check_pythag( -4, 3, 5 ) << '\n' // Invalid data
              << check_pythag( 4, 0, 4 ) << '\n' // Invalid data
              << check_pythag( 21, 220, 221 ) << '\n' // Yes
              << check_pythag( 4, 3, 5 ) << '\n' // Yes
              << check_pythag( 6, 3, 5 ) << '\n' // No
              << check_pythag( 4, 1, 5 ) << '\n' ; // Invalid data

}

// overloaded stream insertion operator for pythag_result_type
std::ostream& operator << ( std::ostream& stm, pythag_result_type result )
{
    switch(result)
    {
        case YES: return stm << "Yes" ;
        case NO:  return stm << "No" ;
        default: return stm << "Invalid data" ;
    }
}

http://coliru.stacked-crooked.com/a/4eed3e9b4f142b0c

If you haven't yet come across enumerations, and want to postpone learning about them for another few days, use three distinct integer values to represent each of the three possible results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <utility> // std::swap

const int YES = 1 ;
const int NO = 0 ;
const int INVALID_DATA = -1 ;

int check_pythag( int a, int b, int c )
{
    // invalid if any side is non-positive
    if( (a<=0) || (b<=0) || (c<=0) ) return INVALID_DATA ;

    // make a the largest of the three sides
    if( a < b ) std::swap(a,b) ; // a is now that larger of(a,b), and b is the smaller of of(a,b)
    if( a < c ) std::swap(a,c) ; // a is now that largest of(a,b,c)

    if( a >= (b+c) ) return INVALID_DATA ; // not sides of a triangle

    return a*a == b*b + c*c ? YES : NO ;
}

int main()
{
    const int result = check_pythag( 20, 15, 25 ) ;
    switch(result) // switch on result
    {
        case YES: std::cout << "Yes\n" ; break ;
        case NO:  std::cout << "No\n" ; break ;
        default: std::cout << "Invalid data\n" ;
    } // Yes
}
Topic archived. No new replies allowed.