Click here to Skip to main content
14,487,702 members

Storing information result in a file without overwriting existing information

Arkorful Jnr asked:

Open original thread
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]
Tags: C++

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




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