Help w/ Friendship Algorithm please.

Pages: 1234
the one i posted above doesnt add up the points. it just says you scored a zero unless i enter 3 for the first question then it says 1 but it doesnt for the others. i just cant get this program to work to the end even if it is wrong in some places. it wont even run according to my yes, no , 1,2,3 answers. ill keep trying though
Last edited on
i keep changing switching and moving things around but x code hates me. i want to understand why something works so i can add it to my c++ arsonal so if/when i get it to work i want to study it
closed account (48T7M4Gy)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <string>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void updateFriendScore( int&, int );
char responder();

int main()
{
	int a = 1, b = 2, c = 3;
	int num = 0, FriendScore = 0;
	char response = 'Y', foodQ = '0', sportsQ = '0', moviesQ = '0', beverageQ = '0';

	cout << "~ Phil's Friendship Algorithm ! ~ " << endl;
	cout << "Calling potential friend.." << endl;
	cout << "Brrrrring....." << endl;
	cout << "Brrrrring....." << endl;
	cout << "Brrr...... Hello ?" << endl;

	// TRY FOOD
	cout << "Hey ! Do you want to get some food? (y/n): " << endl;
	foodQ = responder();

	if ( foodQ == 'Y' )
	{
		cout << "Yay! What do you prefer ?" << "(1-Seafood, 2-steak, or 3-vegetarian)";
		cin >> num;
		updateFriendScore( FriendScore, num );

		cout << "Cool let's grub out . I'll pick you up.";
		cout << " Awesome see you in a bit.. friend ;) ! " << endl;
	}
	else
	{
		//TRY SPORTS
		cout << "Ok no problem. Do you like sports? (y/n): ";
		sportsQ = responder();
		if ( sportsQ == 'Y' )
		{
			cout << "Heck Ya ! Me too ! What do you want to play ?" << "(1-Basketball, 2-Baseball, 3-Football) ";
			cin >> num;
			updateFriendScore( FriendScore, num );
		}
		else
		{
			//TRY MOVIES
			cout << "Alright. I'm tired anyways. How about a movie ?! (y/n) ";
			moviesQ = responder();
			if ( moviesQ == 'Y' )
			{
				cout << "Awesome ! What type of movie do you want to see ?" << "(1-Action, 2-Suspense, 3-Romance) ";
				cin >> num;
				updateFriendScore( FriendScore, num );
				cout << "Cool ! See you in a bit ! I'll bring the popcorn ! " << endl;
			}
			else
			{
				//TRY BEVERAGES
				cout << "Ya movie tickets are pretty expensive. How about we get something to drink ? Nothing expensive or tiring. What do you say ? (y/n)";
				beverageQ = responder();

				if ( beverageQ == 'Y' )
				{
					cout << " Cool lets do it. What kind of beverage do you want to get ? (1-Coffee, 2-Beer, 3-Tea) ";
					cin >> num;
					updateFriendScore( FriendScore, num );
					cout << "Cool, I'll come by and pick you up. See ya in a bit ! ";
					cout << " O Wait !! Before I go, I wanted to tell you not to bring any money. I'll pay since we're new friends. Is that ok ?" << "(1-Yes, 2-Maybe, 3-No) ";
					cin >> num;
					updateFriendScore( FriendScore, num );
				}
				else
				{
					cout << "Darn. Alright well I guess I'll talk to you later. " << endl;
				}
			}
		}
	}

	cout << "You scored a " << FriendScore << " ! This score represents how good of friends you will be !" << endl;

	system("pause");

	return 0;
}

char responder()
{
	char reply = '0';
	do
	{
		cin >> reply;
		reply = toupper(reply);
	} while ( reply != 'Y' && reply != 'N' );
	return toupper( reply );
}

void updateFriendScore( int &currentTotal, int x )
{
	int score = 0;

	if (x == 1)
		score = 10;
	else if (x == 2)
		score = 5;
	else if (x == 3)
		score = 1;
	else
		score = 0;

	currentTotal += score;
	cout << "*** Current score is " << currentTotal << " ***" << endl;

	return;
}
Last edited on
ooo ok i see. the scoring system works now. i am going to go and figure out what made it work now and study what a responder() is. thank you sir. How come when i answered no for the first question it skipped all the way to the movie part ?
-also i need 3 questions to be dependent on the one before it so thats why i had all those whiles and else ifs .
- but this keeps on going down program where it should end & finish and say the score once a yes and then what i want to do/eat/drink (1,2,3) is answered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
~ Phil's Friendship Algorithm ! ~ 
Calling potential friend..
Brrrrring.....
Brrrrring.....
Brrr...... Hello ?
Hey ! Do you want to get some food? (y/n): 
n
Alright. I'm tired anyways. How about a movie ?! (y/n) y
Awesome ! What type of movie do you want to see ?(1-Action, 2-Suspense, 3-Romance) 3
*** Current score is 1 ***
Cool ! See you in a bit ! I'll bring the popcorn ! 
Ya movie tickets are pretty expensive. How about we get something to drink ? Nothing expensive or tiring. What do you say ? (y/n) n
You scored a 1 ! This score represents how good of friends you will be !
sh: pause: command not found 
closed account (48T7M4Gy)
i am going to go and figure out what made it work now

Good

and study what a responder() is.

Keep in mind 'responder' is just a name I made up. I could have called it 'magical_wonder_machinery' Just look at what it does and you'll see that it saves a lot of duplication back in main.

How come when i answered no for the first question it skipped all the way to the movie part ?

Simple, because there is no if statement saying if ( xyz == 'N' )

-also i need 3 questions to be dependent on the one before it so thats why i had all those whiles and else ifs .

Well you might be making life difficult I suspect by adding those structures. The only place I can see for a while is to force a y/n answer ie if it's not one or the other then keep asking. My suggestion would be to extend the if's eg if (sportsQ == 'Y' and foodQ == 'N' ) at least think about it. Work backwards and ask what conditions apply to get into this section.

- but this keeps on going down program where it should end & finish and say the score once a yes and then what i want to do/eat/drink (1,2,3) is answered.

Que?
lol ok ill work from the bottom up and get back on here tomorrow. last thing tonight please, for the last part where you said 'Que' lol.. i meant the program asks every single question that i wrote. i want it to end and say the score once the user enters Y for yes. because think about it. if i called you on the phone and asked do you want to get some food and you say yes, then i wouldnt ask all the other questions. But in this program it asks every question even if you type Y. do you see what i mean ?
Last edited on
i fixed a couple things . here is what it ouputs which is almost right but not quite, once i entered 2 it said... current score 5.... cool see you in a bit !... then it said darn alright well i guess well talk later. i only want "darn etc etc" to output if the user enters no for all the questions. i want the final "score" outputted and the program to end once the user enters yes and a number for an answer like i did below . ya know ?
~ Phil's Friendship Algorithm ! ~ 
Calling potential friend..
Brrrrring.....
Brrrrring.....
Brrr...... Hello ?
Hey ! Do you want to get some food? (y/n): 
n
Ok no problem. Do you like sports? (y/n): n
Alright. I'm tired anyways. How about a movie ?! (y/n) y
Awesome ! What type of movie do you want to see ?(1-Action, 2-Suspense, 3-Romance) 2
*** Current score is 5 ***
Cool ! See you in a bit ! I'll bring the popcorn ! 
Darn. Alright well I guess I'll talk to you later. 
You scored a 5 ! This score represents how good of friends you will be !


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <string>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void updateFriendScore(int&, int);
char responder();

int main()
{
    int a = 1, b = 2, c = 3;
    int num = 0, FriendScore = 0;
    char response = 'Y', foodQ = '0', sportsQ = '0', moviesQ = '0', beverageQ = '0';
    
    cout << "~ Phil's Friendship Algorithm ! ~ " << endl;
    cout << "Calling potential friend.." << endl;
    cout << "Brrrrring....." << endl;
    cout << "Brrrrring....." << endl;
    cout << "Brrr...... Hello ?" << endl;
    
    
    // TRY FOOD
    cout << "Hey ! Do you want to get some food? (y/n): " << endl;
    foodQ = responder();
    
    if (foodQ == 'Y')
    {
        cout << "Yay! What do you prefer ?" << "(1-Seafood, 2-steak, or 3-vegetarian)";
        cin >> num;
        updateFriendScore(FriendScore, num);
        
        cout << "Cool let's grub out . I'll pick you up.";
        cout << " Awesome see you in a bit.. friend ;) ! " << endl;
    }
    
    if (foodQ != 'Y')
    {
        //TRY SPORTS
        cout << "Ok no problem. Do you like sports? (y/n): ";
        sportsQ = responder();
    }
    
    if (sportsQ == 'Y')
    {
        cout << "Heck Ya ! Me too ! What do you want to play ?" << "(1-Basketball, 2-Baseball, 3-Football) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
    }
    
    if (sportsQ != 'Y')
    {
        //TRY MOVIES
        cout << "Alright. I'm tired anyways. How about a movie ?! (y/n) ";
        moviesQ = responder();
    }
    
    if (moviesQ == 'Y')
    {
        cout << "Awesome ! What type of movie do you want to see ?" << "(1-Action, 2-Suspense, 3-Romance) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
        cout << "Cool ! See you in a bit ! I'll bring the popcorn ! " << endl;
    }
    
    if (moviesQ != 'Y')
    {
        //TRY BEVERAGES
        cout << "Ya movie tickets are pretty expensive. How about we get something to drink ? Nothing expensive or tiring. What do you say ? (y/n) ";
        beverageQ = responder();
    }
    
    if (beverageQ == 'Y')
    {
        cout << " Cool lets do it. What kind of beverage do you want to get ? (1-Coffee, 2-Beer, 3-Tea) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
        cout << "Cool, I'll come by and pick you up. See ya in a bit ! ";
        cout << " O Wait !! Before I go, I wanted to tell you not to bring any money. I'll pay since we're new friends. Is that ok ?" << "(1-Yes, 2-Maybe, 3-No) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
    }
    
    if (beverageQ != 'Y')
        cout << "Darn. Alright well I guess I'll talk to you later. " << endl;
    
    cout << "You scored a " << FriendScore << " ! This score represents how good of friends you will be !" << endl;
    
    
    return 0;
}

char responder()
{
    char reply = '0';
    cin >> reply;
    return toupper(reply);
}

void updateFriendScore(int &currentTotal, int x)
{
    int score = 0;
    
    if (x == 1)
        score = 10;
    if (x == 2)
        score = 5;
    if (x == 3)
        score = 1;
    
    currentTotal += score;
    cout << "*** Current score is " << currentTotal << " ***" << endl;
    
    return;
}
Last edited on
alright im goin to bed man. ill talk to you tomorrow. thanks for the help today have a good night buddy
closed account (48T7M4Gy)
But in this program it asks every question even if you type Y. do you see what i mean ?


Yeah I know but as I said it depends how you structure the if( what goes inhere && other stuff && other tests). while statements are probably unnecessary duplication/complication

Just imagine the cascade as separate pathways requiring various codes (answers or combinations of answers) to open the gate. If no gates open you end up at the finish line very quickly. Use you flowchart and that should show you which keys are required at each junction.
Just imagine the cascade as separate pathways requiring various codes (answers or combinations of answers) to open the gate. If no gates open you end up at the finish line very quickly. Use you flowchart and that should show you which keys are required at each junction.

ok man i will do this tomorrow and spend a good few hours after i get done with my classes. i think i can do it !! thanks kemort have a good night !! see ya !
closed account (48T7M4Gy)
Phil, just have a look, I know you won't just copy it.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <string>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::string;

void updateFriendScore(int&, int);
char getResponse(string, char, char);
int getSelection(string, string);

int main()
{
	int FriendScore = 0;
	char response = 'Y';
	string question = "";

	cout << "~ Phil's Friendship Algorithm ! ~ " << endl;
	cout << "Calling potential friend.." << endl;
	cout << "Brrrrring....." << endl;
	cout << "Brrrrring....." << endl;
	cout << "Brrr...... Hello ?" << endl;

	// TRY FOOD
	if (getResponse("Hey ! Do you want to get some food? ", 'y', 'n') == 'Y')
	{
		updateFriendScore(FriendScore, getSelection("Yay! What do you prefer ?", "(1-Seafood, 2-steak, or 3-vegetarian)"));

		cout << "Cool let's grub out . I'll pick you up.";
		cout << " Awesome see you in a bit.. friend ;) ! " << endl;
	}
	else
	{
		//TRY SPORTS
		if (getResponse("Ok no problem. Do you like sports? ", 'y', 'n') == 'Y')
		{
			updateFriendScore(FriendScore, getSelection("Heck Ya ! Me too ! What do you want to play ?", "(1-Basketball, 2-Baseball, 3-Football) "));
		}
		else
		{
			//TRY MOVIES
			if (getResponse("Alright. I'm tired anyways. How about a movie ?!", 'y', 'n') == 'Y')
			{
				updateFriendScore(FriendScore, getSelection("Awesome ! What type of movie do you want to see ?", "(1-Action, 2-Suspense, 3-Romance) "));
				cout << "Cool ! See you in a bit ! I'll bring the popcorn ! " << endl;
				updateFriendScore(FriendScore, getSelection("Cool ! I'll bring the popcorn ! Can you bring the drinks ?", "(1-Yes, 2-Maybe, 3-No) "));
			}
			else
			{
				//TRY BEVERAGES
				if (getResponse("Ya movie tickets are pretty expensive. How about we get something to drink ? Nothing expensive or tiring. What do you say ? ", 'y', 'n') == 'Y')
				{
					updateFriendScore(FriendScore, getSelection(" Cool lets do it. What kind of beverage do you want to get ?", "(1-Coffee, 2-Beer, 3-Tea) "));
					cout << "Cool, I'll come by and pick you up. See ya in a bit ! ";
					updateFriendScore(FriendScore, getSelection(" O Wait !! Before I go, I wanted to tell you not to bring any money. I'll pay since we're new friends. Is that ok ?", "(1-Yes, 2-Maybe, 3-No) "));
				}
				else
				{
					cout << "Darn. Alright well I guess I'll talk to you later. " << endl;
				}
			}
		}
	}

	cout << "You scored a " << FriendScore << " ! This score represents how good of friends you will be !" << endl;

	system("pause");

	return 0;
}

char getResponse(string question, char positive, char negative)
{
	cout << question << endl;

	string prompt = "Please answer ( ";
	prompt += positive;
	prompt += " or ";
	prompt += negative;
	prompt += " ) ";

	cout << prompt;

	char reply = '0';
	cin >> reply;
	reply = toupper(reply);

	while (reply != toupper(positive) && reply != toupper(negative))
	{
		cout << "Sheeesh @#$!! " << prompt;
		cin >> reply;
		reply = toupper(reply);
	}

	return reply;
}

int getSelection(string menu, string prompt)
{
	cout << menu << endl;
	cout << prompt;

	int selection = 0;
	cin >> selection;

	while (selection < 1 || selection > 3)
	{
		cout << "Derrh ... !!" << prompt;
		cin >> selection;
	}

	return selection;
}

void updateFriendScore(int &currentTotal, int x)
{
	int score = 0;

	if (x == 1)
		score = 10;
	else if (x == 2)
		score = 5;
	else
		score = 1;

	currentTotal += score;
	cout << "*** Current score is " << currentTotal << " ***" << endl;

	return;
}
Last edited on
hey kemort :) guess what !!! i did it !! check it out . i will re write it so its not what you typed but i just wanted it to work how i need it to !! the flow chart helped :)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <string>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void updateFriendScore(int&, int);
char responder();

int main()
{
    int num = 0, FriendScore = 0;
    char response = 'Y', foodQ = '0', sportsQ = '0', moviesQ = '0', beverageQ = '0';
    
    cout << "~ Phil's Friendship Algorithm ! ~ " << endl;
    cout << "Calling potential friend.." << endl;
    cout << "Brrrrring....." << endl;
    cout << "Brrrrring....." << endl;
    cout << "Brrr...... Hello ?" << endl;
    
    
    // TRY FOOD
    cout << "Hey ! Do you want to get some food? (y/n): " << endl;
    foodQ = responder();
    
    if (foodQ == 'Y')
    {
        cout << "Yay! What do you prefer ?" << "(1-Seafood, 2-steak, or 3-vegetarian)";
        cin >> num;
        updateFriendScore(FriendScore, num);
        
        cout << "Cool let's grub out . Can you pick me up ? (1-Yes, 2-I lost my license, but my neighbor will take us, 3-Wow, you called me so you should pick ME up, but sure I'll come by soon )";
        cin >> num;
        updateFriendScore(FriendScore, num);
    }
    if (foodQ != 'Y')
    {
        //TRY SPORTS
        cout << "Ok no problem. Do you like sports? (y/n): ";
        sportsQ = responder();
    }
    
    if (sportsQ == 'Y')
    {
        cout << "Heck Ya ! Me too ! What do you want to play ?" << "(1-Basketball, 2-Baseball, 3-Football) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
        cout << "Awesome . I lost my ball though .. can you bring one ? (1-Yes, 2-I would but I don't have one either. I'll ask my neighbor, 3-No, you bring one) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
    }
    
    if (foodQ != 'Y' && sportsQ != 'Y')
    {
        //TRY MOVIES
        cout << "Alright. I'm tired anyways. How about a movie ?! (y/n) ";
        moviesQ = responder();
    }
    
    if (moviesQ == 'Y')
    {
        cout << "Awesome ! What type of movie do you want to see ?" << "(1-Action, 2-Suspense, 3-Romance) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
        cout << "Cool ! I'll bring the popcorn ! Can you bring the drinks ? (1-Yes, 2-Maybe, 3-No) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
        
    }
    
    if (foodQ != 'Y' && sportsQ != 'Y' && moviesQ != 'Y')
    {
        //TRY BEVERAGES
        cout << "Ya movie tickets are pretty expensive. How about we get something to drink ? Nothing expensive or tiring. What do you say ? (y/n) ";
        beverageQ = responder();
    }
    
    if (beverageQ == 'Y')
    {
        cout << " Cool lets do it. What kind of beverage do you want to get ? (1-Coffee, 2-Beer, 3-Tea) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
        cout << "Cool, I'll come by and pick you up. See ya in a bit ! ";
        cout << " O Wait !! Before I go, I wanted to tell you not to bring any money. I'll pay. Is that ok ?" << "(1-Yes, 2-Maybe, 3-No, I'm grown I can pay for myself) ";
        cin >> num;
        updateFriendScore(FriendScore, num);
    }
    
    if (foodQ != 'Y' && sportsQ != 'Y' && moviesQ != 'Y' && beverageQ != 'Y')
    {
        cout << "Darn. Alright well I guess I'll talk to you later. Can I call back another time? (1-Yes, 2-I'll call you, 3-No) " << endl;
        cin >> num;
        updateFriendScore(FriendScore, num);

    }
    
    
    return 0;
}

char responder()
{
    char reply = '0';
    cin >> reply;
    return toupper(reply);
}

void updateFriendScore(int &currentTotal, int x)
{
    int score = 0;
    
    if (x == 1)
        score = 10;
    if (x == 2)
        score = 5;
    if (x == 3)
        score = 1;
    
    currentTotal += score;
    cout << "*** Your score is " << currentTotal << " !! ***" << endl;
    
    return;
}
Last edited on
the only thing now is aesthetics.. when the program runs its very cramped and hard to read imo.. how do i make it so each question skips a line so it flows btter aesthetically
closed account (48T7M4Gy)
cout << endl;
o ya ok thanks. what do you think though ? i know all the && doesnt look good but i just wanted it to work. i will look at what you just posted so i can learn new ways
closed account (48T7M4Gy)
BTW I just ran your latest. Looks good. As it turns out there are lots of ways to do it as you can see. Glad to hear the flowchart helped.

(Psst planning is better than just jumping into the code which is always the big temptation for everybody just about.)
Last edited on
well it seems i've been learning from the best. im told to write my pseudo code first to lay it out which i ever do. the flow chart thing is better imo. can you explain to me what the reply function does [please ? i tried to research but i didnt get a clear picture. what is that responder reply function doing to my program.
closed account (48T7M4Gy)
can you explain to me what the reply function does

You'll see from my latest version above - I got carried away and it looks more complicated than it is - I changed the responder() name to getResponse() because it is more meaningful.

In fact if you use psedocode and meaningful names derived from it you'll find your code tracks the pseudocode closely and reads like a story instead of gibberish looking code.

But to answer your question, a function is like a food processor - put the ingredients in and the internal processor churns back the result. Some times, and in the case of the responder there are no ingredients just the answer comes back. By writing reply = responder() it avoids repeatedly doing the same thing. If you had a thousand replies to get then a function saves a lot of typing and mistakes. If you want to tweak it you only have to do it once not a thousand times.

The responder() just goes away, gets the answer and sends it back to main for the decision to be made. return toUpper(etc) is the statement to send the reply and program control back to main().
o ok that makes more sense. what is toUpper? is there a toLower? or is that just a name you gave it .
- One thing that i might want to do with my code is change the point system a little bit. 1 and 2 will be 10 & 5 points like it already is. but if i want to make 3 be -1 (negative 1) point so it would take points away from the "currentTotal" how would i go about that ?
closed account (48T7M4Gy)
what is toUpper? is there a toLower?

http://www.cplusplus.com/reference/cctype/toupper/?kw=toupper
Note case is important. tolower is up to you to check:)

but if i want to make 3 be -1 (negative 1) point so it would take points away from the "currentTotal" how would i go about that ?

Guess what - make score = -1 in updateScore() function when x == 3 instead
Last edited on
Pages: 1234