My Project: Dinjy

Pages: 12
need help with my confusing code...not sure what i'm doing
Last edited on
Well, it order to find out what type the data is you would have to create a function to loop through your string and check whether each part is a #, letter, or ., then you can use a stringstream in order to change it to the variable.
unfortunately as good an idea that sounds, idk how to do that because i'm simply a beginner ._. would you know how i could code something like that?
You can use the stream member function peek() to get the next character of that stream and then use isdigit() and isalpha() functions
Simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream file("Myfile");
char next;
string sval;
double nval;
while(! file.eof())
{
    next = file.peek();
    if (isaplha(next))
       file >> sval;
    else if(isdigit(next) or next=='.')
       file >> nval;
    else
       //do something else
}
Last edited on
i just tried to implement that the best i could. but i get the errors:
error C3861: 'isaplha': identifier not found
error C2065: 'file' : undeclared identifier
error C2065: 'file' : undeclared identifier
you have to #include <fstream> and <ctipe.h>
it wont open ctipe.h because "there is no such directory" lol... @_@
Yes sorry (misstyped) it is ctype.h ...
lol..man i'm having the stupidest difficult time trying to implement that code and tweak it to my program.
i can't seem to get it to shut up about:
error C3861: 'isaplha': identifier not found
error C2065: 'file' : undeclared identifier
error C2065: 'file' : undeclared identifier
Hmm...well that's not exactly how I was going to say to do it...although it should work fine. For isaplha, it's isalpha() (you switched the l/p too XD).

For the "file" you need to replace that with whatever stream you are using to read from the file you opened. (i.e. is Bazzy's example he opened a file called "Myfile")
thanks firedraco for those typo notifications lol, that worked on account of the console finally running.

now though i can't seem to get it to display isaac.txt's integer numbers or anything to the screen after entering this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	char next;
	string sval;
	double nval;
	isaac.open("isaac.txt");
	isaac >> HP >> PP >> Att >> Def >> Agi >> Lck;
	while(!isaac.eof())
	{
		next = isaac.peek();
		if (isalpha(next))
		   isaac >> sval;
		else if(isdigit(next)|| next=='.')
		   isaac >> nval;
		else;
		   //do something else
	}
	cout << HP << endl;
	cout << PP << endl;
	cout << Att << endl;
	cout << Def << endl;
	cout << Agi << endl;
	cout << Lck << endl;


It's probably because dumping your stream of text into integers doesn't work...you will have to do some conversions using getline() and strings. Inside of while() you need to take the values (the nval etc) and put them into the correct variable, otherwise line 5 (as it is) is just emptying the text file, then your while loop does nothing.
Ok i have a hell of an annoying issue here now... i want to have my program display updated Stats up to 7 times. but it seems to reset back to default integers...basically i don't know how to program this, can someone give me some help here? i'll just show the function i need help with instead of all of my program.
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
void earth()
{
// Variables ********************************
	string line1, line2, line3, line4, line5, line6, line7;
	string choice;
	int HP;
	int PP;
	int Att;
	int Def;
	int Agi;
	int Lck;
	ofstream current;
	ofstream results;
	ifstream stats;
	ifstream flint;
	ifstream granite;
	ifstream quartz;
	ifstream vine;
	ifstream sap;
	ifstream ground;
	ifstream bane;

// Helpful output for the user to follow *********************
	cout << endl << endl;
	cout << "Welcome, Earth Adept!" << endl;
	cout << "Select Earth Djinni from below:" << endl << endl;
	cout << "________ HP  PP  Att  Def  Agi  Lck" << endl;
	cout << "Flint    8   4   3    --   --   --" << endl;
	cout << "Granite  9   --  --   2    2    1" << endl;
	cout << "Quartz   10  3   --   --   3    --" << endl;
	cout << "Vine     12  4   --   3    --   1" << endl;
	cout << "Sap      10  --  3    --   --   1" << endl;
	cout << "Ground   9   3   --   --   3    --" << endl;
	cout << "Bane     12  --  4    --   --   --" << endl;
	cout << "________" << endl << endl;

	stats.open("isaacstats.txt");
	stats >> HP >> PP >> Att >> Def >> Agi >> Lck;
	stats.close();
	current.open("current.txt");
	current << "-Isaac-" << endl;
	current << "HP:  " << HP << endl;
	current << "PP:  " << PP << endl;
	current << "Att: " << Att << endl;
	current << "Def: " << Def << endl;
	current << "Agi: " << Agi << endl;
	current << "Lck: " << Lck;
	cout << "-Current-" << endl;
	cout << "HP:  " << HP << endl;
	cout << "PP:  " << PP << endl;
	cout << "Att: " << Att << endl;
	cout << "Def: " << Def << endl;
	cout << "Agi: " << Agi << endl;
	cout << "Lck: " << Lck << endl;
	current.close();

// If statements for each Djinni to be called with
	cout << endl;
	cout << "Earth Djinni: ";
	cin >> choice;
	cout << endl;
	if (choice == "flint" || choice == "Flint" || choice == "FLINT"){
		flint.open("flint.txt");
		// read text
		getline(flint, line1);
		getline(flint, line2);
		getline(flint, line3);
		// write text
		cout << "You teamed up with Flint!" << endl << endl;
		cout << "-Stats Added-" << endl;
		cout << "HP:  " << line1 << endl;
		cout << "PP:  " << line2 << endl;
		cout << "Att: " << line3 << endl << endl;
		// write results to results.txt
		current.open("current.txt");
		flint >> HP >> PP >> Att >> Def >> Agi >> Lck;
		current << HP + 8 << endl;
		current << PP + 4 << endl;
		current << Att + 3 << endl;
		current << Def << endl;
		current << Agi << endl;
		current << Lck;
		// write results to console
		cout << "Update!" << endl << endl;
		cout << "-Isaac-" << endl;
		cout << "HP:  " << HP + 8 << endl;
		cout << "PP:  " << PP + 4 << endl;
		cout << "Att: " << Att + 3 << endl;
		cout << "Def: " << Def << endl;
		cout << "Agi: " << Agi << endl;
		cout << "Lck: " << Lck << endl << endl;
		//close flint.txt and results.txt
		flint.close();
		results.close();
	}

here is what that code gives me. you can see how "-Current-" results are never saving..idk how to do it ._.
http://i44.photobucket.com/albums/f20/shadowclone666/DinjyCONSOLE.png
Well...you never actually increment your HP, PP, etc...
increment them? not sure what that means....but i've been expecting this problem to be very easy to pinpoint. its just not obvious to me because i'm exhausted and can't think straight to concentrate tonight
1
2
cout << HP + 8; // this returns the values of HP+8 but HP doesn't change
cout << (HP+=8);//this sets HP to HP+8 and returns that value 

The most common way to use that would be this:
1
2
HP += 8;//set HP to HP+8
cout << HP;//print HP 


(Edit) HP+=8; is the same as HP=HP+8;
Last edited on
Ok let me try to sum this up, i want my program to:
-User picks element.
-Console displays 7 names from that element's list.
-User picks up to 7 names from that list (or a combo of 7 names from either 4 elements' lists)
-console displays updated current stats equal to what the 7 chosen names add to default stats.

My PROBLEM:
-IDK how to update the display of current stats correctly.
-IDK how to limit user to a total of 7 picks from a combo of all 4 element lists or of one list
-IDK how to remove a name from the list when already picked so user can't pick it again.

Code for MAIN and EARTH functions only (kinda not necessary to add all elements, they do same thing in the end anyway.):
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
void earth()
{
// Variables ********************************
	string line1, line2, line3, line4, line5, line6, line7;
	string choice;
	int HP;
	int PP;
	int Att;
	int Def;
	int Agi;
	int Lck;
	ofstream current;
	ofstream results;
	ifstream stats;
	ifstream flint;
	ifstream granite;
	ifstream quartz;
	ifstream vine;
	ifstream sap;
	ifstream ground;
	ifstream bane;

// Helpful output for the user to follow *********************
	cout << endl << endl;
	cout << "Welcome, Earth Adept!" << endl;
	cout << "Select Earth Djinni from below:" << endl << endl;
	cout << "________ HP  PP  Att  Def  Agi  Lck" << endl;
	cout << "Flint    8   4   3    --   --   --" << endl;
	cout << "Granite  9   --  --   2    2    1" << endl;
	cout << "Quartz   10  3   --   --   3    --" << endl;
	cout << "Vine     12  4   --   3    --   1" << endl;
	cout << "Sap      10  --  3    --   --   1" << endl;
	cout << "Ground   9   3   --   --   3    --" << endl;
	cout << "Bane     12  --  4    --   --   --" << endl;
	cout << "________" << endl << endl;

	stats.open("isaacstats.txt");
	stats >> HP >> PP >> Att >> Def >> Agi >> Lck;
	stats.close();
	current.open("current.txt");
	current << "-Isaac-" << endl;
	current << "HP:  " << HP << endl;
	current << "PP:  " << PP << endl;
	current << "Att: " << Att << endl;
	current << "Def: " << Def << endl;
	current << "Agi: " << Agi << endl;
	current << "Lck: " << Lck;
	cout << "-Current-" << endl;
	cout << "HP:  " << HP << endl;
	cout << "PP:  " << PP << endl;
	cout << "Att: " << Att << endl;
	cout << "Def: " << Def << endl;
	cout << "Agi: " << Agi << endl;
	cout << "Lck: " << Lck << endl;
	current.close();

// If statements for each Djinni to be called with
	cout << endl;
	cout << "Earth Djinni: ";
	cin >> choice;
	cout << endl;
	if (choice == "flint" || choice == "Flint" || choice == "FLINT"){
		flint.open("flint.txt");
		// read text
		getline(flint, line1);
		getline(flint, line2);
		getline(flint, line3);
		// write text
		cout << "You teamed up with Flint!" << endl << endl;
		cout << "-Stats Added-" << endl;
		cout << "HP:  " << line1 << endl;
		cout << "PP:  " << line2 << endl;
		cout << "Att: " << line3 << endl << endl;
		// write results to results.txt
		current.open("current.txt");
		current << HP + 8 << endl;
		current << PP + 4 << endl;
		current << Att + 3 << endl;
		current << Def << endl;
		current << Agi << endl;
		current << Lck;
		// write results to console
		cout << "Update!" << endl << endl;
		cout << "-Isaac-" << endl;
		cout << "HP:  " << HP + 8 << endl;
		cout << "PP:  " << PP + 4 << endl;
		cout << "Att: " << Att + 3 << endl;
		cout << "Def: " << Def << endl;
		cout << "Agi: " << Agi << endl;
		cout << "Lck: " << Lck << endl << endl;
		//close flint.txt and results.txt
		flint.close();
		results.close();
	}
	else if (choice == "granite" || choice == "Granite" || choice == "GRANITE"){
		granite.open("granite.txt");
		// read text
		getline(granite, line1);
		getline(granite, line2);
		getline(granite, line3);
		getline(granite, line4);
		// write text
		cout << "You teamed up with Granite!" << endl << endl;
		cout << "-Stats Added-" << endl;
		cout << "HP:  " << line1 << endl;
		cout << "Def: " << line2 << endl;
		cout << "Agi: " << line3 << endl;
		cout << "Lck: " << line4 << endl << endl;
		// write results to results.txt
		results.open("results.txt");
		results << "-Isaac-" << endl;
		results << "HP:  " << HP + 9 << endl;
		results << "PP:  " << PP << endl;
		results << "Att: " << Att << endl;
		results << "Def: " << Def + 2 << endl;
		results << "Agi: " << Agi + 2 << endl;
		results << "Lck: " << Lck + 1;
		// write results to console
		cout << "Update!" << endl << endl;
		cout << "-Isaac-" << endl;
		cout << "HP:  " << HP + 9 << endl;
		cout << "PP:  " << PP << endl;
		cout << "Att: " << Att << endl;
		cout << "Def: " << Def + 2 << endl;
		cout << "Agi: " << Agi + 2 << endl;
		cout << "Lck: " << Lck + 1 << endl << endl;
		//close granite.txt and results.txt
		granite.close();
		results.close();
	}
	else if (choice == "quartz" || choice == "Quartz" || choice == "QUARTZ"){
		quartz.open("quartz.txt");
		// read text
		getline(quartz, line1);
		getline(quartz, line2);
		getline(quartz, line3);
		// write text
		cout << "You teamed up with Quartz!" << endl << endl;
		cout << "-Stats Added-" << endl;
		cout << "HP:  " << line1 << endl;
		cout << "PP:  " << line2 << endl;
		cout << "Agi: " << line3 << endl << endl;
		// write results to results.txt
		results.open("results.txt");
		quartz >> HP >> PP >> Att >> Def >> Agi >> Lck;
		results << "-Isaac-" << endl;
		results << "HP:  " << HP + 10 << endl;
		results << "PP:  " << PP + 3 << endl;
		results << "Att: " << Att << endl;
		results << "Def: " << Def << endl;
		results << "Agi: " << Agi + 3 << endl;
		results << "Lck: " << Lck;
		// write results to console
		cout << "Update!" << endl << endl;
		cout << "-Isaac-" << endl;
		cout << "HP:  " << HP + 10 << endl;
		cout << "PP:  " << PP + 3 << endl;
		cout << "Att: " << Att << endl;
		cout << "Def: " << Def << endl;
		cout << "Agi: " << Agi + 3 << endl;
		cout << "Lck: " << Lck << endl << endl;
		//close quartz.txt and results.txt
		quartz.close();
		results.close();
	}
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
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
132
133
134
135
136
137
138
139
140
141
	else if (choice == "vine" || choice == "Vine" || choice == "VINE"){
		vine.open("vine.txt");
		// read text
		getline(vine, line1);
		getline(vine, line2);
		getline(vine, line3);
		getline(vine, line4);
		// write text
		cout << "You teamed up with Vine!" << endl << endl;
		cout << "-Stats Added-" << endl;
		cout << "HP:  " << line1 << endl;
		cout << "PP:  " << line2 << endl;
		cout << "Def: " << line3 << endl;
		cout << "Lck: " << line4 << endl << endl;
		// write results to results.txt
		results.open("results.txt");
		vine >> HP >> PP >> Att >> Def >> Agi >> Lck;
		results << "-Isaac-" << endl;
		results << "HP:  " << HP + 12 << endl;
		results << "PP:  " << PP + 4 << endl;
		results << "Att: " << Att << endl;
		results << "Def: " << Def + 3 << endl;
		results << "Agi: " << Agi << endl;
		results << "Lck: " << Lck + 1;
		// write results to console
		cout << "Update!" << endl << endl;
		cout << "-Isaac-" << endl;
		cout << "HP:  " << HP + 12 << endl;
		cout << "PP:  " << PP + 4 << endl;
		cout << "Att: " << Att << endl;
		cout << "Def: " << Def + 3 << endl;
		cout << "Agi: " << Agi << endl;
		cout << "Lck: " << Lck + 1 << endl << endl;
		//close vine.txt and results.txt
		vine.close();
		results.close();
	}
	else if (choice == "sap" || choice == "Sap" || choice == "SAP"){
		sap.open("sap.txt");
		// read text
		getline(sap, line1);
		getline(sap, line2);
		getline(sap, line3);
		// write text
		cout << "You teamed up with Sap!" << endl << endl;
		cout << "-Stats Added-" << endl;
		cout << "HP:  " << line1 << endl;
		cout << "Att: " << line2 << endl;
		cout << "Lck: " << line3 << endl << endl;
		// write results to results.txt
		results.open("results.txt");
		sap >> HP >> PP >> Att >> Def >> Agi >> Lck;
		results << "-Isaac-" << endl;
		results << "HP:  " << HP + 10 << endl;
		results << "PP:  " << PP << endl;
		results << "Att: " << Att + 3 << endl;
		results << "Def: " << Def << endl;
		results << "Agi: " << Agi << endl;
		results << "Lck: " << Lck + 1;
		// write results to console
		cout << "Update!" << endl << endl;
		cout << "-Isaac-" << endl;
		cout << "HP:  " << HP + 10 << endl;
		cout << "PP:  " << PP << endl;
		cout << "Att: " << Att + 3 << endl;
		cout << "Def: " << Def << endl;
		cout << "Agi: " << Agi << endl;
		cout << "Lck: " << Lck + 1 << endl << endl;
		//close sap.txt and results.txt
		sap.close();
		results.close();
	}
	else if (choice == "ground" || choice == "Ground" || choice == "GROUND"){
		ground.open("ground.txt");
		// read text
		getline(ground, line1);
		getline(ground, line2);
		getline(ground, line3);
		// write text
		cout << "You teamed up with Ground!" << endl << endl;
		cout << "-Stats Added-" << endl;
		cout << "HP:  " << line1 << endl;
		cout << "PP:  " << line2 << endl;
		cout << "Agi: " << line3 << endl << endl;
		// write results to results.txt
		results.open("results.txt");
		ground >> HP >> PP >> Att >> Def >> Agi >> Lck;
		results << "-Isaac-" << endl;
		results << "HP:  " << HP + 9 << endl;
		results << "PP:  " << PP + 3 << endl;
		results << "Att: " << Att << endl;
		results << "Def: " << Def << endl;
		results << "Agi: " << Agi + 3 << endl;
		results << "Lck: " << Lck;
		// write results to console
		cout << "Update!" << endl << endl;
		cout << "-Isaac-" << endl;
		cout << "HP:  " << HP + 10 << endl;
		cout << "PP:  " << PP << endl;
		cout << "Att: " << Att + 3 << endl;
		cout << "Def: " << Def << endl;
		cout << "Agi: " << Agi << endl;
		cout << "Lck: " << Lck + 1 << endl << endl;
		//close ground.txt and results.txt
		ground.close();
		results.close();
	}
	else if (choice == "bane" || choice == "Bane" || choice == "BANE"){
		bane.open("bane.txt");
		// read text
		getline(bane, line1);
		getline(bane, line2);
		// write text
		cout << "You teamed up with Bane!" << endl << endl;
		cout << "-Stats Added-" << endl;
		cout << "HP:  " << line1 << endl;
		cout << "Att: " << line2 << endl << endl;
		// write results to results.txt
		results.open("results.txt");
		bane >> HP >> PP >> Att >> Def >> Agi >> Lck;
		results << "-Isaac-" << endl;
		results << "HP:  " << HP + 12 << endl;
		results << "PP:  " << PP << endl;
		results << "Att: " << Att + 4 << endl;
		results << "Def: " << Def << endl;
		results << "Agi: " << Agi << endl;
		results << "Lck: " << Lck;
		// write results to console
		cout << "Update!" << endl << endl;
		cout << "-Isaac-" << endl;
		cout << "HP:  " << HP + 12 << endl;
		cout << "PP:  " << PP << endl;
		cout << "Att: " << Att + 4 << endl;
		cout << "Def: " << Def << endl;
		cout << "Agi: " << Agi << endl;
		cout << "Lck: " << Lck << endl << endl;
		//close bane.txt and results.txt
		bane.close();
		results.close();
	} // end if
} // end function earth 
I think your function repeats too much stuff...try rewriting it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void earth() {
//declare variables
//show stats etc...
//get type
if(1) {
//display stats change
//change stats
//close file
} else if(2) {
//display stats change
//change stats
//close file
} //...
//cout out updated stats
//close results
}
i appreciate that kind of help. but..i am not so sure i understand it ._. could you give me an example by using some of my code perhaps? i seem to get your attention late at night when i'm out of it lol ._.

and i also think my function repeats too much as well, any constructive criticism helps too. i'm just not sure how to code what you're advising.
Pages: 12