homework related issue

Posts 1–3 of 3 · Page 1 of 1
homework related issue
any idea why its messing up on the second element in the array like this?
https://gyazo.com/ce96dabd37fdb71e76755704a54ff9a2

Code:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

const int MAXSIZE = 50;
const int NAMESIZE = 15;

struct	budget	// declare a structure to hold name and financial information
{
	char name[NAMESIZE + 1];
	float income;		// person's monthly income 
	float rent;			// person's monthly rent
	float food;			// person's monthly food bill 
	float utilities;	// person's monthly utility bill 
	float miscell;		// person's other bills
	float net;			// person's net money after bills are paid
};

int main()
{
	fstream indata;
	ofstream outdata;
	char answer = 'Y';

	indata.open("income.dat", ios::out | ios::binary);

	outdata.open("student.out");

	outdata << left << fixed << setprecision(2);

	budget person[MAXSIZE];

	for (int i = 0; answer == 'y' || answer == 'Y'; i++) 
	{
		cout << "Enter the following information" << endl;

		cout << "Person's name: ";
		cin.getline(person[i].name, NAMESIZE);

		cout << "Income: ";
		cin >> person[i].income; cin.ignore();

		cout << "Rent: ";
		cin >> person[i].rent; cin.ignore();

		cout << "Food: ";
		cin >> person[i].food; cin.ignore();

		cout << "Utilities: ";
		cin >> person[i].utilities; cin.ignore();

		cout << "Miscell: ";
		cin >> person[i].miscell; cin.ignore();

		person[i].net = (person[i].income) - person[i].rent - person[i].food - person[i].utilities - person[i].miscell;

		indata.write((char*)&person, sizeof(person));

		indata.close();

		indata.open("income.dat", ios::in | ios::binary);

		indata.read((char*)&person, sizeof(person));
		
		outdata << setw(20) << "Name" << setw(10) << "Income" << setw(10) << "Rent"
			<< setw(10) << "Food" << setw(15) << "Utilities" << setw(15)
			<< "Miscellaneous" << setw(10) << "Net Money" << endl;

		outdata << setw(20) << person[i].name << setw(10) << person[i].income << setw(10) << person[i].rent << setw(10) << person[i].food << setw(15) << person[i].utilities << setw(15) << person[i].miscell << setw(10) << person[i].net << endl << endl;

		cout << "Enter a Y if you would like to input more data: ";
		cin >> answer; cin.ignore();
	}

	outdata.close();

	return 0;
}
It appears to me that the name is not null terminated, that Í character is 0xCD which is what is used as "uninitialised" value in debug builds.
Why don't you use std::string for the name?
Quote Originally Posted by Hell_Demon View Post
It appears to me that the name is not null terminated, that Í character is 0xCD which is what is used as "uninitialised" value in debug builds.
Why don't you use std::string for the name?
ill look again when i get back. and because its part of the template the professor gave us to use

- - - Updated - - -

anyways fixed it for anyone that cares
Code:
indata.ignore(87, '\n');
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us