Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

at wits end with getline...

Status
Not open for further replies.

BungoMan

Programmer
May 13, 2002
12
0
0
US
grr! i dont understand why its doing this, but for someone it refuses to let the getline statement to work, everytime i compile it says i have the wrong number of arguments, at first it said i had 3 and it needs 2, then i took out the delimeter argument (which according to the msdn thing is supposed to be there) and then i compiled and it said i had 2 and needed 3... wtf?.... i know for a fact i have all the stuff defined correctly cause i even took the example in the msdn thing and it still didnt work correctly... here is is my program and the errors it is giving me, its a lil program thats supposed to be a text based game, nothing complexe, all i need is the stupid thing to get a whole line of input for names and such...


//program:
#include "stdafx.h"
#include "functions.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <fstream.h>
#include <string>
using std::string;
using std::getline;

struct Character
{
public:
string NAME;
int STR, DEX, INT, HITS, STAMINA, MANA;
int LEVEL;
int CURRENTEXP, EXPTOLEVEL;
int WEAPONPOWER, ARMORRATING, MAGICPOWER, MAGICDEFENSE, EVADE, MAGICEVADE;
int EXPGAINED;
string WEAPON, SHIELD, BODYARMOR, LEGARMOR, ARMARMOR, HANDARMOR, NECKARMOR, HEADARMOR;
void save();
void generatestats();
void selectmonster();
void viewstats();
Character::Character();
};

void menu();
void battle();
void equipment();
void buy();
void sell();
void quitwithsave();
void quitwithoutsave();

Character PLAYER;
Character MONSTER;

void main()
{
char bah;
bah=7;
cout << bah << bah << bah; //beep three times
cout << &quot;Battle Arena version .02&quot; << endl;
Skip(2);
int neworload;
cout << &quot;To start a new game press 1 or to load a game press 2.&quot; << endl;
cin >> neworload;
//ClearBuffer(); // commented out cause it doesnt work, its something we do at school whenever we have to cin a single int or char, so it skips to the next line... its in the functions.h file
Skip(2);
do
{
if(neworload==1)
{
string charname;
getline(cin,charname);
getline(cin,charname, '\n');
PLAYER.NAME=charname;
PLAYER.generatestats();
menu();
}
else
{
cout << &quot;This option is not available yet..&quot; << endl;
main();
}
}
while( neworload!=1 || neworload!=2 );
}

Character::Character()
{
NAME=&quot;unnamed&quot;;
STR=0;
DEX=0;
INT=0;
HITS=0;
STAMINA=0;
MANA=0;
LEVEL=1;
}

void Character::generatestats()
{
STR=rand()%25;
DEX=rand()%25;
INT=rand()%25;
while(STR<10)
STR=rand()%25;
while(DEX<10)
DEX=rand()%25;
while(INT<10)
INT=rand()%25;
CURRENTEXP=0;
EXPTOLEVEL=25;
}

void menu()
{
cout << &quot;Select what you wish to do.&quot; << endl;
cout << &quot;1. Battle a monster.&quot; << endl;
cout << &quot;2. Change equipment.&quot; << endl;
cout << &quot;3. Buy equipment.&quot; << endl;
cout << &quot;4. Sell equipment.&quot; << endl;
cout << &quot;5. View your stats.&quot; << endl;
cout << &quot;6. Save your data.&quot; << endl;
cout << &quot;7. Save and quit.&quot; << endl;
cout << &quot;8. Quit without saving.&quot; << endl;
int menuselection;
cin >> menuselection;
//ClearBuffer();
switch(menuselection)
{
case 1 : battle();
break;
case 2 : equipment();
break;
case 3 : buy();
break;
case 4 : sell();
break;
case 5 : PLAYER.viewstats();
break;
case 6 : PLAYER.save();
break;
case 7 : quitwithsave();
break;
case 8 : quitwithoutsave();
break;
default : cout << &quot;That is not a valid choice.&quot; << endl;
Skip(2);
menu();
}
}

void battle()
{
MONSTER.selectmonster();


}

void equipment()
{
}

void buy()
{
}

void sell()
{
}

void Character::viewstats()
{
cout << PLAYER.NAME.c_str() << &quot;s Current Stats:&quot; << endl;
cout << &quot;Level: &quot; << PLAYER.LEVEL << endl;
cout << &quot;Strength: &quot; << PLAYER.STR << endl;
cout << &quot;Intelligence: &quot; << PLAYER.INT << endl;
cout << &quot;Dexterity: &quot; << PLAYER.DEX << endl;
Skip(2);
cout << &quot;Weapon Power: &quot; << PLAYER.WEAPONPOWER << endl;
cout << &quot;Armor Rating: &quot; << PLAYER.ARMORRATING << endl;
cout << &quot;Magic Attack: &quot; << PLAYER.MAGICPOWER << endl;
cout << &quot;Magic Defense: &quot; << PLAYER.MAGICDEFENSE << endl;
cout << &quot;Evade: &quot; << PLAYER.EVADE << endl;
cout << &quot;Magic Evade: &quot; << PLAYER.MAGICEVADE << endl;
Skip(2);
cout << &quot;Total Experience: &quot; << PLAYER.CURRENTEXP << endl;
cout << &quot;Experience needed to level up: &quot; << PLAYER.EXPTOLEVEL << endl;
Skip(2);
cout << &quot;Current Equipment:&quot; << endl;
cout << PLAYER.WEAPON.c_str() << endl;
cout << PLAYER.SHIELD.c_str() << endl;
cout << PLAYER.BODYARMOR.c_str() << endl;
cout << PLAYER.LEGARMOR.c_str() << endl;
cout << PLAYER.ARMARMOR.c_str() << endl;
cout << PLAYER.HANDARMOR.c_str() << endl;
cout << PLAYER.NECKARMOR.c_str() << endl;
cout << PLAYER.HEADARMOR.c_str() << endl;
Skip(2);
cout << &quot;To return to the main menu press 1 then enter.&quot; << endl;
int returntomenu;
cin >> returntomenu;
menu();
}

void Character::save()
{
//string filename;
//cout << &quot;Enter the name of the file you data in.&quot; << endl;
ofstream OutFile;
//OutFile.open(filename.c_str());
OutFile.open(&quot;savegame.sav&quot;);
OutFile << PLAYER.NAME.c_str() << endl;
OutFile << PLAYER.LEVEL << endl;
OutFile << PLAYER.STR << endl;
OutFile << PLAYER.INT << endl;
OutFile << PLAYER.DEX << endl;
OutFile << PLAYER.WEAPONPOWER << endl;
OutFile << PLAYER.ARMORRATING << endl;
OutFile << PLAYER.MAGICPOWER << endl;
OutFile << PLAYER.MAGICDEFENSE << endl;
OutFile << PLAYER.EVADE << endl;
OutFile << PLAYER.MAGICEVADE << endl;
OutFile << PLAYER.CURRENTEXP << endl;
OutFile << PLAYER.EXPTOLEVEL << endl;
OutFile << PLAYER.WEAPON.c_str() << endl;
OutFile << PLAYER.SHIELD.c_str() << endl;
OutFile << PLAYER.BODYARMOR.c_str() << endl;
OutFile << PLAYER.LEGARMOR.c_str() << endl;
OutFile << PLAYER.ARMARMOR.c_str() << endl;
OutFile << PLAYER.HANDARMOR.c_str() << endl;
OutFile << PLAYER.NECKARMOR.c_str() << endl;
OutFile << PLAYER.HEADARMOR.c_str() << endl;
OutFile.close();
}

void quitwithsave()
{
}

void quitwithoutsave()
{
}

void Character::selectmonster()
{
int select;
if(PLAYER.LEVEL<5)
select=rand()%4;
switch(select)
{
case 0 : MONSTER.NAME=&quot;Imp&quot;;
MONSTER.STR=5;
MONSTER.HITS=5;
MONSTER.DEX=4;
MONSTER.STAMINA=4;
MONSTER.INT=1;
MONSTER.MANA=1;
MONSTER.LEVEL=1;
MONSTER.EXPGAINED=4;
MONSTER.WEAPONPOWER=2;
MONSTER.ARMORRATING=3;
MONSTER.MAGICPOWER=0;
MONSTER.MAGICDEFENSE=1;
MONSTER.EVADE=2;
MONSTER.MAGICEVADE=1;
break;
case 1 : MONSTER.NAME=&quot;Slime&quot;;
MONSTER.STR=2;
MONSTER.HITS=2;
MONSTER.DEX=3;
MONSTER.STAMINA=3;
MONSTER.INT=1;
MONSTER.MANA=1;
MONSTER.LEVEL=1;
MONSTER.EXPGAINED=1;
MONSTER.WEAPONPOWER=1;
MONSTER.ARMORRATING=2;
MONSTER.MAGICPOWER=0;
MONSTER.MAGICDEFENSE=0;
MONSTER.EVADE=20;
MONSTER.MAGICEVADE=0;
break;
case 2 : MONSTER.NAME=&quot;Headless&quot;;
MONSTER.STR=4;
MONSTER.HITS=4;
MONSTER.DEX=3;
MONSTER.STAMINA=3;
MONSTER.INT=1;
MONSTER.MANA=1;
MONSTER.LEVEL=1;
MONSTER.EXPGAINED=5;
MONSTER.WEAPONPOWER=6;
MONSTER.ARMORRATING=4;
MONSTER.MAGICPOWER=0;
MONSTER.MAGICDEFENSE=1;
MONSTER.EVADE=5;
MONSTER.MAGICEVADE=1;
break;
case 3 : MONSTER.NAME=&quot;Bandit&quot;;
MONSTER.STR=6;
MONSTER.HITS=6;
MONSTER.DEX=8;
MONSTER.STAMINA=8;
MONSTER.INT=3;
MONSTER.MANA=3;
MONSTER.LEVEL=2;
MONSTER.EXPGAINED=6;
MONSTER.WEAPONPOWER=2;
MONSTER.ARMORRATING=3;
MONSTER.MAGICPOWER=0;
MONSTER.MAGICDEFENSE=1;
MONSTER.EVADE=2;
MONSTER.MAGICEVADE=1;
break;
case 4 : MONSTER.NAME=&quot;Giant Toad&quot;;
MONSTER.STR=7;
MONSTER.HITS=7;
MONSTER.DEX=3;
MONSTER.STAMINA=3;
MONSTER.INT=1;
MONSTER.MANA=1;
MONSTER.LEVEL=1;
MONSTER.EXPGAINED=7;
MONSTER.WEAPONPOWER=4;
MONSTER.ARMORRATING=5;
MONSTER.MAGICPOWER=0;
MONSTER.MAGICDEFENSE=0;
MONSTER.EVADE=1;
MONSTER.MAGICEVADE=0;
break;
}
}

//errors:
--------------------Configuration: Battle Arena - Win32 Debug--------------------
Compiling...
Battle Arena.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Battle Arena\Battle Arena.cpp(56) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : expects 2 argume
nts - 3 provided
c:\program files\microsoft visual studio\vc98\include\string(145) : see declaration of 'getline'
C:\Program Files\Microsoft Visual Studio\MyProjects\Battle Arena\Battle Arena.cpp(56) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : could n
ot deduce template argument for 'class std::basic_istream<_E,_Tr> &' from 'class istream_withassign'
C:\Program Files\Microsoft Visual Studio\MyProjects\Battle Arena\Battle Arena.cpp(56) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : could n
ot deduce template argument for 'class std::basic_istream<_E,_Tr> &' from 'class istream_withassign'
C:\Program Files\Microsoft Visual Studio\MyProjects\Battle Arena\Battle Arena.cpp(56) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : could n
ot deduce template argument for 'class std::basic_istream<_E,_Tr> &' from 'class istream_withassign'
Error executing cl.exe.

Battle Arena.exe - 4 error(s), 0 warning(s)
 
Try this:

Code:
#include <fstream.h>

istream datasource;

if(datasource.peek()!='\n')
   datasource.ignore(100,'\n');
datasource.getline(x,'\n');

// x is the maximum number of characters to extract
// from the stream
This code will first look at the next character in the stream. If the character is not the delimiter (you can change the \n to be any single character), then it ignores everything up until the character after the next \n. It then proceeds to remove characters from the stream until either x characters have been removed or it's reached another \n, whichever comes first. You shouldn't need the if statement if you're doing several getlines together... but sometimes when you're getting it off the screen, things go ballistic because of the output stream characters, so it's just a safety thing. Hope this works!

Ben

// This
 
By the way... in the above code, set datasource equal to either cin or an open input file stream before using it;

Code:
istream& datasource; // syntax error above, should be an
                     // address

datasource=cin; //should work just like this, or

datasource.open(&quot;path/filename.ext&quot;);

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top