Lighter program

I have done this program but i do not think that it is the best way to do it. I have the idea that if i do it with the ASCII code it would be more effective (if it is possible to dit it like this ) but maybe there is a kind of function or whatever that permits me to do the program lighter. ( Do not request a solution if there is one, just hints, thanks )

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

//Similar to rock-paper-scissors game
//but with A, P and V. 
//This is a though program...
//a way to be more effective?

int main() {
	string x, y, A, P, V;  
	cin >> x >> y;
	if( x == y ) cout << "-" << endl; 
	if( x == "A" and y == "P" ) cout << "1" << endl;
	if( x == "A" and y == "V" ) cout << "2" << endl;
	if( x == "P" and y == "V" ) cout << "1"<< endl;
	if( x == "P" and y == "A" ) cout << "2"<< endl;
	if( x == "V" and y == "A" )	cout << "1" << endl;
	if( x == "V" and y == "P" )	cout << "2"<< endl;		
}
If your "strings" only have a single character think of using a char instead of the string.

Also you have several unused variables A, P, and V.

You may also want to see about simplifying your logic. You can combine all the if() statements that print "1" (consider just printing the number instead of a string) into one statement, the same with the if() statements printing "2".

Ouch yes what a mistake wow. At first i did put char and variables A P V but did not work so then i put string and the " " ones so i forbid the A P V variables.

I have been doing some changes and different tries but nothing... If i put char the program prints nothing, with " " and without them. But if i work with string and " " it prints it ok. When i work with char i must use ascii or else the program does not work? Do not know what i am doing wrong when using char because if thing i have worked with char before so.

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>
using namespace std;

//Similar to rock-paper-scissors game
//but with A, P and V. 
//This is a though program...
//a way to be more effective?

int main() {
	
	char x, y, A, P, V;  
	cin >> x >> y;

	if( x == y ) cout << "-" << endl; 

	if( ( x == A and y == P ) or 
		( x == P and y == V ) or 
		( x == V and y == A ) ) {
		cout << "1" << endl;
	}
	
	if( ( x == A and y == V ) or
		( x == P and y == A ) or 
		( x == V and y == P ) ) {
		cout << "2" << endl;
	}	
}

Last edited on
Why are you using the variables A, P, and V uninitialized?
Do you realize that a variable name is different from a string constant "A" or a character constant 'A'? And that "1" is a string constant not the number 1?
I missed the fact that chars go with ' '. And did not know why i put "1", maybe because i had doubts about it. Thanks! Final product(works)

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
#include <iostream>
using namespace std;

//Similar to rock-paper-scissors game
//but with A, P and V. 

int main() {
	
	char x, y;  
	cin >> x >> y;

	if( x == y ) cout << "-" << endl; 

	if( ( x == 'A' and y == 'P' ) or 
		( x == 'P' and y == 'V' ) or 
		( x == 'V' and y == 'A' ) ) {
		cout << 1 << endl;
	}
	
	if( ( x == 'A' and y == 'V' ) or
		( x == 'P' and y == 'A' ) or 
		( x == 'V' and y == 'P' ) ) {
		cout << 2 << endl;
	}	
}
There is also the switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ( x == y ) {
} else {
 switch ( x ) {
  case 'A':
    if ( y=='P' ) {} else if ( y=='V' ) {} else { /* y is none of the three */ }
    break;
  case 'P':
    // likewise
    break;
  case 'V':
    // likewise
    break;
  default:
    // x is none of the three
 }
}


and mapping of some sort:
1
2
3
4
5
6
7
8
auto lhs = (x=='A') ? 0 : (x=='P') ? 1 : (x=='V') ? 2 : -1;
auto rhs = (x=='A') ? 2 : (x=='P') ? 0 : (x=='V') ? 1 : -1;
if ( lhs < 0 or rhs < 0 ) return 1;

char result = '-';
if ( x != y ) {
  result = ( lhs == rhs ) ? '1' : '2';
}
Whew, did not know about that switch thing, neither auto... I have and idea how that works but i will search it, thanks!
Topic archived. No new replies allowed.