Thread: Please help me with my program!!

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Exclamation Please help me with my program!!

    Hey there! I'm doing this 10 question quiz where I have to keep track of the score and then if the user scored the highest I have to ask for his name and save his name and score to a textfile called hscore.txt where the highest score is at the top. It's sort of like a video game in an arcade. Anyway, I can get the quiz part and how to keep track, but how do I save his name and score to a textfile with the highest score on the top? If someone could help me that would be great!! Thanks in advance.

    xoxo, Sabrina

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    To use files, include <fstream>. The class you need for output is std:fstream. Your book/compiler/google has answers and examples on how to use it. For keeping the highest score on top, read in all the values in the old high scores, sort with the new one, and output.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    Thanks for your help. Another question...how would I read all the old high scores? I understand how to do the other two.

    xoxo, Sabrina

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    well store the scores one under the other in the text file.. read them and sdisplay them on the screen as simple as that.. You know how to read a file so read the content and display them..

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Sorry if this isnt what u need but i've been up all night and i havent used C++ in 2 weeks. Its sketchy but i could improve/comment/edit it if u need. I was trying not to make it exact to what u need just give u a place to start and something to look at.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	int score[10],
    		largest;
    	char name[20];
    
    	ofstream	outfile;
    	ifstream	infile;
    
    	largest = score[0];
    
    	for (int i = 0; i < 10; i++)
    	{
    		cout << "Enter score: ";
    		cin >> score[i];
    
    		if (largest < score[i])
    		{
    			largest = score[i];
    		}
    	}
    
    	cout << "Largest score was " << largest << endl;
    	cout << "Enter name for score: ";
    	cin >> name;
    
    	outfile.open("c:\\hscore.txt");
    
    	outfile << largest << " " << name << endl;
    
    	outfile.close();
    
    	cout << "High Score and Holder: " << endl;
    	cout << "\n";
    	cout << largest << "   " << name << endl;
    	cout << "\nThis information has been saved to file!" << endl;
    
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    One thing i would change in that code would be asking for the name of the highest score. Dont use cin becuase if he enters two names it wont be displayed.
    Instead of
    Code:
    cin >> name;
    use this
    Code:
    getline (cin , name);
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    based on the original post i see no circumstance within this particular instance in which more than one name would be entered, or i would not have used cin.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    Thanks for the replies and help. LOL, I might be back...

    xoxo, Sabrina

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM