Results 1 to 3 of 3
  1. #1
    gogogokitty's Avatar
    Join Date
    Feb 2013
    Gender
    male
    Posts
    1,090
    Reputation
    113
    Thanks
    3,503

    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;
    }
    LEEEEEEROY JEEEEENKINS

  2. #2
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    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?
    Ah we-a blaze the fyah, make it bun dem!

  3. #3
    gogogokitty's Avatar
    Join Date
    Feb 2013
    Gender
    male
    Posts
    1,090
    Reputation
    113
    Thanks
    3,503
    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');
    LEEEEEEROY JEEEEENKINS

Similar Threads

  1. [Solved] Need assistance with some scripthook v or other related issues
    By jaywoosh in forum Grand Theft Auto 5 (GTA V) Help
    Replies: 3
    Last Post: 07-11-2015, 04:16 AM
  2. Google Related Posts
    By Dave84311 in forum General
    Replies: 3
    Last Post: 10-26-2010, 09:56 AM
  3. Hacking Warrock - Server Related - BLOG
    By Dave84311 in forum WarRock - International Hacks
    Replies: 8
    Last Post: 01-08-2006, 11:07 PM