Click here to Skip to main content
14,487,207 members
Rate this:
Please Sign up or sign in to vote.
See more:
i would like to store the result of the program in a file. But i do not want to overwrite
the files anytime it is executed
#include <iostream>
#include <stdlib.h>
#include <string>
#include <conio.h>
#include <fstream>

using namespace std;

class contest 
{

private:
    double index;
    float x;
    char name[50];
    char s[100],ans;

public:
    contest()
    { x=0; }
    void init();
    void define();
    void display();
    void clrscr();
   };
void  contest :: define()
{
    ifstream in;	   
    char a;
    int i=0;
   
    in.open("questions.txt"); //opening file
    while(in.eof()==0)
    {
        in.getline(s, 150); //number of lines to read
        cout<<endl<<s;
        if(i==5) //if number of lines is equal to five skip next line
        {
            in.get(a); //inputting answer
            cout<<"\nans:";
            cin>>ans;// input answer
            i=-1; //display question by questions
            if(ans==a)// if answer inputted is equal to value of a
            {
                x=x+5;//add plus  5 
                cout<<" *Correct.\n";
            }
            else
            {
                cout<<" #Wrong.\n";
            }
        }
        i++;
    }
    in.close();//close file
}
void contest :: init() //input students name and index number
{
    cout<<"Sur Name:";
    cin>>name;
    cout<<"Index Number:";
    cin>>index;
}
void contest :: display()
{
    cout<<"\n **Result:\n";  //display student's name and result
    cout<<"\t"<<name<<" you got total "<<x<<" marks, out of 50.\n";
}

int main()
{
    //clrscr();
    contest r;
    cout<<"      \t\t\t		QUIZ 	\t      Total Marks:50\n\n";
    r.init();
    cout<<" "<<endl;
    cout<<"\t 5 Marks Per Questions";
L:
    cout<<"\n\n\tSELECT YOUR CHOICE:\n"
        <<"\n\t*To start Quiz 		Press 1"
        <<"\n\t*To quit		Press 0	:";
    int c;
    cin>>c;
    switch(c)
    {
    case 1:
        r.define();
        r.display();
        break;
    case 0:
        exit(1);
    default:
        cout<<"invalid choice!";
        goto L;
    }
    getch();
}

[EDIT: Code formatting and indentation]
Posted
Updated 16-Dec-13 5:08am
v3
Comments
Richard MacCutchan 16-Dec-13 11:31am
   
Open the file with the append option, then all data written to it will be after any existing content.
[no name] 16-Dec-13 11:41am
   
Or then the brute force method: Get a unique file name from the OS an write your result to it. The "ugly" thing then only is to get the latest file. Not realy ugly but a little bit work and hopefully timestamps on the files are correct.
H.Brydon 16-Dec-13 22:11pm
   
[without checking] Google will probably tell you how to do this if you search for "open file append"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100