troubleshooting Question

ofstream giving incorrect values

Avatar of businessesatoz
businessesatoz asked on
CC++
13 Comments1 Solution704 ViewsLast Modified:
I am working on a learning code using multiple files and i'm having some issues with ofstream.. my programs job is make ask the user how many birthdates he would like to enter and then write them out to a file in a  good formatted format. I got most of it running but i can't seem too a file . The rest i did get working..

when it creates the file it prints out junk stuff like "ª r·GÝ™\eÕ
13971760/13971868/-1713535878" should show "Joe 10/5/1988" (NAME, Month, Day, Year) format as birthday.

here is my current files:
bdays.cpp (this files job is to get the information from the user regarding the birthdates.
#include <iostream>
#include <fstream>
#include "bdays.h"
using namespace std;

void GetBdayRecs(int number_of_b_days,ofstream& out_stream)
{
	int number=1;
	bday Birth_date;

	do
	{
		cout <<"Birth Date " <<number <<endl;
		cout <<"Name: ";
		cin >> Birth_date.name;
		cout <<"month:" ;
		cin >> Birth_date.month;
		cout <<"Day:";
		cin >> Birth_date.day;
		cout <<"Year:";
		cin >> Birth_date.year;
		cout <<endl;
		number++;
	}while(number <= number_of_b_days);

}
void WriteBDayInfo(bday Birth_date_info, ofstream& out_stream)
{
	out_stream <<Birth_date_info.name;
	out_stream <<endl;

	out_stream << Birth_date_info.month <<"/"  <<Birth_date_info.day <<"/" <<Birth_date_info.year;

}

main.cpp
// ============================================================================
// This program allows the user to enter records of birthday information and
// save them to a formatted output file.
// ============================================================================

#include    <iostream>
#include    <fstream>
#include    <cstdlib>
#include    "bdays.h"
using namespace std;

// ==== main ==================================================================
//
// ============================================================================

int     main(void)
{
        int         numDays;
        char        fname[FNAME_LEN];
        ofstream    birthdayFile;
		bday Birth_date;

    // get the name of the output file
    cout << "What is the name of the output file? ";
    cin.getline(fname, FNAME_LEN);

    // open the output file
    birthdayFile.open(fname);
    if (!birthdayFile.is_open())
        {
        cerr << "Unable to open the output file..." << endl;
        exit(EXIT_FAILURE);
        }
	
    // ask the user how many birthdays to record
    cout << "How many birthdays would you like to save? ";
    cin >> numDays;

    // get user info, write it to the file
    GetBdayRecs(numDays,birthdayFile);
	WriteBDayInfo(Birth_date, birthdayFile);

    // close the output file

    birthdayFile.close();

   // display a message and quit
    cout << numDays << " birthdays written to file " << fname << endl;
	system("pause");
    return  0;

}  // end of "main"

bday.h
const int FNAME_LEN=50;
using namespace std;

struct bday{
	
	char name[FNAME_LEN];
	int month;
	int day;
	int year;
};

void GetBdayRecs(int number_of_b_days, ofstream& out_stream);
void WriteBDayInfo(bday Birth_date_info, ofstream& out_stream);
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros