   High score system
  I am tring to make a highscore system. I am testing the saving procces in a controlled environment. I have already put 99 into the file "1.txt". After i run the program, the file contains nothing. I have tried using strings and integers.what aam i doing wrong#include<fstream.h>#include <iostream.h>#include <stdlib.h>    int main()    {   char a;   a=1030;   char str;   fstream file_in("C:\\1.txt",ios::in);   fstream file_op("C:\\1.txt",ios::out);   while(file_op >> str);   cout << str<<"\n";    if (a>str);    {    file_op<<a;    }    cout<<"\n";   file_op.close();   file_in.close();   system("PAUSE");   return 0;    }   
Quote:while(file_op >> str);  cout << str << "\n";  if (a > str);  { file_op << a;  }file_op >> strfile_op << a;...must be something wrong here.What is the program trying to do?   ;  I got this code off the internet tutorial on io minipulation. http://www.codersource.net/cpp_file_io.htmlchar str[2000];   fstream file_op("c:\\test_file.txt",ios::in);   while(file_op >> str)   cout << str ;I cheacked this and it works, it reads it and displays it.fstream file_op("c:\\CoderSource_file.txt",ios::out);   file_op<<"Test Write to file";this opens the file and writes code to it.I modified these to piece so that i could get the score from the page and then see if the new score was better. Thwn put the new score into the file.   ;#include<fstream>#include <iostream>#include <cstdlib>using namespace std;int main(){    int newscore=1050;    int oldscore=1030;    ifstream file_in("1.txt", ios::binary);    while(!file_in.eof())    {   file_in >> oldscore;   cout << oldscore << "\n";    }    file_in.close();    if(newscore > oldscore)    {   ofstream file_op("1.txt", ios::binary);   file_op << newscore;   file_op.close();    }    system("PAUSE");    return 0;}Here is one way to do it. (Be aware that I have made some changes!)Im using ifstream/ofstream rather than just fstream.[Edited by - pulpfist on December 16, 2005 11:49:20 AM];  thanks i can implement it into my game now;  I changed the code slightly and it still doesnt work.I alwasy displays the old score as 575, and it never saves the new number in it#include<fstream>#include <iostream>#include <cstdlib>#include <stdlib.h>using namespace std;int main(){    int newscore=4000;    int oldscore;    ifstream file_in("1.txt", ios::binary);    while(!file_in.eof())    {   file_in >> oldscore;   cout << oldscore << "\n";    }    file_in.close();    if(newscore > oldscore);    {   cout<<"new high score"<<newscore<<"\n";   ofstream file_op("1.txt", ios::binary);   file_op << newscore;   file_op.close();    }    system("PAUSE");    return 0;}   ;  Hey!Look at the if statementif(newscore > oldscore);Remove that semicolon, and see if that helps.The compiler will think that the if is over, as soon as it finds the semicolon.It is possible to create a empty if clause:if(a > b)  ;Here, the semicolon works as an empty statement. In other words, the compiler think that the if clause contains only an empty statement.So the {cout<<"new high score"<<newscore<<"\n";ofstream file_op("1.txt", ios::binary);file_op << newscore;file_op.close();}part is considered to be outside the if clause, and will be executed every time.I had this error in my own example too, but I removed it (A bit too late it seems)=)   ;Quote:Original post by eXtremeMachineI am tring to make a highscore system. I am testing the saving procces in a controlled environment. I have already put 99 into the file "1.txt". After i run the program, the file contains nothing. I have tried using strings and integers.what aam i doing wrongPlenty of things - please see comments embedded in the corrected code below.// Please use   or source tags to hold source// code snippets (depending on length).#include <fstream>#include <iostream>// #include <cstdlib> <-- we don't actually need this one.// Those are the proper names for those headers. Throw your reference book -// er, internet bookmark? - away; it is very out of date.using namespace std; // The new headers place type definitions and object// instances into the std namespace. This declaration tells the compiler// "If you can't find the declaration for something, try looking in std::".// That will prevent errors for type declarations like "fstream" and global// objects like "cout".int main() {  // "char" type variables represent single bytes of data. That simply cannot  // properly hold a value like "1030"; it holds a number, not text, and that  // number ranges up to 255 at most (since it is a single byte).  // "char[]" is an array of characters, which can be *used to represent*  // text in a primitive way, but it is not really a string type. The  // library provides std::string for that purpose, in the <string> header.  // However, integers are sufficient for our purposes.  int testScore = 1030;  // When you know the initial value of a variable, you should initialize it  // to that value.  int fileScore; // give your variables meaningful names when you can.  ifstream file_in("C:/1.txt");  // By specifying that we want an ifstream object, opening for input is  // implicit. It also provides "better documentation" by being more specific   // about our intent.  // Use forward slash as a path separator in file names, even on systems  // where that is not the system path separator. For details see:  // http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.16  // "Of course you could use "\\version\\next\\alpha\\beta\\test.dat", but   // that might hurt you (there's a non-zero chance you'll forget one of the   // "\"s, a rather subtle bug since most people don't notice it) and it can't   // help you (there's no benefit for using "\\" over "/"). Besides "/" is more   // portable since it works on all flavors of Unix, Plan 9, Inferno, all   // Windows, OS/2, etc., but "\\" works only on a subset of that list. So "\\"   // costs you something and gains you nothing: use "/" instead.// Don't try to open two streams for the file at the same time; that's asking  // for trouble. We will read with the input stream, close the file, and then  // possibly write via an output stream.  // The old while loop wasn't working right because of the semicolon at the  // end of the line. It was interpreted as "while you can read into the  // variable, do nothing else". Since you had a variable of char type, this  // would just end up putting the last byte of the file into that variable.  // Anyway, we only want to read one number, so the while loop is uncalled for  // anyway.  file_in >> fileScore;  cout << fileScore << endl; // you should use std::endl with std::cout to  // make sure that things actually get displayed. std::endl means "append a  // newline ('\n') to the buffer, and then show anything that's still in the  // buffer". The stream automatically uses this buffering as a speed  // optimization.  if (fileScore < testScore) {// you had a semicolon there as well which meant that the stuff you meant// to be inside the if-condition would always be executed: "if this is true,// do nothing; now do this". It is legal in C++ to just wrap a few// statements in {} without any if/while/for/etc. ; it just creates a// separate local scope for variables.    // If the file score was less than the test score, then we want to write    // the test score to the file. So:    file_in.close();    ofstream file_out("C:/1.txt");    file_out << testScore;  }  // At end of scope, ifstream and ofstream objects are automatically .close()d  // by the object destructors. (There is no problem if the file is closed  // multiple times; .close() is written to make that safe. The destructor  // simply calls .close(), and then the system frees up the object memory.)  // You only need or want to use .close() directly if you have to close a file   // in the middle of that object's scope (like with the input stream in our  // case).  // You should also not pause your programs artificially at the end with a  // system call; it is hackish and interferes with attempts to hook up your  // program to others with scripts. Instead, write a script (batch file on  // Windows; .sh or similar on Unix-like systems) to run your program and then  // the pause program.  // You also do not need to put an explicit return 0; at the end of main();  // C++ will do this for you as a special case for the main function (this is  // in the standard).};  thanks alot. I was wondering how ot attack code.In very c++ tutorial they had a return o in it.why is it needed in the first place. I dont understand why i shouldnt pause the program manully, and how would a create a batch file to pause if for me.;Quote:In very c++ tutorial they had a return o in it.why is it needed in the first place.You mean the "return 0" at the end of main?If you dont type "return 0" at the end of main, c++ will assume you did.Smarter things could be said about this, but to give you a hint;The value returned by main() is "catched" by the OS.Lets say you have a program that dont write to the console (like a server) so you dont know if it was closed by an error or by reaching the end of main.To check this you could type the following command in the same console you used to execute the program.> echo $?(This command shows how to do it on a UNIX system, im not sure what the equivalent would be on the Win32 console)If the program exited due to a "return 0" statement, this would print> 0on your console.This value would be of interrest (obviously) if you where writing a batch scriptthat need to make sure a command was successful before it proceeds with the next.