Witam,
Mam problem ponieważ gdy w notatniku liczba pucharów zapisuje np. liczba 025 się to przy następnym uruchomieniu programu nie odczytuje się. Oto mój kod:
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <windows.h>
#include <string>
#include <stdio.h>
using namespace std;
int liczba, strzal, proby, opcja, puchary, wyzwanie, Puchary_z_Pliku, napis , wybor, nr_linii=1;
string imie;
void zapisz()
{
FILE *save=fopen("save.txt","w");
fprintf(save,"%03d", puchary);
fclose(save);
}
void wczytaj()
{
FILE *save=fopen("save.txt","r");
fprintf(save,"%03d", puchary);
fclose(save);
}
int main()
{
for(;;)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, 0x0009 );
wczytaj();
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 swoja liczbe pucharow! "<<endl;
cout << "4.Wyjdz z gry"<<endl;
cin >>opcja;
//napis = napis + puchary;
if(opcja==4)
{
cout << "Zapisywanie stanu gry...";
Sleep(500);
zapisz();
exit(0);
}
if(opcja == 3)
{
cout << endl;
cout << endl;
cout << endl;
cout << "Twoja liczba pucharow to:"<<puchary;
cout << 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;
//napis = napis + puchary;
cout << "Twoja liczba pucharow: "<<Puchary_z_Pliku + 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)
{
HANDLE hOut;
hOut = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute( hOut, FOREGROUND_BLUE );
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++;
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: 30 pucharow!";
puchary = puchary + 30;
//napis = napis + 30;
cout << "Twoja liczba pucharow: "<<Puchary_z_Pliku + 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(proby>23)
{
cout<<endl;
cout<<endl;
cout<< "Przegrales! W: "<<proby<<" probie. Twoja liczba to:"<<liczba<<endl;
cout<<endl;
cout<<endl;
}
}
}
if(wyzwanie==2)
{
srand(time(NULL));
liczba = rand()%(100+1);
while(strzal!=liczba && proby<14)
{
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: 50 pucharow!";
puchary = puchary + 50;
cout << "Twoja liczba pucharow: "<< Puchary_z_Pliku + 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(proby>13)
{
cout<<endl;
cout<<endl;
cout<< "Przegrales! W: "<<proby<<" probie. Twoja liczba to:"<<liczba<<endl;
cout<<endl;
cout<<endl;
}
}
}
if(wyzwanie==3)
{
srand(time(NULL));
liczba = rand()%(100+1);
while(strzal!=liczba && proby<9)
{
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: 60 pucharow!";
puchary = puchary + 60;
napis = napis + 60;
cout << "Twoja liczba pucharow: "<< Puchary_z_Pliku + 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(proby>8)
{
cout<<endl;
cout<<endl;
cout<< "Przegrales! W: "<<proby<<" probie. Twoja liczba to:"<<liczba<<endl;
cout<<endl;
cout<<endl;
}
}
}
}
}
return 0;
}