Hello everyone,I have a question on file manipulation and would like to know how
could i create multiple files from ofstream. For example in the code below when option 4 is selected a file is created called "Progress.txt" but i want to know how to enable the program to create a new file like "Progress1.txt" instead of just overwriting the old existing one,kinda like a database but i do not want to rewrite the information in the old file instead i want a new file created with the new information stored in it (Keeping records).I do not know how to do that in code form.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
usingnamespace std;
int main ()
{
//variables
char Devotion = '\0';
int Devpoints = 0;
int Pushups = 0;
int Pushpoint=0;
int Situps = 0;
int Sitpoint = 0;
int Gym = 0;
int Gympoints = 0;
char Research = '\0';
int Researchpt = 0;
int Texts =0;
int Textspt = 0;
int Words = 0;
int Wordspt = 0;
int Puzzles = 0;
int Puzzlespt =0;
int Unblockme = 0;
int Unblockmept = 0;
int Sudoku = 0;
int Sudokupt = 0;
int Somethingnew = 0;
int Somethingnewpt=0;
int Bed = 0;
int Bedpt = 0;
int Book = 0;
int Bookpt = 0;
int RBook=0;
int Total=0;
char c='\0';
int Option=0;
string Name = "";
//string Last[14] = {};
//int Lresults[14] = {};
//int i = 0;
//Getting the user informatiions
cout<<"Please enter your name:"<<endl;
getline(cin,Name);
system("cls");
do {
cout<<"WELCOME "<<Name<<endl<<endl;
cout<<"Please Choose an option from the menu below"<<endl
<<"1. ENTER YOUR PROGRESS FOR THE WEEK."<<endl
<<"2. VIEW YOUR TOTAL SCORE FOR THE CURRENT WEEK."<<endl
<<"3. TO CLEAR THE SCREEN."<<endl
<<"4. TO SAVE YOUR CURRENT PROGRESS IN A FILE."<<endl
//<<"5. TO VIEW YOUR PROGRESS FROM LAST WEEK."<<endl
<<"Please choose and option:"<<endl;
cin>>Option;
switch (Option){
case 1:
cout<<"1) Have you done all your devotions for the week? y or n"<<endl;
cin>>Devotion;
cout<<"2) How many Pushups have you done for the week"<<endl;
cin>>Pushups;
cout<<"3) How many Situps have you done for the week"<<endl;
cin>>Situps;
cout<<"4) How many Gym days have you gone for the week"<<endl;
cin>>Gym;
cout<<"5) Did you do your research for the week? y or n"<<endl;
cin>>Research;
cout<<"6) How many Texts have you learnt for the week?"<<endl;
cin>>Texts;
cout<<"7) How many words have you learnt for the week?"<<endl;
cin>>Words;
cout<<"8) How many Puzzles have you done for the week?"<<endl;
cin>>Puzzles;
cout<<"9) How many unblockme's have you done for the week?"<<endl;
cin>>Unblockme;
cout<<"10) How many Sudoku Puzzles have you done for the week?"<<endl;
cin>>Sudoku;
cout<<"11) How many things did you learn this week?"<<endl;
cin>>Somethingnew;
cout<<"12) How many times did you make you Bed for this week?"<<endl;
cin>>Bed;
cout<<"13 )Did you read a book?\n(0)Did not read\n(1)Started\n(2)enter the amount of books you have read"<<endl;
cin>>Book;
if (Book==2){cout<<"14) How many Books have you read for this week?"<<endl;cin>>RBook;}
//calculatons
//calculations for devotions
if (Devotion=='y') {
Devpoints=6;
}
elseif (Devotion=='n'){
Devpoints=3;
}
//calculations for push ups
if (Pushups==450){
Pushpoint=3;
}
elseif (Pushups>450){
Pushpoint=((Pushups-450)/50)+3;
}
//calculatons for situps
if (Situps==500){
Sitpoint=3;
}
elseif (Pushups>500){
Sitpoint=((Situps-500)/50)+3;
}
//calculations for Gym
if (Gym==0){
Gympoints=0;
}
elseif (Gym>0){
Gympoints=Gym*5;
}
//Calculations for Research
if (Research=='y')
{
Researchpt = 5;
}
elseif (Research=='n')
{
Researchpt=0;
}
//Calculatuons for bible texts
if (Texts==0)
{
Textspt=0;
}
elseif (Texts>0)
{
Textspt=Texts*2;
}
//calculatons for words
if (Words==0)
{
Wordspt=0;
}
elseif (Words>0)
{
Wordspt=Words*2;
}
//calculatons for Puzzles
if (Puzzles==0)
{
Puzzlespt=0;
}
elseif (Puzzles>0)
{
Puzzlespt=Puzzles*2;
}
//calculatons for Unblockme
if (Unblockme==0)
{
Unblockmept=0;
}
elseif (Unblockme>0)
{
Unblockmept=Unblockme*2;
}
//calculatons for Sudoku
if (Sudoku==0)
{
Sudokupt=0;
}
elseif (Sudoku>0)
{
Sudokupt=Sudoku*2;
}
//calculatons for Something new
if (Somethingnew==0)
{
Somethingnewpt=0;
}
elseif (Somethingnew>0)
{
Somethingnewpt=Somethingnew*2;
}
//calculations for making bed
if (Bed==0)
{
Bedpt=0;
}
elseif (Bed>0)
{
Bedpt=Bed*2;
}
//Calculautons for reading books
if (Book==0)
{
Bookpt=0;
}
elseif (Book==1)
{
Bookpt=2;
}
elseif (RBook>0)
{
Bookpt=RBook*5;
}
Total=Devpoints+Pushpoint+Sitpoint+Gympoints+Researchpt+Textspt+Wordspt+Puzzlespt+Unblockmept+Sudokupt+Somethingnewpt+Bedpt+Bookpt;
break;
case 2 :
//output of the program
cout<<" YOUR SCORE FOR THIS WEEK :) "<<Name<<endl;
cout<<"Devotion: " <<Devpoints<<" Pts"<<endl;
cout<<"Push-ups: " <<Pushpoint<<" Pts"<<endl;
cout<<"Sit-ups: " <<Sitpoint<<" Pts"<<endl;
cout<<"Gym: " <<Gympoints<<" Pts"<<endl;
cout<<"Research: " <<Researchpt<<" Pts"<<endl;
cout<<"Texts: " <<Textspt<<" Pts"<<endl;
cout<<"Words: " <<Wordspt<<" Pts"<<endl;
cout<<"Puzzles: " <<Puzzlespt<<" Pts"<<endl;
cout<<"Unblockme: " <<Unblockmept<<" Pts"<<endl;
cout<<"Sudoku: " <<Sudokupt<<" Pts"<<endl;
cout<<"Something New: " <<Somethingnewpt<<" Pts"<<endl;
cout<<"Making Bed: " <<Bedpt<<" Pts"<<endl;
cout<<"Books: " <<Bookpt<<" Pts"<<endl;
cout<<"Total: " <<Total<<" Pts"<<endl;
break;
case 3:
system("cls");
break;
default :
cout<<"Thank you...."<<endl;
break;
}
if (Option==4){
ofstream Progress;
Progress.open ("Progress.txt");
Progress<<" YOUR SCORE FOR THIS WEEK :) "<<Name<<endl;
Progress<<"Devotion: " <<Devpoints<<" Pts"<<endl;
Progress<<"Push-ups: " <<Pushpoint<<" Pts"<<endl;
Progress<<"Sit-ups: " <<Sitpoint<<" Pts"<<endl;
Progress<<"Gym: " <<Gympoints<<" Pts"<<endl;
Progress<<"Research: " <<Researchpt<<" Pts"<<endl;
Progress<<"Texts: " <<Textspt<<" Pts"<<endl;
Progress<<"Words: " <<Wordspt<<" Pts"<<endl;
Progress<<"Puzzles: " <<Puzzlespt<<" Pts"<<endl;
Progress<<"Unblockme: " <<Unblockmept<<" Pts"<<endl;
Progress<<"Sudoku: " <<Sudokupt<<" Pts"<<endl;
Progress<<"Something New: " <<Somethingnewpt<<" Pts"<<endl;
Progress<<"Making Bed: " <<Bedpt<<" Pts"<<endl;
Progress<<"Books: " <<Bookpt<<" Pts"<<endl;
Progress<<"Total: " <<Total<<" Pts"<<endl;
Progress.close();
break;
}
cout<<"\n\nDo you want to continue y or n?:"<<endl;
cin>>c;
system("cls");//clear screen
}
while ((c=='y')||(c=='Y'));
system("pause");
return 0;
}
Thank you for responding . But instead overwriting i want it to create a new file everytime the option is choosen,so first time i run it i creates test.txt but the next time i run it i want it to create a new file called test1.txt and so on...