Jump to content
Sign in to follow this  
kening

Fstream Dgn Cstdlib ?

Recommended Posts

tolong ar.. x bleh run aku pun x tauu nape.. pastu mcm ne nak link student.txt tu dgn fail cpp ni??

ni coding:

#include <iostream>

#include <cstdlib>

#include <fstream>

using namespace std;

//Dear Students: Do not delete the line below

ifstream input;

const string filename="student.txt";

//-----------------------------------------------------------------------------

//dear students : please accept this function and do not modify

//-----------------------------------------------------------------------------

void openFile(void)

{

input.clear();

input.open(filename.c_str(),ios::in);

if (!input)

cout << "error open file " << endl;

}

//-----------------------------------------------------------------------------

//dear students : please accept this function and do not modify

//-----------------------------------------------------------------------------

void closeFile()

{

input.close();

}

//-----------------------------------------------------------------------------

// Press any key to continue then clears the screen

//-----------------------------------------------------------------------------

void PressAnyKeyToContinue(void)

{

system("pause"); //pauses the system until you press a key

system("cls"); //clears the screen

}

//-----------------------------------------------------------------------------

// Display A basic report Header

//-----------------------------------------------------------------------------

void DisplayHeader(void)

{

cout << "Name\tCP1\tMath\tEnglish" <<endl;

cout << "---------------------------------------------" <<endl;

}

//-----------------------------------------------------------------------------

// Example view the contents of a file

//-----------------------------------------------------------------------------

void ViewAllInformation()

{

int m1,m2,m3;

string name;

openFile(); // must open the file before use

DisplayHeader();

while (!input.eof()) // stop if End Of File is reached ?

{

input >> name >> m1 >> m2 >> m3; // reads a line from the file

if (!input.fail()) // checks if the last line is read

{

cout << name << "\t"<< m1 << "\t"<< m2 << "\t"

<< m3<< endl;

}

}

closeFile(); // must close the file

cout << endl << endl; // create 2 emty lines after display

}

//------------------------------------------------------------------------------

// A menu item 2 sample module showing how to implement menu items

// as functions

//------------------------------------------------------------------------------

void TestOption(void)

{

cout << "Display a submenu or display Information"

<< endl<< endl;

}

//-----------------------------------------------------------------------------

// A function that displays a menu and returns the option selected as an integer

// number

//-----------------------------------------------------------------------------

int MainMenu (void)

{

int choice=0;

bool gotInput = false;

cout << "=====================================" << endl;

cout << " Main Menu" << endl;

cout << "=====================================" << endl;

cout << "1. View all information" << endl;

cout << "2. Statistics By Subject " << endl;

cout << "3. Overall Statistics" << endl;

cout << "4. Exit" << endl;

cout << "=====================================" << endl;

do { //Do not modify this loop

cout << "Please input your choice:"; //an accept it as it is

if(!(cin >> choice)) //loop to make sure

{ //that only valid options

cin.clear(); //are entered

cin.ignore(10000,'\n');

gotInput = true;

}

if (choice >= 1 && choice<=9) gotInput = true;

} while(!gotInput);

return choice; //return the selected choice

}

//-----------------------------------------------------------------------------

// The main function where execution starts

//-----------------------------------------------------------------------------

int main()

{

int choice;

bool flag = false;

int m1,m2,m3;

string name;

//

//This loop will interpret the menu selection and executes

//the correct module (function). It will terminate if the

//the selection is 4

//

do{

choice = MainMenu(); //get the choice

system("cls");

if (choice == 1) ViewAllInformation();

else if (choice == 2) TestOption();

else if (choice == 3) TestOption();

else if (choice == 4) flag = true;

else cout << "use only number shown in the menu" << endl;

if(!flag) PressAnyKeyToContinue();

}while (!flag);

return 0;

}

fail student.txt die mcm ni:

fcuk 90 90 88

Ahmed 70 87 66

Amy 45 76 44

Asaad 90 65 44

Belal 87 67 98

Celia 88 77 87

Dahlila 66 65 56

Jacob 70 87 67

Simon 54 45 90

Somnuk 78 78 87

Radha 65 67 87

Toufik 90 98 89

Eugene 34 67 99

Ezmir 76 85 45

Helmi 80 78 77

Ibrahim 78 86 56

Indra 78 87 65

Ismail 88 89 99

Kannan 54 56 76

Kelvin 77 67 87

KCKhor 55 65 75

CCKiu 66 77 88

CSLee 88 77 99

KCLee 99 87 67

SLLim 88 77 88

Majdi 67 76 88

Manoj 90 56 76

Safi 76 67 76

Michael 56 65 76

Nazri 89 89 90

NgHu 78 87 98

Azyyati 87 78 67

Pee 65 77 88

Share this post


Link to post
Share on other sites

tolong ar.. x bleh run aku pun x tauu nape..

#include <string> :ph34r:

pastu mcm ne nak link student.txt tu dgn fail cpp ni??
apa maksud nk link kan file tu?...kalu ko letakkan text file tu kat .cpp ko, so masa nk bukak file tu, filename ko cuma perlukan

const string filename="student.txt";

////
input.open(filename.c_str(),ios::in);
tp kalu letak kat lain, assignkan 'filename' tu dgn full path..cth:
const string filename="C:\\folder\\folderLagi\\student.txt";

////
input.open(filename.c_str(),ios::in);

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...