Any Efficient way to do this

The program tells you what you can do depending on your age.
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
/*This is basically the what the program is supposed to do in the windows form application*/
//special thanks to cynthia-ga (http://answers.google.com/answers/threadview/id/608781.html)

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int x; //x=age
	cout << "This program tells you your rights depending on your age" << endl;
	cout << "please enter age:" << endl;
	cin >> x;

	if (x <= 0)
	{
		cout << "YOU ARE STILL IN YO MAMA'S WOMB STOP USING THE COMPUTER AND GO BACK TO DEVELOPING" << endl;
	}
	if ((x > 0) && (x < 5)) // less than 5 greater than 0
	{
		cout << "your age is " << x << endl;
		cout << " you may apply for SSN" << endl;
	}
	if ((x >= 5) && (x < 8)) //5
	{
		cout << "your age is " << x << endl;
		cout << " you may apply for SSN" << endl;
		cout << " Kindergarten, enter public (or private) school" << endl;
	}
	if ((x >= 8) && (x < 12)) //8
	{
		cout << "your age is " << x << endl;
		cout << " you may apply for SSN" << endl;
		cout << " Kindergarten, enter public (or private) school" << endl;
		cout << " Age of reason - legal age to know right from wrong/lie vs. Truth in court of law" << endl;
	}
	if ((x >= 12) && (x < 13))//12
	{
		cout << "your age is " << x << endl;
		cout << " you may apply for SSN" << endl;
		cout << " Kindergarten, enter public (or private) school" << endl;
		cout << " Age of reason - legal age to know right from wrong/lie vs. Truth in court of law" << endl;
		cout << " Most airlines allow child to travel alone" << endl;
	}
	system("pause");
	return 0;
}


This is a snipet of the work but it runs until age 70 and the older you get the more stuff you can do.
Last edited on
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
   const int SIZE = 5;
   int lower[SIZE] = { 0, 5, 8, 12, 13 };
   string message[SIZE-1] = { "You may apply for SSN",
                              "Kindergarten, enter public (or private) school",
                              "Age of reason - legal age to know right from wrong/lie vs. Truth in court of law",
                              "Most airlines allow child to travel alone" };

   int age;
   cout << "This program tells you your rights depending on your age" << endl;
   cout << "Please enter age: ";
   cin >> age;

   if ( age <= 0 )
   {
      cout << "Wait a little longer before coming into this cold, cold world" << endl;
   }
   else if ( age >= lower[SIZE-1] )
   {
      cout << "You can pay taxes like the rest of us" << endl;
   }
   else
   {
      int n = 0;
      cout << "Your age is " << age << endl;
      while ( age >= lower[n] ) cout << message[n++] << endl;
   }
}
lastchance

Wait I have a question wont that only work on a 1-1 basis? like at age 5 you get 5 rights and at age 6 you get 6 rights...

But what if you get 2 additional rights when you go from age 6 to 7?
I would implement this as a table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Rights
{   int     minage;
    int     maxage;
    string  what;
};
const int NUM_RIGHTS = 4;

const Rights rights[NUM_RIGHTS] = 
    {   1, 100, "You may apply for a SSN",
        5, 8,   "Kindergarten, enter public (or private) school",
        8, 12,  "Age of reason - legal age to know right from wrong/lie vs. Truth in court of law",
        12, 13, "Most airlines allow child to travel alone"
    };

//  Display all the rights for a specific age
void display_rights (int age)
{   for (int i=0; i<NUM_RIGHTS; i++)
        if (age >= rights[i].minage && age <= rights[i].maxage)
            cout << rights[i].what << endl;
}

Wait I have a question wont that only work on a 1-1 basis?


Hello, @MegsD,
Have you tried it?

Apart from the facetious comment about what you can do after age 13 it is doing what your original program did: adding to the list of additional rights at certain ages (though what a 12-year-old is doing in Kindergarten is anybody's guess). If you want to cut them off at certain ages then do as @AbstractionAnon suggests - his code is much more flexible than mine. He probably anticipated what you might want better than I did.

like at age 5 you get 5 rights and at age 6 you get 6 rights...

No; the while loop is working on how many lower bounds are <= age, not indexing on age itself.

But what if you get 2 additional rights when you go from age 6 to 7?

That seems to be something you have just changed from the original question.
If you have two extra rights then in my code simply put a newline character in the middle of the message string; thus: "This is the first right\nThis is the second right"
@AbstractionAnon's code would deal with this without change.
Last edited on
Thanks for clearing it up @lastchance
also thanks @AbstractionAnon
there are 2 ways to make this type of logic problem more efficient.

a switch statement with fall-through can trigger multiple successes.

or

rewire the if-then logic to do the same type of thing. You can combine conditions in all kinds of ways to cut the amount of comparisons way down. Drawing a truth table or k-map can help correlate weird logic that is not obvious to boil it down farther.



Topic archived. No new replies allowed.