Witam,
Jak podmieniać zmienne zapisane w pliku *.txt? Chciałbym aby wykonywało to się automatycznie, gdy zdobędę nową ilość "pucharów" w mojej grze. A oto kod źródłowy: (chodzi mi o bibliotekę fstream)
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <windows.h>
#include <fstream>
using namespace std;
int liczba, strzal, proby=0, opcja, puchary=0, wyzwanie;
string imie;
int main()
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0009 );
cout << "Witaj! W grze! Wybierz co chcesz zrobic:" << endl;
cout << "Wersja gry: 1.2" << endl;
cout << "1.Graj! "<<endl;
cout << "2.Wyzwanie! "<<endl;
cout << "3.Sprawdz swoją liczbe pucharow! "<<endl;
cin >>opcja;
if(opcja == 3)
{
string linia;
int nr_linii=1;
fstream plik;
plik.open("wynik.txt", ios::in);
if(plik.good()==false) cout<<"Nie mozna otworzyc pliku!";
while (getline(plik, linia))
{
switch (nr_linii)
{
case 1: puchary=atoi(linia.c_str()); break;
}
nr_linii++;
}
plik.close();
plik<<puchary;
cout <<""<<endl;
cout <<""<<endl;
cout <<puchary<<endl;
cout <<""<<endl;
cout <<""<<endl;
}
if(opcja == 1)
{
srand(time(NULL));
liczba = rand()%(100+1);
while(strzal!=liczba)
{
proby++;
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0007 );
cout << "Zgadnij jaka to.(to Twoja "<<proby<<" proba.):";
cin >> strzal;
if(strzal==liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0003 );
cout << "Brawo! Zgadles w: "<<proby<<" probie"<<endl;
cout << "Wygrywasz: 25 pucharow!";
puchary = puchary + 25;
cout << "Twoja liczba pucharow: "<< puchary <<endl;
}
if(strzal<liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_RED );
cout << "Niestety podales za mala liczbe :("<<endl;
}
if(strzal>liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_GREEN );
cout << "Niestety podales za duza liczbe :("<<endl;
}
if(strzal>100)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_BLUE );
cout << "Podales wieksza liczbe niz jest w zakrasie! :("<<endl;
}
}
}
if(opcja==2)
{
cout << "Wybierz nr i poziom trudnosci wyzwania:"<<endl;
cout << "1.Latwy,zgadnij liczbe w mniej niz 25 rund"<<endl;
cout << "2.Sredni,zgadnij liczbe w mniej niz 15 rund"<<endl;
cout << "3.Trudny,zgadnij liczbe w mniej niz 10 rund"<<endl;
cin >>wyzwanie;
if(wyzwanie==2)
srand(time(NULL));
liczba = rand()%(100+1);
while(strzal!=liczba && proby<15)
{
proby++;
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0007 );
cout << "Zgadnij jaka to liczba.(W mniej niz 15 rund)(to Twoja "<<proby<<" proba.):";
cin >> strzal;
if(proby>23)
{
cout << ""<<endl;
cout << ""<<endl;
cout << "Przegrales!"<<endl;
cout << ""<<endl;
cout << ""<<endl;
cout << "Ta liczba to: "<< liczba;
}
if(strzal==liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0003 );
cout << "Brawo! Zgadles w: "<<proby<<" probie"<<endl;
cout << "Wygrywasz: 70 pucharow!";
puchary = puchary + 70;
cout << "Twoja liczba pucharow: "<< puchary <<endl;
}
if(strzal<liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_RED );
cout << "Niestety podales za mala liczbe :("<<endl;
}
if(strzal>liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_GREEN );
cout << "Niestety podales za duza liczbe :("<<endl;
}
if(strzal>100)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_BLUE );
cout << "Podales wieksza liczbe niz jest w zakrasie! :("<<endl;
}
}
}
if(wyzwanie==1)
{
srand(time(NULL));
liczba = rand()%(100+1);
while(strzal!=liczba && proby<24)
{
proby++;
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0007 );
cout << "Zgadnij jaka to liczba.(W mniej niz 25 rund)(to Twoja "<<proby<<" proba.):";
cin >> strzal;
if(proby>23)
{
cout << ""<<endl;
cout << ""<<endl;
cout << "Przegrales!"<<endl;
cout << ""<<endl;
cout << ""<<endl;
cout << "Ta liczba to: "<< liczba;
}
if(strzal==liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0003 );
cout << "Brawo! Zgadles w: "<<proby<<" probie"<<endl;
cout << "Wygrywasz: 70 pucharow!";
puchary = puchary + 70;
cout << "Twoja liczba pucharow: "<< puchary <<endl;
}
if(strzal<liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_RED );
cout << "Niestety podales za mala liczbe :("<<endl;
}
if(strzal>liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_GREEN );
cout << "Niestety podales za duza liczbe :("<<endl;
}
if(strzal>100)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_BLUE );
cout << "Podales wieksza liczbe niz jest w zakrasie! :("<<endl;
}
}
}
while(puchary<=5000000000)
{
proby++; proby=0;
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0009 );
cout << "Witaj! W grze! Wybierz co chcesz zrobic:" << endl;
cout << "1.Graj! "<<endl;
cout << "2.Wyzwanie! "<<endl;
cout << "3.Sprawdz swoja liczbe pucharow"<<endl;
cout << "Twoja liczba pucharow: "<< puchary <<endl;
cin >>opcja;
if(opcja == 3)
{
string linia;
int nr_linii=1;
fstream plik;
plik.open("wynik.txt", ios::in);
if(plik.good()==false) cout<<"Nie mozna otworzyc pliku!";
while (getline(plik, linia))
{
switch (nr_linii)
{
case 1: puchary=atoi(linia.c_str()); break;
}
nr_linii++;
}
plik.close();
cout <<""<<endl;
cout <<""<<endl;
cout <<puchary<<endl;
cout <<""<<endl;
cout <<""<<endl;
}
if(opcja == 1)
{
srand(time(NULL));
liczba = rand()%(100+1);
while(strzal!=liczba)
{
proby++;
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0007 );
cout << "Zgadnij jaka to liczba.(to Twoja "<<proby<<" proba.):";
cin >> strzal;
if(strzal==liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0003 );
cout << "Brawo! Zgadles w: "<<proby<<" probie"<<endl;
cout << "Wygrywasz: 25 pucharow!";
puchary = puchary + 25;
cout << "Twoja liczba pucharow: "<< puchary <<endl;
}
if(strzal<liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_RED );
cout << "Niestety podales za mala liczbe :("<<endl;
}
if(strzal>liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_GREEN );
cout << "Niestety podales za duza liczbe :("<<endl;
}
if(strzal>100)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_BLUE );
cout << "Podales wieksza liczbe niz jest w zakrasie! :("<<endl;
}
}
}
if(opcja==2)
{
cout << "Wybierz nr i poziom trudnosci wyzwania:"<<endl;
cout << "1.Latwy,zgadnij liczbe w mniej niz 25 rund"<<endl;
cout << "2.Sredni,zgadnij liczbe w mniej niz 15 rund"<<endl;
cout << "3.Trudny,zgadnij liczbe w mniej niz 10 rund"<<endl;
cin >>wyzwanie;
if(wyzwanie==1)
{
srand(time(NULL));
liczba = rand()%(100+1);
while(strzal!=liczba && proby<24)
{
proby++;
cout << "Zgadnij jaka to liczba.(W mniej niz 25 rund)(to Twoja "<<proby<<" proba.):";
cin >> strzal;
if(strzal==liczba)
{
cout << "Brawo! Zgadles w: "<<proby<<" probie"<<endl;
cout << "Wygrywasz: 75 pucharow!";
puchary = puchary + 75;
cout << "Twoja liczba pucharow: "<< puchary <<endl;
}
if(strzal==liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0003 );
cout << "Brawo! Zgadles w: "<<proby<<" probie"<<endl;
cout << "Wygrywasz: 25 pucharow!";
puchary = puchary + 25;
cout << "Twoja liczba pucharow: "<< puchary <<endl;
}
if(strzal<liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_RED );
cout << "Niestety podales za mala liczbe :("<<endl;
}
if(strzal>liczba)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_GREEN );
cout << "Niestety podales za duza liczbe :("<<endl;
}
if(proby>23)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_BLUE );
cout << "Przegrales!"<<endl;
}
}
}
}
}
return 0;
}
W pliku txt zapisane jest: 0. (jako liczba pucharów na początku gry) (chodzi mi o coś w stylu "Zapisania stanu gry")
Z góry dziękuję.