Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java programmer trying to write C++

  • 15-03-2003 3:01pm
    #1
    Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭


    God dammit this language is frustrating :)

    Right, this is an assignment, where the main objective is to learn about OOP. I have an Employee abstract class, and two implmentations - PermanentEmployee and TemporaryEmployee. That's fine, and compiles/works fine, but the test bench we're required to write is what's annoying me :)

    Basically, given a file with 3 lines for each employee (name, type of employee and salary), we need to determine whether the employee is permanent or part-time, then create the object and push it into a vector.

    2 problems -

    1. Comparing strings. I'm using the string object for my strings instead of char *, and can't find any way to compare two strings, e.g. in Java, it would be string1.equals(string2).

    2. Reading a double in from the file - the salary bit. When I read it using fin >> myDouble, it doesn't progress to the next line, and causes all sorts of errors. Instead, I could read it in as a string, then parse it to a double, but I don't know how to do this either.

    Cheers all :)


Comments

  • Registered Users, Registered Users 2 Posts: 2,281 ✭✭✭DeadBankClerk


    double salary;
    fscanf(file, "%Lf\n", &salary);
    // %Lf = long float
    // '\n' = end of line character

    This will read the double salary from file.


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Right, problem one sorted :). The ANSI string class has a method to change a string to a char *. From there you can use strcmp to compare them, i.e.

    strcmp(myString.c_str(), "A String")

    Is there any other way?

    (and still have the other prob :))

    {EDIT:

    DBC - can the fscanf() be used to read in the strings too? As I have it, The file is being read in through an ifstream, and not the scanf yokie......


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by seamus
    1. Comparing strings. I'm using the string object for my strings instead of char *, and can't find any way to compare two strings, e.g. in Java, it would be string1.equals(string2).

    The equivalent in C++ is string1 == string2

    This was considered too complicated, so Java uses .equals.

    2. Reading a double in from the file - the salary bit. When I read it using fin >> myDouble, it doesn't progress to the next line, and causes all sorts of errors. Instead, I could read it in as a string, then parse it to a double, but I don't know how to do this either.

    Eat the newline. An ignored call to getline would be one way to do this.

    fscanf() is for legacy code and code that has to work with C only. Avoid.

    Don't define your code in terms of ifstream. Apart from the code that obtains the ifstream define it in terms of istream so that you can easily use it for other types of istream.

    BTW. If you can, put the employee type as the first line, this allows you to first test the type, then create an instance of this type, and then to in >> employee. You will need to write something in the employee classes to handle that being done, but if the employee classes become more complicated, if you want to use other types of istream, if you add new types of employee, etc.

    You can't make the >> handler virtual, so you would have to write a >> handler in the base that calls a virtual function with the stream, that virtual function can be private.


  • Registered Users, Registered Users 2 Posts: 683 ✭✭✭TenLeftFingers


    Seamus, I haven't done any c++ in about a year, but there's a website called www.programmersheaven.com.

    It's the buisness. Chances are, any question you want to ask has been asked ans answered there. Got me through the year ;


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by merlin_bar
    Chances are, any question you want to ask has been asked ans answered there.
    Such as the question that's already been answered :rolleyes:


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 683 ✭✭✭TenLeftFingers


    The link I provided is fhelpful or programming in general, not just this specific question. Spare me the sarcasm. Surely you have better things to be doing.


Advertisement