Structure and reading from a file

This is a discussion on Structure and reading from a file within the C++ Programming forums, part of the General Programming Boards category; Hello all, I had to made a code to read from an input file using structures. The input file was ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    Princess Anne, Maryland, United States
    Posts
    8

    Structure and reading from a file

    Hello all, I had to made a code to read from an input file using structures. The input file was information relating to a car dealership. I didn't post the input file, but I can if you need me to. I had to calculate the total revenue, the cars that have years greater than 2000, the cars that have prices under 5000, the cars that were black, and the cars that were toyotas and red. I am having trouble with the total revenue, the cars that are black, and the cars that are toyotas and red, other than that my program works fine. Can you help me please?


    Code:
     
    # include<cmath>
    # include<iostream>
    # include<iomanip>
    # include<cstring>
    # include<cctype>
    # include<fstream>
    # include<string>
    
    usingnamespace std; 
    
    int main()
    {ifstream fin;
    fin.open("fin.txt");
    
    int cnt2000 = 0; 
    int cnt5000 = 0; 
    int cntcolor = 0; 
    int cntcoma = 0; 
    int sum = 0; 
    
    struct car {
    string id;
    string make;
    string model;
    int year;
    string color;
    int purchase_month;
    int purchase_day;
    int purchase_year;
    float cost;
    int sold_month;
    int sold_day;
    int sold_year;
    int sale;
    int revenue;
    }mycar;
    
    cout << "ID " << "Make " << "Model " << "Year " << "Color " << "Purchased " << "Cost " << "Sold " << "Sale " << "Revenue " << endl; 
    
    cout << "************************************************************************" << endl; 
    
    while (fin)
    {fin >> mycar.id >> mycar.make >> mycar.model >> mycar.year >> mycar.color >> mycar.purchase_month >> mycar.purchase_day; 
    
    fin >> mycar.purchase_year >> mycar.cost >> mycar.sold_month >> mycar.sold_day >> mycar.sold_year >> mycar.sale; 
    
    mycar.revenue = mycar.sale - mycar.cost; 
    
    cout << mycar.id << " " << mycar.make << " " << mycar.model << " " << mycar.year << " " << mycar.color << " "; 
    
    cout << mycar.purchase_month << "/" << mycar.purchase_day << "/" << mycar.purchase_year << " " << mycar.cost << " "; 
    
    cout << mycar.sold_month << "/" << mycar.sold_day << "/" << mycar.sold_year << " " << mycar.sale << " " << mycar.revenue << endl; 
    
    cout << endl; 
    
    sum = ++mycar.revenue; 
    
    if (mycar.year > 2000) 
    {++cnt2000; }
    
    if (mycar.cost < 5000) 
    {++cnt5000; }
    
    if (mycar.color == "black")
    { ++cntcolor; }
    
    if (mycar.make == "toyota" && mycar.color == "red") {++cntcoma; }
    };
    
     
    cout << "Total Revenue = " << sum << endl; 
    cout << "Year greater than 2000 = " << cnt2000 << endl; 
    cout << "Cost less than 5000 = " << cnt5000 << endl; 
    cout << "Blacks = " << cntcolor << endl; 
    cout << "Toyota and Red = " << cntcoma << endl; 
     
    system ("pause"); 
    
    return 0; 
    }
    
    





  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    You should repost your code, only this time make sure you "paste as text" and not "paste as HTML".

    Then perhaps we might see some decent indentation, and somewhat consistently coloured code.

    The "wall of blue" really isn't helping that much.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Can you also tell us in detail what "having trouble" means in this quote?

    I am having trouble with the total revenue, the cars that are black, and the cars that are toyotas and red, other than that my program works fine.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Location
    Princess Anne, Maryland, United States
    Posts
    8
    Sorry what I meant by "having trouble" is that when I run my program I don't get the results that I want. I don't know how to add all the revenues up correctly and I don't know how to search for a string correctly to output results. And as for the rest of my program I get when I intended its only these few things that aren't working. I posted a better version of my code below sorry for all the blue lol.

    Code:
     
    
    # include<cmath>
    # include<iostream>
    # include<iomanip>
    # include<cstring>
    # include<cctype>
    # include<fstream>
    # include<string>
    
    usingnamespace std; 
    
    int main()
    {ifstream fin;
    fin.open("fin.txt");
    int cnt2000 = 0; 
    int cnt5000 = 0; 
    int cntcolor = 0; 
    int cntcoma = 0; 
    int sum = 0; 
    struct car {
    string id;
    string make;
    string model;
    int year;
    string color;
    int purchase_month;
    int purchase_day;
    int purchase_year;
    float cost;
    int sold_month;
    int sold_day;
    int sold_year;
    int sale;
    int revenue;
    }mycar;
    
    cout << "ID " << "Make " << "Model " << "Year " << "Color " << "Purchased " << "Cost " << "Sold " << "Sale " << "Revenue " << endl; 
    
    cout << "************************************************************************" << endl; 
    
    while (fin)
    {fin >> mycar.id >> mycar.make >> mycar.model >> mycar.year >> mycar.color >> mycar.purchase_month >> mycar.purchase_day; 
    
    fin >> mycar.purchase_year >> mycar.cost >> mycar.sold_month >> mycar.sold_day >> mycar.sold_year >> mycar.sale; 
    
    mycar.revenue = mycar.sale - mycar.cost; 
    
    cout << mycar.id << " " << mycar.make << " " << mycar.model << " " << mycar.year << " " << mycar.color << " "; 
    
    cout << mycar.purchase_month << "/" << mycar.purchase_day << "/" << mycar.purchase_year << " " << mycar.cost << " "; 
    
    cout << mycar.sold_month << "/" << mycar.sold_day << "/" << mycar.sold_year << " " << mycar.sale << " " << mycar.revenue << endl; 
    
    cout << endl;
     
    sum = ++mycar.revenue; 
    if (mycar.year > 2000) 
    {++cnt2000; }
    
    if (mycar.cost < 5000) 
    {++cnt5000; }
    
    if (mycar.color == "black")
    { ++cntcolor; }
    
    if (mycar.make == "toyota" && mycar.color == "red") 
    {++cntcoma; }
    };
     
    cout << "Total Revenue = " << sum << endl; 
    cout << "Year greater than 2000 = " << cnt2000 << endl; 
    cout << "Cost less than 5000 = " << cnt5000 << endl; 
    cout << "Blacks = " << cntcolor << endl; 
    cout << "Toyota and Red = " << cntcoma << endl; 
     
    system ("pause"); 
    return 0; 
    }
    
    Last edited by acmarshall; 12-12-2011 at 02:16 PM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    #1
    Code:
    sum = ++mycar.revenue;
    That's probably not what you want.





    #2
    Code:
    if (mycar.color == "black")
    { ++cntcolor; }
    if (mycar.make == "toyota" && mycar.color == "red") 
    {++cntcoma; }
    The comparison is case sensitive. If your input file has "TOYOTA" or "Toyota" or "ToYoTa" then it isn't going to match "toyota" since they are considered different. It might help to see what's in the input file.





    #3
    Code:
    while (fin)
    This results in the same problem/bug as using eof to control a loop. Instead you should test the result of the read operation which would look like this:
    Code:
    while( fin >> mycar.id >> mycar.make >> mycar.model >> mycar.year >> mycar.color >> mycar.purchase_month >> mycar.purchase_day >> mycar.purchase_year >> mycar.cost >> mycar.sold_month >> mycar.sold_day >> mycar.sold_year >> mycar.sale )
    That's a mouthful and so I'd recommend you overload the stream extraction operator (operator>>) if possible so you could reduce that to:
    Code:
    while( fin >> mycar )
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    What you would expect vs. what you are receiving would be helpful information.

    Code:
    sum = ++mycar.revenue;
    That adds one to the value of mycar.revenue and sets the value of sum to that incremented value. Therefore, you need to rethink that logic. And that's pretty much all it is - thinking about the logic of that line.

    I don't know how to search for a string correctly to output results
    Where does that fit in with what you've posted, either in code or in the details you've provided? This looks to be a strict read the data in, do calculations, and output the results program.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Location
    Princess Anne, Maryland, United States
    Posts
    8
    Thank you! I just changed my program. I used sum = sum +mycar.revenue. And the first letters of my strings were capital. I can't believe it was that simple lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text file into a structure
    By jb1989 in forum C Programming
    Replies: 1
    Last Post: 12-13-2010, 11:17 AM
  2. Need help:reading and writing structure into file
    By GOGO1104 in forum C Programming
    Replies: 7
    Last Post: 10-26-2010, 12:04 PM
  3. Reading structure from file
    By nime in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2010, 04:17 PM
  4. reading structure from file..??
    By CyC|OpS in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 05:28 AM
  5. Reading in from 1 file into 1 dynamic structure
    By ajax in forum C Programming
    Replies: 2
    Last Post: 07-22-2002, 02:40 AM

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