C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 19th, 2004, 10:11 PM
alpha-numberic alpha-numberic is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 2 alpha-numberic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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;
}

Reply With Quote
  #2  
Old June 21st, 2004, 05:54 AM
tenevie tenevie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 tenevie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
problem fixed

Change the cin.getline (choice, 10); into cin>>choice; and it will work.

Reply With Quote
  #3  
Old July 1st, 2004, 10:26 PM
Monkey Junk Monkey Junk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Monkey Junk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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???

Reply With Quote
  #4  
Old May 22nd, 2005, 04:28 AM
rajesh_krec rajesh_krec is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 2 rajesh_krec User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 52 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old May 22nd, 2005, 04:30 AM
rajesh_krec rajesh_krec is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 2 rajesh_krec User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 52 sec
Reputation Power: 0
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.

Reply With Quote
  #6  
Old May 23rd, 2005, 04:20 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
Quote:
Originally Posted by rajesh_krec
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.

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.



Reply With Quote
Closed Thread

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > c++ cin.getline I think is the problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT