Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Chryzmo

fin.ignore Problems

This topic is 5384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heyas, I am trying to use fin.ignore to read through a text file and fine a line that says "XPR = ", leaving a number to be read next, however I can't seem to get this to work. fin worked before I tried adding the XPR part.

//text file - there is a space at the end of each line

XPR=1 
lalala
other text that shouldn't be read

//fin function

#include <iostream>
#include <fstream>

using namespace std;



int main()
{
    struct MVAR
    {
        //variable to read into

        // I have also tried making this char XPR[1], that did not work either

        int XPR; 
        void MVAR::getdata()
        {
                //Open the file with the data

                ifstream fin("XPR.txt");
        
                //Import Variable

                fin.ignore(20, 'XPR=');
                fin >> XPR;
        
                //show result

                cout << XPR;
                
                //close the file

                fin.close;
        }
};
    
    //call and instance of the struct

    MVAR monster;
    //call the function

    monster.getdata();
  
  system("PAUSE");	
  return 0;
}

When I use char XPR[1] I get some weird character, not even a letter. When I use int I get a bunch of numbers, not the one I'm trying to get. Also, Dev-C++ gives me this warning: 32 E:\Program Files\Dev-Cpp\MyProjects\main.cpp:21 [Warning] multi-character character constant I've been told to treat warning as errors, so is there anyway around this one? One more thing, does anyone know what the differnce between the C++ Primer and the C++ Primer Plus is? Thanks, Chris [edited by - Chryzmo on September 22, 2003 6:10:29 PM]

Share this post


Link to post
Share on other sites
Advertisement
from what i know you aren''t supposed to be declaring functions inside structs... does this work?

C++ Primer is a book that gets you primed for learning c++. it explains the concepts to you and gives you a little boost in your studies of the language. it does not teach you all the details and tricks of it but it does help as a good study aid. C++ Primer Plus i would imagine would polish over some areas that weren''t covered initially and dive into some more advance areas that you may of not been quite ready for at the beginning.

Share this post


Link to post
Share on other sites
quote:
Original post by iNfuSeD
from what i know you aren''t supposed to be declaring functions inside structs... does this work?


In C++ structs are approximately equal to classes.

Chryzmo: I suggest you review character arrays and character constants as well as how to call functions (fin.close


Colin Jeanne | Invader''s Realm

Share this post


Link to post
Share on other sites
quote:
Original post by Chryzmo
One more thing, does anyone know what the differnce between the C++ Primer and the C++ Primer Plus is?



quote:
Original post by iNfuSeD
C++ Primer is a book that gets you primed for learning c++. it explains the concepts to you and gives you a little boost in your studies of the language. it does not teach you all the details and tricks of it but it does help as a good study aid. C++ Primer Plus i would imagine would polish over some areas that weren''t covered initially and dive into some more advance areas that you may of not been quite ready for at the beginning.

No.

They are unrelated books.

  • C++ Primer by Stanley Lippman and Josee Lajoie
  • C++ Primer Plus by Stephen Prata

    Oh, and the difference is that C++ Primer by Stanley Lippman and Josee Lajoie is an excellent book, whilst the other isn''t.

    [ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by iNfuSeD
    from what i know you aren't supposed to be declaring functions inside structs... does this work?



    In C++ you can declare functions in structs, but in C you can't.

    About 'fin.ignore(20, 'XPR=');'. Make sure that it does not only ignore one character at a time. You have passed it a string. I could be very wrong, though. But check anyway.



    [edited by - mathematix on September 23, 2003 11:09:12 AM]

    Share this post


    Link to post
    Share on other sites
    see http://www.cplusplus.com/ref/iostream/istream/ignore.html
    ignore
    Tadd
    - WarbleWare

    [edited by - reana1 on September 23, 2003 11:23:30 AM]

    Share this post


    Link to post
    Share on other sites
    Yeah, I got it to work. You can''t use any kind of variable except char. Is there some form of char that can hold numbers? I have tried putting a number into one and then using a line like

    if (XPR == 1)

    and it always gives me an error saying it cannot convert the char to an int value, or something to that effect. It can read in the 1 (from my example text file), however I have no way of saying is XPR == 1... I have tried putting it in single and double quotes and it never works for me, or it returns some bogus value.

    Thanks for the help so far

    -Chris-

    Share this post


    Link to post
    Share on other sites
    first, this line:
    fin.ignore(20, ''XPR='');

    XPR= should be in double quotes not single quotes. Single quotes should only be used for SINGLE characters. Your warning is a direct result of this mistake.

    And you can compare char''s using logical operators. even characters/strings aren''t stored as actual characters, they''re stored in the char variables as their ascii numerical codes.

    If you want to display the char correctly as a number rather than its ascii numerical code, then cast it to an int before you display it, like so:
    cout << (int)myInput;



    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by deathtrap
    If you want to display the char correctly as a number rather than its ascii numerical code, then cast it to an int before you display it, like so:
    cout << (int)myInput;




    So, how would I go about casting it as an int in an if statement?



    // Now, I have this

    // I have also tried it with double quotes and it does not work

    if (XPR == ''1'')

    // I tried this based on what you just posted

    // Also, I tried double quotes as well...

    if (XPR == (int)''1'')

    // Both times I get this error

    BattleArenaBattle.cpp:115: ISO C++ forbids comparison between pointer and integer


    Share this post


    Link to post
    Share on other sites

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    Participate in the game development conversation and more when you create an account on GameDev.net!

    Sign me up!