| |||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
c++ cin.getline I think is the problem
okay, I'm absolutely at my wit's end here. I had a funtioning program, changed a couple things, and now a seemingly unrelated part of the program isn't working. I've checked and rechecked and I just can't figure out why its wrong. I'll post the entire code here (its about 200 lines). I believe the problem is in the choice_fun funtion, specifically the cin.getline, but I don't know why it isn't working. Any help you can give me would be immensely appreciated.
Code:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
/****** GLOBALS GO HERE *******/
char name[20]; // this needs to be changed to avoid buffer overflows
enum race_type{human=1, elf, orc};
race_type race;
bool physical;
/****** GLOBALS END HERE ******/
class player{
public :
int health; //max 100
int strength; //1-10
int magic; //1-25
int hitchance; //90-100
player(int player_race) {
switch (player_race) {
case 1:{
health = 85;
strength = 6;
magic = 10;
hitchance = 95;
break;
};
case 2: {
health = 60;
strength = 4;
magic = 20;
hitchance = 95;
break;
};
case 3: {
health = 100;
strength = 8;
magic = 1;
hitchance = 95;
break;
};
} // ends switch
} // ends player constructor
}; // ends class player
class monster {
public:
int health;
int strength;
int hitchance;
monster();
monster(int h, int s, int hc);
};
monster::monster(int h, int s, int hc) {
health = h;
strength = s;
hitchance = hc;
};
monster::monster() {
health =10;
strength = 0;
hitchance = 0;
};
int choice_fun() {
char choice[10];
int final;
cout <<"Enter 1 for physical attack" << endl;
cout <<"Enter 2 for magical attack" << endl;
cout <<"If you enter anything else, I'll kill you." << endl;
cin.getline (choice, 10);
cout <<"you entered " << choice << endl;
final = atoi(choice);
return final;
};
class Ccombat{
public:
void combat(int playH, int playS, int playM, int playHC, int monH, int monS, int monHC);
};
void combat(int playH, int playS, int playM, int playHC, int monH, int monS, int monHC) {
int damage, i, turn = 0; // 0 = player 1= monster
cout << "your magic skill is " << playM << endl;
while (playH >0 && monH >0) {
if (monH >0 && playH >0){
switch(choice_fun()) {
case 1:
cout << name << " does a regular attack" << endl;
physical = true;
break;
case 2:
cout << name << " casts a spell!" << endl;
break;
default:
cout <<"Hey! I warned you!" << endl;
playH = 0;
} // end switch
} //end if >0
for (i=0; i<2; i++) {
if (turn == 0){
if (physical) {
srand(time(NULL));
damage = rand()%5;
srand(time(NULL));
damage = damage + (playS/5 * (rand()%4 + 1));
monH -= damage;
turn++;
}
else
{srand(time(NULL));
damage = rand()%5;
srand(time(NULL));
damage = damage + (playM/5 * (rand()%4 + 1));
monH -= damage;
turn++;
};
} // end turn ==0
else // turn ==1
{
srand(time(NULL));
damage = rand()%5;
playH -= damage;
turn--;
}
}; // end for i=0
cout <<"Your health is " << playH << " the monster's is " << monH << endl;
} // end while
if (monH <= 0)
cout <<"You won! yay!"<< endl;
else if (playH > 0)
cout <<"You ran away. Coward." << endl;
else
cout <<"You died. Yep thats all, you lost. Loser." << endl;
}; // end combat
int racecheck (int inputrace) {
switch (inputrace){
case 1:{
race = human;
cout << "You are a Human" << endl;
return 1;
}
case 2:{
race = elf;
cout << "You are an Elf" << endl;
return 1;
}
case 3: {
race = orc;
cout << "You are an Orc" << endl;
return 1;
}
default:
return 0;
};
};
int main() {
int race_temp;
cout <<"Welcome to the World of Tabloc" << endl;
cout <<"Enter your name" << endl;
cin.get (name, 20, '\n');
cout <<"You can be a Human, Elf, or Orc" << endl;
cout <<"Humans have average stats, Elves are better suited to magic, " << endl;
cout <<"and Orcs are the strongest." << endl;
do {
cout <<"Enter (1) for Human, (2) for Elf, or (3) for Orc."<< endl;
cin >> race_temp;
} while (racecheck(race_temp) == 0);
player one (race_temp);
monster grr;
grr.health = 100;
combat(one.health,one.strength,one.magic,one.hitch ance,grr.health,0,0);
cout <<"Press any key to continue"<< endl;
getch();
return 0;
}
|
|
#2
|
|||
|
|||
|
problem fixed
Change the cin.getline (choice, 10); into cin>>choice; and it will work.
|
|
#3
|
|||
|
|||
|
I thought when you use cin.getline and then try to output or input values the standard input device already has its value so you need to add a cin.ingnore () after the getline statement.
Am I wrong??? |
|
#4
|
|||
|
|||
|
Hi...it has been a year when u posted his problem.......i'm seeing it today...........might have got the solution also.......if not here it is........
cin.getline(choice,10) may not be taking the input as probably '\n' was there in the input buffer stream because of the previous input to the program. So this getline treats that as a terminating characrter and discards it from the input stream and stores NULL in choice Any further input will be accepted by cin.getline() function as '\n' no longer remains in the buffer as it is discarded by the getline function One more problematic area i find in your code is : cout <<"Enter your name" << endl; cin.get (name, 20, '\n'); This can lead to a deadlock situation when u try to call this function again.You will not be able to accept any input line as after entering the name when u press '\n', it remains in the input buffer and is not discarded by the next call of cin.get() function (unlike cin.getline()) so all the subsequent calls to cin.get() will fail This works here because u called cin>>race_temp which "eats" all the whitespces(which include '\n') from the input buffer and will prompt u to enter a number. Hope it solves ur problem. In case of any other prob. plz reply me at rajeshnagpal@tataelxsi.co.in. Regards, Rajesh |
|
#5
|
|||
|
|||
|
Actually u need to add a cin.ignore() before u can cin.getline() function and not after that.
ci.ignore() will grab a character off an input stream which may be possibily the delimiting character for the cin.getline() function and hence it skips the input to be taken from the user. |
|
#6
|
||||
|
||||
|
Quote:
Thank you for your answer, however, it is custom to not drag up old threads, even if you can answer the question(s) that were posed ![]()
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > c++ cin.getline I think is the problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|