Write a program that opens a file, reads records into a container of data structures, and prints out the sum in order.
You can use Vectors to contain the data structures.

  1. Create a data structure to represent the record (struct cost in cost.h)
  2. Write a function called parse_account that parses one (string) record from a file, and populates a data structure with that data.
  3. Write a function called sum_accounts that, when passed a container of structures, returns a double representing the sum of the amounts in those structures.
  4. Create a main program
    a) Create an appropriate container of data structures
    b) Open the accounts file (costfile.txt)
    c) While you can read a line from the file without error
    Call parse_account, which returns a data structure.
    Add the returned data structure to the container (using, pushToV)
    d) Call sum_accounts to determine the amount of money represented
    e) Print a message and the result.

Try this with some sample data, such as the following lines in costfile.dat
You may add or create your own data file to test the program with:
Pass_Go 200.0 135
Reading_RR -50.0 136
Connecticut -120.0 137
Chance 25.0 138

Please help.

Im sorry I forgot to add what I have so far.

**under cost.h I have the following**
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
typedef vector<cost> costRecords;
cost parse_account (char record[]);
double sum_accounts(costRecords accounts);

extern costRecords cost_items;
struct cost {
    string description;
    double amount;
};

**under the mainassignment.cpp I have this**
// Assignment9.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
costRecords cost_items;

ifstream input("C:\Users\Hello\Desktop\Assignment9\costfile.txt");

int _tmain(int argc, _TCHAR* argv[])
{
    char buffer[100];
    while ( input.getline(buffer, sizeof buffer) ) {
       cost c = parse_account(buffer);
       cost_items.push_back(c);
       double sum = sum_accounts(cost_items) ;



}





ERRORS:
1>  cost.cpp
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(7): error C2146: syntax error : missing ';' before identifier 'cost_items'
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(9): error C2146: syntax error : missing ';' before identifier 'parse_account'
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(10): error C2146: syntax error : missing ';' before identifier 'thisCost'
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(10): error C2065: 'thisCost' : undeclared identifier
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(12): error C2065: 'thisCost' : undeclared identifier
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(12): error C2228: left of '.description' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(13): error C2065: 'thisCost' : undeclared identifier
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(13): error C2228: left of '.amount' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(14): error C2065: 'thisCost' : undeclared identifier
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(17): error C2146: syntax error : missing ')' before identifier 'accounts'
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(17): error C2059: syntax error : ')'
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(17): error C2143: syntax error : missing ';' before '{'
1>c:\users\hello\desktop\assignment9\assignment9\cost.cpp(17): error C2447: '{' : missing function header (old-style formal list?)
1>  Assignment9.cpp
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(8): error C2146: syntax error : missing ';' before identifier 'cost_items'
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(10): warning C4129: 'H' : unrecognized character escape sequence
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(10): warning C4129: 'D' : unrecognized character escape sequence
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(10): warning C4129: 'A' : unrecognized character escape sequence
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(10): warning C4129: 'c' : unrecognized character escape sequence
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(16): error C2065: 'cost' : undeclared identifier
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(16): error C2146: syntax error : missing ';' before identifier 'c'
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(16): error C2065: 'c' : undeclared identifier
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(16): error C3861: 'parse_account': identifier not found
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(17): error C2228: left of '.push_back' must have class/struct/union
1>          type is 'int'
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(17): error C2065: 'c' : undeclared identifier
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(18): error C3861: 'sum_accounts': identifier not found
1>c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(24): fatal error C1075: end of file found before the left brace '{' at 'c:\users\hello\desktop\assignment9\assignment9\assignment9.cpp(13)' was matched
1>  Generating Code...

Edited 4 Years Ago by funnyguy1

UNDER cost.cpp I have

#include "stdafx.h"
#include <iostream>
#include <sstream>

using namespace std;

costRecords cost_items;

cost parse_account (char record[]) {
    cost thisCost;
    istringstream istr(record) ;
    istr >> thisCost.description;
    istr >> thisCost.amount;
    return thisCost;
}

double sum_accounts(costRecords accounts) {
    double results = 0.0;
    for (int i=0; i<accounts.size(); i++)
        result += acounts[i] .amount;
    return result;
}

Write a function called parse_account that parses one (string) record from a file, and populates a data structure with that data.

Reasonable start. But I would start with the basics. Forget structs for the time being.

Get this working with your file:

Write a function called parse_account that parses one (string) record from a file, and populates a data structure with that data.

Also, are you allowed to use std::strings or must you stick with char[] arrays?

Edited 4 Years Ago by iamthwee

This article has been dead for over six months. Start a new discussion instead.