Thread: trouble with classes.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    trouble with classes.

    edit: actually I just figured it out. The problem was the struct. apparently it can't go there. New question. How can I use a struct in a class? Or should I just make a whole new class for that data?

    edit again: Well, I feel special. I just forgot a semi-colon after the struct. So, I answered my own questions. I may have more later though!
    Last edited by System_159; 09-19-2006 at 07:22 PM.

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Ok, So this isn't specific to classes, but since I have this thread open I'll go ahead and post it in here

    I have a float being passed into a function. The float is a combination of two numbers, the first being year, second being date. I have it set up to be year.date, so say 1998.07 would be july 1998.
    I need some help seperating the two though(if it's possible) This is what I have
    Code:
        float year, month;
        year = yearMonth/1;
        month = yearMonth % year;
    where yearMonth is being passed into the function as something like 1998.07.
    the error is as follows
    Code:
    136 C:\Dev-Cpp\add_data.cpp      invalid operands of types `float' and `float' to binary `operator%'
    Keep in mind that I'm getting this yearMonth number from inside of a different function, so I can't pass it as two seperate numbers(not that I can see anyway).
    Any help is appreciated!


    edit:
    Also:

    I'm trying to save the info that I get to a file. The file should be named by the number of the month in a folder by year.
    Code:
    dataFile.open("C:\\"year "\\"month".dat", ios::out);
    gives the error
    Code:
    207 C:\Dev-Cpp\add_data.cpp     invalid operands of types `const char[4]' and `float' to binary `operator+'
    I don't get it, is there a way to have a dynamic file out location?
    Last edited by System_159; 09-19-2006 at 07:49 PM.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    dataFile.open( ("C:\\" + year + "\\" + month + ".dat").c_str() , ios::out);
    assuming a std::string ... you may want to use some string streams too ... are year and month numbers? You'll have to convert them to std:: strings search around for functions to do it.

    try doubles for your other problem
    Last edited by twomers; 09-19-2006 at 07:59 PM.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm not sure why you want dates in that format... but regardless of that:

    Code:
    year = static_cast<int>(YearMonth);
    month = static_cast<int>(YearMonth - static_cast<int>(YearMonth));
    The error you are getting, btw, comes from the fact you can only use the modulos operator with integrals.

    EDIT: Just noticed something...

    You are declaring year and month as floats. I suppose you don't want that. They are integrals. Integers... at most long integers, if you really want to work that far in the future

    Change those declarations to int.
    Last edited by Mario F.; 09-19-2006 at 08:01 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by twomers
    Code:
    dataFile.open( ("C:\\" + year + "\\" + month + ".dat").c_str() , ios::out);
    assuming a std::string
    actually, year and month are numbers. Either double or float or whatever works for the other problem. I suppose I could store the numbers in strings if that code works.

    Quote Originally Posted by twomers
    try doubles for your other problem
    double didn't work either.


    Here's a thought I had:
    Could I use pointers to store the values of year and month in a struct somewhere? We haven't gone over pointers in class yet, but I've read over the tutorial a few times and I think that's what they're about.(on that note, we haven't really gone over classes or file i/o well enough to be doing this assignment yet )

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by System_159
    Could I use pointers to store the values of year and month in a struct somewhere? We haven't gone over pointers in class yet, but I've read over the tutorial a few times and I think that's what they're about.
    See my previous reply to fix your problem.

    Regardless, pointer have other uses. Although, invariably lead to acessing a variable value. It's just that pointers would be an unecessary complication from what I can see of what you are trying to achieve.

    You can use a class or struct though. But since you haven't gone through those also, it's best we leave it alone for now.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I have a float being passed into a function. The float is a combination of two numbers, the first being year, second being date. I have it set up to be year.date, so say 1998.07 would be july 1998.
    I'm almost sure a float (or double) would be very unsuitable for dates like that.

    If I have two floats I'd expect adding them up gives me a meaningful sum. In this case it won't. (Data is not numeric.)

    Most importantly, floating point numbers are imprecise (it's impossible to express the infinity of numbers there is between 0.0 and 1.0 or any other range). 1998.07 could easily come back to you as 1998.069999999998 or something like that.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    ok, I found a way around my problem, but of course ran into another one.
    The solution that I came up with was to use stringstreams to turn my float into a string. then string.erase() to seperate it into two different strings. here's an example of what I mean:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
        string syear, smonth;
        string syearMonth = "1998.06";
        syear = syearMonth;
        smonth = syearMonth;
        syear.erase(4);
        smonth.erase(1,5);
        cout << syear << endl;
        cout << smonth << endl;
        system("pause");
        return(0);
    }
    The problem I run into is that the zero comes back as a 1. Could anybody tell me why this is?

    edit: Mario, I just realized why the code you gave earlier didn't work for me, you had YearMonth, where I had yearMonth. So it seems I don't need the above code. But it still makes me wonder why that happened, if anybody knows. Thanks for all the help and suggestions so far!

    My gosh, sorry to keep editing, it surely makes for a mess, but I just had a problem with your tidbit Mario. Since I'm now using stringstreams and not strings I get this error:
    Code:
    214 C:\Dev-Cpp\add_data.cpp      no match for 'operator+' in '"C:\\" + yearString'
    Last edited by System_159; 09-20-2006 at 05:42 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> smonth.erase(1,5);
    Arrays (and string character positions) start from zero.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > edit: Mario, I just realized why the code you gave earlier didn't work for me, you had YearMonth, where I had yearMonth.

    Well other than that, there is somehing else fundamentally wrong with my code. If you try it with the variable renamed correctly it will give you 0 for the month. The formula is wrong because it's not taking into account the common logarithm for the extracted decimal.

    The correct formula would then be:

    Code:
    year = static_cast<int>(YearMonth);
    month = static_cast<int>((YearMonth - static_cast<int>(YearMonth)) * 100);
    But then as anon pointed out you will still have a problem. Decimal types are not geared towards precision. On my compiler, for instance, a float declared as 1874.07 becomes 1974.06995... and this is not good. Your month would be 6, instead of 7.

    So, if you really insist in having dates stored like that, stringstreams is indeed the way to go.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    I understand the concern for getting the wrong int from the float but I've tested it with all possible months and it comes out right, so as of now I'm fine with it. here's the code:
    Code:
        int year = static_cast<int>(yearMonth);
        float fmonth = ((yearMonth - year) * 100);
        int month = static_cast<int>(fmonth);
        
        ostringstream yearString;
        ostringstream monthString;
        yearString << year;
        monthString << month;
    Could I use setpercision(don't know if that's the right syntax, but you know the function) to keep it down to two after the decimal?

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    setprecision. It's a manipulator declared in the iomanip header.

    Well, no. Manipulators work only with the iostream objects (cin and cout). setprecision() works only with std::cout.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Can I ask an obvious question?

    Why don't you just store the variables in a string on the first place?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having trouble using classes
    By rainman39393 in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2008, 02:45 AM
  2. Trouble with private portion of Classes
    By SoBlue in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2007, 05:32 PM
  3. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 12:44 PM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM
  5. Trouble with classes
    By PorkyChop in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2002, 02:43 PM