kening 0 Report post Posted April 24, 2008 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 88Ahmed 70 87 66Amy 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 90Somnuk 78 78 87 Radha 65 67 87Toufik 90 98 89Eugene 34 67 99 Ezmir 76 85 45Helmi 80 78 77Ibrahim 78 86 56 Indra 78 87 65 Ismail 88 89 99Kannan 54 56 76 Kelvin 77 67 87 KCKhor 55 65 75 CCKiu 66 77 88CSLee 88 77 99KCLee 99 87 67 SLLim 88 77 88Majdi 67 76 88Manoj 90 56 76 Safi 76 67 76Michael 56 65 76Nazri 89 89 90NgHu 78 87 98Azyyati 87 78 67 Pee 65 77 88 Quote Share this post Link to post Share on other sites
mchammer 0 Report post Posted April 25, 2008 Apa fungsi program ni? Quote Share this post Link to post Share on other sites
betik 11 Report post Posted April 27, 2008 tolong ar.. x bleh run aku pun x tauu nape..#include <string> 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); Quote Share this post Link to post Share on other sites