Jump to content

Check out our Community Blogs


Member Since 12 Sep 2010
Offline Last Active Jan 05 2011 11:09 PM

Posts I've Made

In Topic: Confused as to how to make a .dat file? (To test ifstream/ofstream)

24 November 2010 - 02:34 AM

Ok so I understand that the program automatically understands whitespaces as new integers and that in order to use something like a comma (,) to seperate the numbers I will have to use the command "getline();" with my ifstream command. However, I fixed the code like you said (1 2 3 4) and it still shows no results. When I run the program, the outfile.dat is unchanged and when I change the program to "cout" to test it by sending it to the screen AGAIN it sends messed up information. (something like "192401" instead of "6" (the sum of the first three digits)) Any reason this is happening? (Not writing to the outfile.dat like it is supose too, as well as giving false information)

In Topic: Confused as to how to make a .dat file? (To test ifstream/ofstream)

24 November 2010 - 02:08 AM

This is the first im hearing about them being stored as characters and not integers. This is the SAMPLE program that my BOOK shows:
#include <fstream>

int main()
    using namespace std;
    ifstream in_stream;
    ofstream out_stream;


    int first, second, third;
    in_stream >> first >> second >> third;
    out_stream << "The sum of the first 3\n"
                     << "numbers in the infile.dat\n"
                     << "is " << (first + second + third)
                     << endl;
    return 0;

I copied this exactly as is (with out the documentation) in order to test what I am doing wrong. The input is supose to come from the infile.dat file (and I made a notepad and named it infile.dat and gave it the numbers the BOOK says it is supose to have(1, 2, 3, 4, one on each line)) and I made the outfile.dat file. Left it blank like the program sample program has it. This program is supose to take from infile.dat the first three numbers and sum them up and write it to the outfile.dat file! Still NO luck! I think the problem I am having is how to make these files I can read and write too... I copied this code stright from the book and the only thing I did was try to make the file it reads from and the file it writes too. Sorry for being such a pain ;)

I had to skip ahead 300 pages just to read up on what an atoi is...

In Topic: Confused as to how to make a .dat file? (To test ifstream/ofstream)

24 November 2010 - 12:40 AM

I want to read from a file then output the results on the screen. The problem is I dont know how to make these files that I can read from. I tried to make a notepad document and save it as .dat and I was able to read from this file, but instead of outputting the 4 numbers 1, 2, 3 and 4 it output some random (or so It seems) result of something like "4818626491". My book just tells me to get the files I require from the teacher (just basic files with basic information, IE numbers and such) but I am teaching my self programing so I need to figure out how to make these files my self. Any ideas?

In Topic: Confused as to how to make a .dat file? (To test ifstream/ofstream)

23 November 2010 - 11:23 PM

I thought I would give your program a try even though I haven't gotten to the stdlib include directive. Still comes up with random numbers. I have tried many combinations to make the .dat file.

Like this one above, with a number on each line

or like this one below, with all the numbers on one line seperated by a comma
1, 2, 3, 4

Not sure if I am doing that right.

In Topic: New to "call by reference" and "void functions", few errors i...

01 November 2010 - 10:52 PM

//program calculates military time to standard time
#include <iostream>
//function declarations
double input(int& hour, int& minute);
//function to gather the military time
double calculation(int hour_calc, int minute_calc, char& merid);
//function to calculate the military time
void output(int hour_out, int minute_out);
//function to output the results
void initialize();
//function to initialize the screen

//main program
int main()
    char loop;
    using namespace std;


        int hours, minutes;
        char meridi;

        [COLOR="red"]input(hours, minutes);
        calculation(hours, minutes, meridi);
        output(hours, minutes)[/COLOR];

    }while (loop == 'Y' || loop == 'y');
    cout << "Good bye";
    void initialize();
    return 0;
//function initializes screen
void initialize()
    using namespace std;
    cout << endl;
//function gathers nessasary input from user
double input(int& hour, int& minute)

    using namespace std;
    cout << "Please enter the military time, the two first digits: ";
    cin >> hour;
    cout << "Please enter the last two digits of the military time: ";
    cin >> minute;

//function calculates the data gathered from input and monitors weather its AM or PM with "char merid"
double calculation(int hour_calc, int minute_calc, char& merid)

    using namespace std;
    [COLOR="red"]char PM, AM;[/COLOR]

    if(hour_calc > 11)

    hour_calc = hour_calc - 12;
    [COLOR="red"]merid = PM;[/COLOR]

    [COLOR="red"]merid = AM;[/COLOR]
//function outputs results of the calculation
void output(int hour_out, int minute_out)
    using namespace std;

    cout << "Your time in standard is: " << hour_out << ":" << minute_out << endl;


Thanks! That works! Unfortunitly I have a logic error, but I think I can manage that :)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download