How to use Do/While loops

Someone Please Help Me

Page 1 of 1

7 Replies - 1173 Views - Last Post: 11 August 2007 - 11:37 AM Rate Topic: -----

#1 ilandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-August 07

How to use Do/While loops

Posted 10 August 2007 - 10:15 PM

Basically what I want to do is to get a part of my program to loop back to the point of asking for the direction again if the user does not enter the correct letter to move on. I know that I should use the do while loop but I guess I am not really getting that to well. Anyways here is the code.

#include <iostream>
#include <fstream>
#include <process.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <iomanip>
#include <math.h>
#include <signal.h>
using namespace std;   //This is so that our program can use cout and endl.
char Player[20];
char Race;
char Class;
char Answer;
char North;
char South;
char East;
char West;
char Up;
char Down;
char Direction;
string Dir;
string RaceName;
string ClassName;
string Score;
long int PLevel;
long int PStrength;		 // PlayerStrength
long int PIntelligence;	 //PlayerIntelligence
long int PWisdom;		  //PlayerWisdom
long int PConstitution;	//PlayerConstitution
long int PDexterity;	   //PlayerDexterity
double PHitPoints;		   //PlayerHitPoints
long int PManaPoints;		  //PlayerManaPoints
long int PMovements;		  //PlayerMovementPoints
long int PExp;




int main()
{
	system("color 0B");
	cout<<"********************************************************************************"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<"**********  W E L C O M E   T O   T H E   H O M E   O F   T H E   **************"<<endl;
	cout<<"**********  O  V  E  R  L  O  R  D  S  !  !  !  !  !  !  !  !  !  **************"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<"********************************************************************************"<<endl;
	cout<<"**********************written*by*Kasie******************************************"<<endl;
	cout<<"********************************************************************************"<<endl;
	system("pause");
	system("cls");
	cout<<"Welcome to the home of the Overlords. I hope that you enjoy your stay with us."<<endl;
	cout<<"I suppose we should just get down to our first order of buisness."<<endl;
	cout<<"What is the name that you shall go by?"<<endl;
	cin>>Player;
	cout<<"Very well then. Now the next thing that I think we should do is pick which"<<endl;
	cout<<"race you are going to be. As that is a most important thing for anyone."<<endl;
	cout<<"Here are your choices: "<<endl;
	cout<<"*********************************************"<<endl;
	cout<<"*				(H)uman					*"<<endl;
	cout<<"*				(D)warf					*"<<endl;
	cout<<"*				(E)lf					  *"<<endl;
	cout<<"*				(O)gre					 *"<<endl;
	cout<<"*********************************************"<<endl;
	cin>>Race;  
	Race=toupper (Race);
	switch(Race){
				 case 'H':
					  RaceName="Human";
					  break;
				 case 'D':
					  RaceName="Dwarf";
					  break;
				 case 'E':
					  RaceName="Elf";
					  break;
				 case 'O':
					  RaceName="Ogre";
					  break;
				 default:
						 RaceName="Unknown";
						 }
	cout<<"Good now that you have picked which race you are going to be we now"<<endl;
	cout<<"need to go about picking out which class you are going to be."<<endl;
	cout<<"Your choices are as follows:"<<endl;
	cout<<"*********************************************"<<endl;
	cout<<"*		   (W)arrior					   *"<<endl;
	cout<<"*		   ®ogue						 *"<<endl;
	cout<<"*		   (M)age						  *"<<endl;
	cout<<"*		   ©leric						*"<<endl;
	cout<<"*********************************************"<<endl;
	cin>>Class;
	Class=toupper(Class);
	switch(Class){
				  case 'W':
					   ClassName="Warrior";
					   PStrength=8;
					   PIntelligence=8;
					   PWisdom=8;
					   PConstitution=8;
					   PDexterity=8;
					   PHitPoints=8;
					   PManaPoints=0;
					   PMovements=8;
					   PLevel = 0;
					   break;
				  case 'R':
					   ClassName="Rogue";
					   PStrength=8;
					   PIntelligence=8;
					   PWisdom=8;
					   PConstitution=8;
					   PDexterity=8;
					  PHitPoints=8;
					   PManaPoints=0;
					   PMovements=8;
					   PLevel = 0;
					   break;
				  case 'M':
					   ClassName="Mage";
					   PStrength=8;
					   PIntelligence=8;
					   PWisdom=8;
					   PConstitution=8;
					   PDexterity=8;
					  PHitPoints=8;
					   PManaPoints=20;
					   PMovements=8;
					   PLevel = 0;
					   break;
				  case 'C':
					   ClassName="Cleric";
					   PStrength=8;
					   PIntelligence=8;
					   PWisdom=8;
					   PConstitution=8;
					   PDexterity=8;
					  PHitPoints=8;
					   PManaPoints=20;
					   PMovements=8;
					   PLevel = 0;
					   break;
				  default:
						  ClassName="Unknown";
						  }
	system("cls");
cout<<Player<<" the "<<RaceName<<" "<<ClassName<<endl;
cout<<"Does this look right to you? Y/N"<<endl;
cin >> Answer;
if (Answer == 'N' || Answer == 'n')
{
		   return main();
		   }
if(Answer == 'Y' || Answer == 'y')
{
		  cout<<"Good I am glad we got that right."<<endl;								 
		  }
cout << "Would you like to go through the tutorial section of this game? Y/N"<<endl;
cin >> Answer;
if (Answer == 'N' || Answer == 'n')
{
		   cout << "I am sorry right now this part of the game is not implimented."<<endl;
		   return main();
		   }
if (Answer == 'Y' || Answer == 'y')
{
		   system("pause");
		   }
		   system("cls");
		   system("color 0F");
cout<<" You stand in the middle of a room that seems to be a library of some sorts   "<<endl;
cout<<" books line the walls from ceiling to floor and from one wall to another	  "<<endl;
cout<<" a woman who seems to be a librarian is sitting at one end of the library	 "<<endl;
cout<<" reading a book. She pauses what she is doing as you begin to take steps	  "<<endl;
cout<<" towards her.																 "<<endl;
cout<<" Well "<<Player<<" it took you long enough to get to this point.			  "<<endl;
cout<<" Apparently you are going to be one of my new students. Very Well.			"<<endl;
cout<<" The first thing that we are going to work on is your stats. Go ahead and	 "<<endl;
cout<<" <score> to see what your score is.										   "<<endl;
cout<<"\094\094\094"<<endl;
cin >> Score;
cout<<Player<<" the "<<RaceName<<" "<<ClassName<<endl;
cout<<"STR:	 "<<PStrength<<"	  HP:"<<PHitPoints<<"\n"<<"DEX:	 "<<PDexterity<<"	  MOVE:"<<PMovements<<"\n"<<"WIS:	 "<<PWisdom<<"	  MANA:"<<PManaPoints<<"\n"<<"INT:	 "<<PIntelligence<<"	  LEVEL: "<<PLevel<<"\n"<<"CON:	 "<<PConstitution<<"\n"<<endl;
cout<<" "<<endl;
cout<<"Everything as far as your stats is pretty much self explanitory."<<endl;
cout<<"Type <North> to continue."<<endl;
cin >> Direction;
if(Direction != 'N' || Direction != 'n')
{
			 cout<<"I believe that I told you to type North and nothing else right?"<<endl;
			 }
			 if (Direction == 'N' || Direction == 'n')
			 return Direction();
			 {
						   system("cls");
						   }

getch();
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to use Do/While loops

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1634
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to use Do/While loops

Posted 10 August 2007 - 11:18 PM

Here is a basic do/while loop looking for user to enter 'North'

cout<<"Type <North> to continue."<<endl;
cin >> Direction;

if(Direction != 'N' || Direction != 'n')
{
   do{
			 cout<<"I believe that I told you to type North and nothing else right?"<<endl;
			 }
				if (Direction == 'N' || Direction == 'n')
				return Direction();
			{
	  }
	  while((Direction !='N') && (Direction !='n'))
}



Hope this at least points you in the right direction :)

Happy Coding!
Was This Post Helpful? 0
  • +
  • -

#3 ilandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-August 07

Re: How to use Do/While loops

Posted 10 August 2007 - 11:30 PM

unfortunatly the problem i am facing is that Direction cannot be used as a function.
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: How to use Do/While loops

Posted 11 August 2007 - 02:45 AM

View Postilandra, on 10 Aug, 2007 - 11:30 PM, said:

unfortunatly the problem i am facing is that Direction cannot be used as a function.

Hi ilandra, PsychoCoder was giving you an example on how to use the loop, not a solution to your game.
And Direction is not used as function here, it is used as a variable.
Was This Post Helpful? 0
  • +
  • -

#5 ilandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-August 07

Re: How to use Do/While loops

Posted 11 August 2007 - 10:17 AM

View PostPennyBoki, on 11 Aug, 2007 - 02:45 AM, said:

View Postilandra, on 10 Aug, 2007 - 11:30 PM, said:

unfortunatly the problem i am facing is that Direction cannot be used as a function.

Hi ilandra, PsychoCoder was giving you an example on how to use the loop, not a solution to your game.
And Direction is not used as function here, it is used as a variable.

I guess what i am trying to get at is I am stuck and don't know how to fix my problem.
i have tried changing direction to:
string
int
char
void
all with the same problem.
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: How to use Do/While loops

Posted 11 August 2007 - 10:42 AM

Have you actually tried the code that was posted (I made one small change)?
cout<<"Type <North> to continue."<<endl;
cin >> Direction;

if(Direction != 'N' && Direction != 'n')
{
   do{
			 cout<<"I believe that I told you to type North and nothing else right?"<<endl;
			 }
				if (Direction == 'N' || Direction == 'n')
				return Direction;
			{
	  }
	  while((Direction !='N') && (Direction !='n'))
}


if so are you receiving an error? Undesired behaviour?
Was This Post Helpful? 0
  • +
  • -

#7 ilandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-August 07

Re: How to use Do/While loops

Posted 11 August 2007 - 11:26 AM

I have tried the code that both of you posted my errors look like this:

expected `while' before '(' token
expected `;' before "return"
expected primary-expression before '}' token
expected `;' before '}' token
`Direction' cannot be used as a function
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1634
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to use Do/While loops

Posted 11 August 2007 - 11:37 AM

Heres another shot at it :)

cout<<"Type <North> to continue."<<endl;
cin >> Direction;

if(Direction != 'N' && Direction != 'n')
{
   do{
			 cout<<"I believe that I told you to type North and nothing else right?"<<endl;
			 
			 if (Direction == 'N' || Direction == 'n')
						 {
				  return Direction;
			  }
	  }
	  while((Direction !='N') && (Direction !='n'))
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1