How should I optimize this?

I managed to build a multiple choice template with enum. How do I optimize this, to take on bigger IO problems, and make perhaps an RPG Game?

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

//in main

#include "stdafx.h"
#include <iostream>
#include <string>
#include"RaezersIO.h"


int main()
{ 
	while (true)
	{
		IO question;
		question.CaseA = "Hello";
		question.CaseB = "bonjour";
		question.CaseC = "Hola";
		question.CaseD = "goodbye"; 
		question.CaseE = "I love you"; 
		question.SetOutput("A,B,C,D,or E");
		question.Takeinput();
		question.Caseprint();
		
		main(); 

		return 0;

	}
}
//in the header file.
class IO
{ public:

	string Takeinput()
	{
		cin>>userinput; 
		
		return Stringreturn;
	}
	enum choice {
		A = 0,
		B = 1,
		C = 2,
		D = 3,
		E = 4
	};
	 
	string SetOutput(string Output) {
		cout << Output;
		return Stringreturn; 
	
	};

	string Caseprint() {
		
		if (userinput == "A"||userinput=="a" )
		{
			choice Cases = A; 
			if( Cases==A )
			{
				SetOutput(CaseA);
			}
			
		}
		else if (userinput == "B"||userinput=="b")
		{
			choice Cases = B;
		 if (Cases == B)
			{
				SetOutput(CaseB);
			}
		 
		}
		else if (userinput == "C"||userinput=="c")
		{
			choice Cases = C;
			 if (Cases == C)
			{
				SetOutput(CaseC);
			}
			  
		}
		else if (userinput == "D"||userinput=="d")
		{
			choice Cases = D;
			if (Cases == D)
			{
				SetOutput(CaseD);
			}
			
		}
		else if (userinput == "E"||userinput=="e")
		{
			choice Cases = E;
			if (Cases == E)
			{
				SetOutput(CaseE);
			}
			
		}
		else SetOutput("error, not a choice");

		return Stringreturn; 
	}
public:

	const string Stringreturn="  "; 
	string CaseA;
	string CaseB; 
	string CaseC; 
	string CaseD; 
	string CaseE; 

	string userinput; 
	
private:
	string Output; 

	
	
	
	    
	 

};
  
Last edited on
right away I see a need for a constructor.
eg
IO question ("A words", "B words" … … "A,B,C,D, or E", … etc);

I prefer
if (toupper(userinput) == "D")

the enum seems redundant. Make userinput a single char. OR if you want strings allow words input, string is ok, but if you want single char inputs, use a single char.
use that in your conditions (might be cleaner looking to use a switch statement)

it only allows A-E inputs. No numbers, etc. you can make it generic via a string class member and a list of what is allowed.
eg question.allowed = "1234";
and then if(allowed.find(userinput) … etc
then its a valid input
and even better the position from find tells you which action to do, so if they put in 3, do the 3rd action in this case … but nothing is stopping you from "NESW" to represent compass directions (do the 3rd action on 'S' which is still indicated from the find), or something else cool!

you need a way to define actions for what to do when it gets a choice. I am thinking virtual functions, but maybe someone else can help here; that is not an area I am expert in.


Last edited on
Topic archived. No new replies allowed.