I need help as soon as possible.

This is a discussion on I need help as soon as possible. within the C++ Programming forums, part of the General Programming Boards category; What I am trying to do is: a) The total number of trucks sold and total truck sales for each ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    18

    I need help as soon as possible.

    What I am trying to do is:

    a) The total number of trucks sold and total truck sales for each
    month at all dealerships.

    b) The total number of cars sold and total car sales at each
    dealership for each month.

    c) The most popular pick-up sold at all dealerships.

    d) The dealership with the most SUVís sold in each month.

    e) The most popular mid-size car sold at all dealerships.


    A sample of the output file looks like:

    SUV Escape 03219380
    Full Fusion 09117145
    Mid-SizeFusion 02317145
    Small Focus 09317145
    SUV Explorer 11221975
    Small Focus 06221975
    Full Taurus 08220830
    Pick-Up F-150 12321325
    Full Mustang 10116470
    Full Fusion 01217145
    SUV Escape 10219380
    SUV Excursion 01238035
    Mid-SizeTaurus 07320830
    Full Mustang 05216470


    in the number at the end of each line the first two numbers represents the month the vehicle was sold, the third number is the dealership(there is only 1-3) and the remaining numbers are the price of the vehicle.




    What I have so far is:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <conio>
    
    using namespace std;
    ofstream out;
    struct cars 
    {
      char type[9];
      char model[15];
      char year[3];
      char dealership[2];
      double price;
    };
    
    
    void ReadString(ifstream&, char[],int);
    
    int main()
    {
    cars hi;  
    ifstream input;
    // ofstream out;
    
    out.open("cars.out");
    input.open("cars.dat");
    ReadString(input,hi.type,8);
    while(!input.eof())
    {
      ReadString(input,hi.model,14); 
      ReadString(input,hi.year,2);
      ReadString(input,hi.dealership,1); 
      input >> hi.price;
      out << hi.price << endl;
      input.get();
      ReadString(input,hi.type,8);
    }
    
    input.close();
    out.close(); 
    return 0; 
    }
    /*********************************************************/
    void ReadString(ifstream& input, char line[],int Limit)
    {
    int I;  
    
    I = 0;
    while (I < Limit && !input.eof())
      {
      input.get(line[I]);
      I++; 
      }
    if (!input.eof())
    {  
      line[I] = '\0';
       out << line; 
    }
    }
    I think that i need to convert the char to int somehow and keep track of each type of sale.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I think you should specify in what format are they in .dat file.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    18
    somewhat like:
    (CLASS 1 - 8,
    MODEL NAME 9 - 22, MONTH PURCHASED 23 - 24, DEALERSHIP Col 25, and
    PURCHASE PRICE 26 - 30)

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Opps

    opps

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The basic rule for reading from a file is: the read statement should be part(or all of) your while conditional, e.g.
    Code:
    while( inputFile.get(achar) )
    {
    	...
    	...
    }
    inputFile.get(achar) calls a function that reads data into achar and then returns the inputFile object, which converts the while conditional to:

    while(inputFile)

    If any errors occur that will prevent you from reading data from the file, the inputFile object will evaluate to false in the while conditional. eof is considered an error, so the while loop will terminate correctly when you reach the end of the data in a file. However, there are other possible errors that can occur while reading from a file. If one of those errors occurs, your current while loop will try to keep reading data because it hasn't encountered eof yet(and because the counter hasn't reached its maximum value)--but the error will prevent you from reading any data, and you won't have any way of knowing you aren't reading in data. The while loop will continue looping and not reading in data until the loop counter reaches its max and the while loop terminates, and you'll think you successfully read in all the data.

    If you change your while loop by putting the read statement in the while loop conditional, the loop will end immediately if there's any error, so you will be able to check the counter's value to find out whether you were able to read in all the data succesfully: if the loop counter is equal to the max, you read in all the data; if it's less than the max, the loop terminated prematurely due to an error.

    One thing you have to be careful about in the case of a compound while conditional where one of the conditionals is a read statement is that the combined conditional can fail causing the while loop to end--but the read statement will still execute. For instance, if you do this:

    while ( inputFile.get( data[i] ) && i < 10 )

    if i equals 10, the read statement will execute and then the loop counter will be evaluated, terminating the loop. However, you don't want the read statement to execute on that last loop because then you will read in the 11th character and all you want is 10. The solution is to switch the order:

    while ( i < 10 && inputFile.get( data[i] ) )

    C++ knows that if the first conditional is false, the combined conditional can't be true because when you && two conditionals together they both have to be true for the combined conditional to be true. So, if the first conditional is false, C++ is smart enough to know the combined conditional will be false no matter what the second conditional evaluates to, so it doesn't need to evaluate the second conditional, and it just returns false immediately. Therefore, the read statement won't execute.
    ----
    In this line:
    Code:
    out.open("cars.out");
    You are using the variable 'out', but it doesn't exist because you never declared it.
    ----
    Code:
    ReadString(input,hi.type,8);
    Is the "type" the first 8 characters in the file? Looking at the first line of your file:

    SUV Escape 03219380

    the 8th character is the 'p' in Escape.
    ----

    You need to try to determine in your own mind why you are using get() to read from the file? Assuming the variable 'type' has already been declared, why not read type like this:

    inFile>>type;

    Then you could read the model like this:

    inFile>>model;

    and you could read the numerical code like this:

    inFile>>code;

    Or, written more succinctly:

    inFile>>type>>model>>code;

    That will read the first three pieces of data from your file. When you use >> to read in data, a piece of data is deemed to end when any whitespace is encountered--whitespace is a space, tab, or newline--so it's a perfect way to read in data when your data is separated by spaces and newlines.

    But you also need to read in many lines of data and store all that data. If you put that read statement in a while loop conditional, it will overwrite the values in the variable every time through the loop. So, how about creating an array of cars to hold all the data? Each car can store one line of data. You can set a loop variable called 'i' equal to 0 in a statement before the while loop, and inside the while loop you can increment the variable i++ to change the index position in your car array.
    Last edited by 7stud; 11-07-2005 at 07:58 PM.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    18
    it is spaced out more like:
    SUV-----Escape---------01235648
    Last edited by hyrule; 11-07-2005 at 08:38 PM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It doesn't matter whether there is one space or 100 spaces or even 20 newlines separating each piece of data, the operator>> will do the job as long as the data is separated by whitespace(is your data actually separated by dashes?). The operator>> is programmed to skip all leading whitespace and terminate reading at the first whitespace it encounters. Applying those rules to the example input you posted(assuming the dashes are really spaces): the first time operator>> is used, it reads in "SUV" and stops after reading in the 'V'. Then because operator>> skips leading whitespace, the next time operator>> is used it will skip to the 'E' in "Escape" and start reading in data and then stop after reading in the 'e'. The next time operator>> is used it will skip to the beginning of the number sequence, start reading, and then stop after reading in the '8'.
    Last edited by 7stud; 11-08-2005 at 04:24 PM.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Next time you write a program with a "cin >>" statement, when you run it and come to the input, type a couple of enters and spaces and watch it read the input fine.
    Sent from my iPadģ

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21