fin.ignore Problems

Started by
11 comments, last by Chryzmo 20 years, 6 months ago
you''re still declaring XPR as int XPR right? if not then tell me how you''re declaring it.
Advertisement
No, I had changed XPR to char XPR[3]. Sorry if I forgot to mention that :\. I tried using int to start with, but the fin.getline function returned an error:

main.cpp:20: invalid conversion from `int'' to `char*''

main.cpp:20: initializing argument 1 of `std::basic_istream<_CharT, _Traits>&
std::basic_istream<_CharT, _Traits>::getline(_CharT*, int) [with _CharT = char, _Traits = std::char_traits]''

When I use char as the variable type, as I am pretty sure the fin.getline function only works with a char variable, it will read the character, in this case a number, in from the text file, but when I add the if statement, I get the error I mentioned above.

Here is the code I have now, this will run however, the if statement won''t work even though I make sure XPR == 1. I have tried single quotes aswell but that returns this error:

main.cpp:26: ISO C++ forbids comparison between pointer and integer

//  text fileXPR=1 lalalal other text that shouldn''t be read yet....//  main.cpp//  This will compile and run, but the if statement does not go //  through#include <iostream>#include <fstream>using namespace std;int main(){    struct MVAR    {        //Variable based on player''s XP, decides what type of monster to use.        int PlayerXPR;        char XPR[3];        void MVAR::getdata()        {                //Open the file with the data                ifstream fin("XPR.txt");                        //Import Variables                fin.ignore(200, ''='');                fin.getline(XPR, '' '');                                //  Make sure XPR is 1                        cout << endl << "XPR = " << XPR << endl;                                //this statement does not execute                if (XPR == "1") {                                cout << "Woo! it is working now";                }        }};        MVAR monster;    monster.getdata();    system("PAUSE");	  return 0;}
Oh my god. either use strcmp to compare the string in XPR or use XPR[0] == 1 (without the quotes) . What you''re trying to do right now in that if statement is compare the address of XPR(not the actual value of XPR but the memory address of XPR) to a string. This is wrong on so many levels.

I suggest you read through some documents on pointers, and arrays, and C-style strings.
Also , to have fin.getline or fin.get read into a variable other than a char, you have to cast the variable to a char pointer and pass in its address, like so:
fin.getline((char*)&XPR,...);

This topic is closed to new replies.

Advertisement