Writing a simple program???

I'm taking an introduction class in C++ and need a little help with an assignment. I need to write a simple program for the following:

Write a complete program that reads from a file. The file is called email.txt and can be found on the c drive under a folder called sentemails. The file can contain only a single entry about a sent email of the following format: email address, date sent, message. Display all three pieces of data to the user.

This is what I've written, but it does not show me the complete information from the file, which I made up and saved(which is just: email address, February 24, 2010, and some message content). Any help would be greatly appreciated!!!

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
ifstream inFile;

string message, date;
char email[30];

inFile.open("C:\\sentemail\\email.txt");

inFile >> email;
cout << email << endl;
inFile >> date;
cout << date << endl;
inFile >> message;
getline(inFile, message);
cout << message << endl;


system("PAUSE");
return EXIT_SUCCESS;
}
Last edited on
1. Please use code tags.

2. Don't use 'system("pause");'. Ever. (http://cplusplus.com/forum/articles/11153/)
The inFile >> will stop at whitespace.
Can you display the content of email.txt,how exactly you are putting data into this file and how you want to see as a output.
http://cplusplus.com/reference/iostream/istream/getline/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() {

    ifstream inFile;

    string message,;
    char email[64], date[32];

    inFile.open("C:\\sentemail\\email.txt");

    inFile.getline(email, 256, '|');
    cout << email << endl;

    inFile.getline(date, 256, '|');
    cout << date << endl;

    getline(inFile, message);
    cout << message << endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
email.txt

blackcoder41@yahoo.com|feb 25, 2010|hello world


note: the date contains a comma so a pipe is use as delimiter instead
Last edited on

See the side bar and click "Information" then type "code tag" [without double quotes] into the search bar.
It's a simple matter to use code tags.
Topic archived. No new replies allowed.