If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Looking for a Mentor

Nope. Just the next assignment. This week we are learning arrays. I've already gotten my assignment and am ready to start working on it. *deep breath* More time to work on it this week! That way I can get help easier!!

Re: Looking for a Mentor

This routine is to obtain data from a file. However you are passing these parameters by value so any change you make to them in the function will not be reflected in the calling routine. If you want changed values to be available in the calling routine then you need to pass by reference like

There are two issues with this. Firstly == is used as the equality comparision and secondly character constants are placed within ' '. What this statement does is assigns the value of variable A to the variable vechicleClass and tests whether this value is non-zero. You use

Code:

if (vechicleClass == 'A')

or

Code:

if (vechicleClass == "A")

if comparing against a string instead of a char.

Code:

else if (5 < vehicleAge <= 10)

whilst syntactically correct, it doesn't do what you think it will. It first compares 5 with vehicleAge which gives either a 0 or a 1. It then compares this 1 or 0 with 10. As it is always less than 10, this statement is always true. You need to write

Code:

else if (vechicleAge > 5 && vechicleAge <= 10)

Last edited by 2kaud; January 14th, 2014 at 04:34 PM.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Looking for a Mentor

Those make a lot of sense!

I had figured out the vehicleClass == 'A' one. But I didn't know about the else if or the int&.

My new assignment that I'll be starting on tonight (when I'm not on a work computer) is
1. Read in a file of vehicle type and registration cost.
2. Sort that into an array. 1 for Autos and 1 for Motorcycles,
3. Calculate the highest registration cost, the lowest, and the average for each array.
4. Array size of 500 - produce an error if more than 500 lines.
5. Output to screen number of vehicles registered, high, low and average for each array.

So I'm thinking that instead of getData, I need to use getline() so that it reads a line at a time. Then have a loop to read the next line. Test something like, if (char == '\n'). Would that work?

Then for the array, I should just be able to make one of size 499 (to store 500 vehicles) and then I'm not sure how but search it for the high and low.

If you just want to calculate the highest, lowest and average for different vechicle types, there is no need to read the data into arrays. You want a struct containing high, low, average, total, numreg and then a 2 element array of this struct (for types A and M ) or even better use a map like in my example program. As you read in each line from the file, just update high, low, total, numreg for the appropriate array and then at the end calculate the average. Then just output the required info. Simples!

Last edited by 2kaud; January 14th, 2014 at 06:28 PM.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Looking for a Mentor

Originally Posted by 2kaud

If you just want to calculate the highest, lowest and average for different vechicle types, there is no need to read the data into arrays. You want a struct containing high, low, average, total, numreg and then a 2 element array of this struct (for types A and M ) or even better use a map like in my example program. As you read in each line from the file, just update high, low, total, numreg for the appropriate array and then at the end calculate the average. Then just output the required info. Simples!

The assignment is teaching us to use arrays. Haha. Hahaha.

I'm trying to get an idea of where to start before I get home from work and actually start. Haha

Re: Looking for a Mentor

Originally Posted by spideyflygirl90

The assignment is teaching us to use arrays.

Well, 2kaud's suggestion is more efficient than reading into an array and then repeatedly looping over the array to obtain the results that you want. This is a good. On the other hand, it couples together the distinct operations to obtain each result, which can be a bad since you cannot reuse the code to say, only find the highest.

Re: Looking for a Mentor

I'm trying to get an idea of where to start before I get home from work and actually start

I would start by producing a simple program that just loops reading the data from the file and displaying it on the screen (see my sample program for an idea of one way of doing it). That was the first version of the example I posted above. Once that worked OK I then extended the program in stages. Each stage was tested first before the next stage was added. This way you always have a working program - one that doesn't do all it's supposed to, but working nevertheless. Once you have a program that you know reads the data OK, then you can add code that puts it in an array. Test that. Then sort the array and test that. Then add code to find highest etc etc etc. The important point is that at every stage you have a working program that does what's it's supposed to at that stage in the design.

Oh, I don't want you guys to write it for me! Then I'll never learn and I'll forever be stuck!!! I'm working on it now and will post some code when I get stuck...hahaha. As in, if I finish writing the getLine part and it doesn't work! ;-P

Re: Looking for a Mentor

However, it's giving me an error that says "Cannot find or open the PDB file".

The PDB file (Program Database) holds debugging and project state information that allows incremental linking of a Debug configuration of your program. A PDB file is created when you build with /ZI or /Zi (for C/C++). See http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

When you define the file variable, you can specifiy the file name as wellhttp://www.cplusplus.com/reference/f...ream/ifstream/
Using .open() is OK but as you know the name of the file when you define the file variable IMO specifying the name then is easier than having a separate .open() statement.

The class function .is_open() returns true if there is a valid file opened to the file variable. If the open failed it returns false. As the program is concerned with file processing, the rest of the program is invalid if the file hasn't been opened OK. Another way of coding this could be

This way there is only 1 entry and 1 exit point to the function - rather than 2 exits as in the previous example.

stream extraction (infile >> vehicleClass ...) returns a NULL if there is a problem performing the extraction (see http://www.cplusplus.com/reference/i...perator%3E%3E/). Hence the while condition is true if the extraction is successfull and is false if there is an error with the extraction and the while loop then terminates. The extraction can fail for three reasons 1) end-of-file has been reached 2) the characters extracted could not be interpreted as a valid value of the appropriate type (trying to read an int and finds an 'A' 3) an error occured during the extraction and data couldn't be read. See http://www.cplusplus.com/reference/ios/ios/good/

Last edited by 2kaud; January 16th, 2014 at 06:54 AM.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Looking for a Mentor

Update:

So I figured out the major reason why I was struggling so much! My computer has a bug! Ahh!! I've been needing a new laptop for school, picked it up yesterday, ran the codes I have finished so far for my assignment this week and they worked!

What a relief.

So I have my openfile and getdata functions figured out! working on sort data and displayresults!

Re: Looking for a Mentor

I'm stuck again, maybe someone can help ;-P

I wrote my openFile function and tested it, worked just fine! I put a cout statement to verify that it worked. It displayed perfectly. Then I moved it into it's own function instead of main, called it from main, and now it won't display the cout statement. It just gives me a return 0 value. Here's my code: I have extra #include statements for my following code but I haven't gotten that far yet.