If else statement using single cout

Write a program to decide the class of family depending on its yearly income using if else with single cout
The various classes are :

< Rs 50000 very poor
50K to 100K poor
100K to 300K lower middle and so on (total 7 classes)

I will write the code myself but my doubt is how to use single cout to decide the class of family
As much as I know. I will need 7 couts for the 7 classes but how to do it using single cout
Last edited on
@shivamjain

Something along the line of...
1
2
3
4
5
6
7
8
9
10
string family_class;
if(income <50000)
  family_class = "very poor";
else if(income>=50000 && income <100000)
  family_class = "poor";
else if(income>=100000 && income <300000)
  family_class = "middle class";
// etc. to end of income checks

cout << "The family class is being listed as " << family_class<< " according to their income." <<endl;
Last edited on
It sounds like you will need something like
1
2
3
4
5
6
7
8
9
10
if (condition)
    action1
else if (condition)
    action2
else if (condition)
    action3
...
etc
else
    action7


In that part you will test some input value and assign an output value.

after that, use cout to output the value previously stored.
@whitenite1:
If your code has to evaluate line 4, then we know that line 2 did evaluate to false.
If <N is false, then >=N is implicitly true and does not need testing.

@shivajain1:
There is an underlying principle: computation and output are best kept separate.
The computation should do exactly that: compute a result.
When and how the result is used (for example shown) is an unrelated matter.
The only thing required is that the computation stores the result (in variables) and the output has access to the stored results.
That concept applies to input also. Get the input somewhere else. Compute something. Return *result* (not always an output). Do something with the result (possibly, output, or feed forward to more computations, does not matter). This will save you a great deal of pain someday.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main()

{
    auto incomeBracket = [](const double income)
    {
        using namespace std::string_literals;
        if(income <= 50'000)    return "very poor"s;
        else if (income > 50'000 && income <= 100'000)return "poor"s;
        else if (income > 100'000 && income <= 300'000) return "lower middle"s;
        else if (income > 300'000 && income <= 400'000) return "middle"s;
        else if (income > 400'000 && income <= 500'000) return "upper middle"s;
        else if (income > 500'000 && income <= 600'000) return "rich"s;
        else return "upper rich"s;
    };
    
    std::cout << incomeBracket (250'000) << "\n";
}


http://coliru.stacked-crooked.com/a/f9cd7f4fed27d4af
I'd get rid of the extra duplicated comparisons in the if-else block.

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
#include <iostream>
#include <string>
#include <iomanip>

int main()
{
    auto incomeBracket = [](const double income)
    {
        if      (income <= 50000)  return "very poor";
        else if (income <= 100000) return "poor";
        else if (income <= 300000) return "lower middle";
        else if (income <= 400000) return "middle";
        else if (income <= 500000) return "upper middle";
        else if (income <= 600000) return "rich";
        else                       return "upper rich";
    };
    
    std::cout << std::fixed << std::setprecision(2);
    
    for (double income : {  40000.00,  49999.99,  50000.00,  50000.01, 
                            80000.00, 100000.00, 200000.00, 250000.00, 
                           350000.00, 450000.00, 550000.00, 599999.99, 
                           600000.00, 600000.01, 700000.00 } )
        
        std::cout << std::setw(15) << income 
            << "   " << incomeBracket (income) << '\n';
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
   int   upper[] = {    50000   , 100000,     300000    ,  400000 ,     500000    , 600000,   1000000    };   // last isn't used
   string text[] = { "very poor", "poor", "lower-middle", "middle", "upper-middle", "rich", "super-rich" };
   int N = sizeof( upper ) / sizeof( upper[0] );

   int income;
   cout << "Input family income: ";
   cin >> income;

   int i = 0;
   while( i < N - 1 && income >= upper[i] ) i++;

   cout << "Your income bracket is " << text[i];
}
lastchance; you'd certainly get my prize for the most elegant solution for this one! I was thinking on similar lines but couldn't quite nail it. well done!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
   int   limit[] = {    50000   , 100000,     300000    ,  400000 ,     500000    , 600000  };  // at these boundaries ... go up to next
   string text[] = { "very poor", "poor", "lower-middle", "middle", "upper-middle", "rich", "super-rich" };   // deliberately one more
   int N = sizeof( limit ) / sizeof( limit[0] );

   int income;
   cout << "Input family income: ";
   cin >> income;
   cout << "Your income bracket is " << text[ upper_bound(limit,limit+N,income) - limit ];
}
There are 10 sorts of people: those who like PODs and those who like standard containers :)
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 <map>
#include <string>

void waitForEnter();

int main()
{
    std::map<double, std::string> family { {    50'000.00, "programmer"   },
                                           {   100'000.00, "poor"         },
                                           {   300'000.00, "lower-middle" },
                                           {   400'000.00, "middle"       },
                                           {   500'000.00, "wealthy"      },
                                           {   600'000.00, "politician"   },
                                           { 1'000'000.00, "plumber"      } };
    std::cout << "Input family income: ";
    double income;
    std::cin >> income;
    std::cout << "Your income bracket is " 
              << (family.upper_bound(income) == family.end() ? 
                  family.rbegin()->second : family.upper_bound(income)->second)
              << '\n';
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Ok, I just copied lastchance’s code, I admit it.
Topic archived. No new replies allowed.