i want to make a high score list for my game, but i can't figure out how to read and write integers from a file.
i want to make a high score list for my game, but i can't figure out how to read and write integers from a file.
Ugh.. read the FAQ/Tutorials
http://www.cprogramming.com/tutorial/lesson10.html
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
Maybe not the best way, but it works for me
Code:#include <iostream> #include <fstream.h> class player { public: player(){}//: //health (10), //speed (50){} friend istream& operator >>(istream& stats, player&p) { return stats>>p.health>>p.speed; //this here is overloaded so you can enter both a health and a speed //for the player for example when the computer asks you for an input //cin>>first.........then you input 10 (Enter) 50 } void SetHealth(int health){health=health;} int const GetHealth() {return health;} int health; int speed; }; int main() { player first;//looks like I would have to create a class //if I wanted to load the stats for a certain player I //would have to create a player class......... cout<<"Enter the Player's Health and speed separated by pressing Enter"<<endl; cin>>first; ofstream saveplayer ("playerstats.txt", ios::app); saveplayer<<first.health<<"\n"<<endl; saveplayer<<first.speed<<"\n"<<endl; saveplayer.close(); ifstream read ("playerstats.txt"); cout<<"Now I shall display the stats"<<endl; read>>first.health;//boots up the stats.......kind of read>>first.speed; cout<<first.health<<endl;//displays it on the screen cout<<first.speed; read.close(); int x;//this is when I used DevC++ cin>>x; //this is also some old stuff I kind of reworked some time ago. return 0; }
Last edited by incognito; 05-21-2002 at 04:48 PM.
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
http://www.cplusplus.com/doc/tutorial/tut6-1.html
well I hope that gets you started.
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
Try this.
Code:#include <iostream> #include <fstream> using namespace std; int main(void) { int scores[10] = {30000,29000,25000,20000,10000,5000,4000,3000,200,100}; int i, score; char filename[80] = "scores.txt"; //Write high score file ofstream out(filename); if (!out) { cout << "Unable to open file:" << filename << endl; return 1; } for (i=0; i<10; i++) out << scores[i] << " "; out << endl; out.close(); //Read high score file ifstream in(filename); if (!out) { cout << "Unable to open file:" << filename << endl; return 1; } cout << "***** Top Ten High Scores *****" << endl; for (i=0; i<10; i++) { in >> score; cout.width(5); cout << i+1 << ": "; cout.width(10); cout << score << endl; } in.close(); return 0; }
thanks for the help