Why doesn't the highscore function work? It always accepts your name even if it is lower....And How do I make it so that high[4] and high [5] together equals the high score? I tried high[4,5] along with other things..,Thanks!
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int number;
int percent = 10;
int fav = 1 + (rand()%percent);
int again;
int input;
int turns = 0;
int rounds = 0;
char high[7];
int highscorenumber;
char initials[3];
ifstream readscore ("high.txt");
readscore.getline(high,7);
<<high[4] high[5]<< = highscorenumber;
cout<<"See if you can guess the number! You get 6 turns per round!\n Every round you complete it gets tougher!";
cin.get();
system("cls");
do{ //Main Game Loop
cout<<"\n\nEnter a number!!!";
cin>> number; //Gets number
cin.ignore();
while (number != fav){//Do while the number isn't equal to Favorite
if (number < fav && turns < 6)
{
turns = turns + 1;
system("cls");
cout<<"| Turns || Rounds || Last Guess |";
cout<<"\n| "<<turns<<" || "<<rounds<<" || "<<number<<" |";
cout<<"\n---------------------------------";
cout<<"\n\nWRONG! Try guessing Higher\n";
cout<<"Enter New Number...\n";
cin>> number; //Tells wrongs and to guess a higher number
}
else if (number > fav && turns < 6) {
turns = turns + 1;
system("cls");
cout<<"| Turns || Rounds || Last Guess |";
cout<<"\n| "<<turns<<" || "<<rounds<<" || "<<number<<" |";
cout<<"\n---------------------------------";
cout<<"\n\nWRONG!!! Try guessing Lower...\n";
cout<<"Enter New Number...\n";
cin>> number;
}
else if (turns == 6) {
cout<<"WRONG!! Game Over!\n\nThe number was "<<fav<<"";
cout<<"\nYou played " <<rounds<< " rounds!\n";
if (highscorenumber < rounds) {
cout<<"You got A High Score!";
cout<<"Enter your Initials! Enter 3! If you have 2 just add a space to the end!";
cin>>initials;
ofstream highscore ("high.txt");
highscore << initials <<"-" <<rounds<< endl;
cout<<"Thanks For Playing!";
cin.get();
return 0;
}
else {
cin.get();
cin.get();
return 0;
}
}
}
rounds = rounds+1;
turns = turns - turns;
cout<<"CORRECT!!!\n";
cout<<"Do you want to go to the next round? 1 for Yes/2 for No\n";
cin>> input;
cin.ignore();
switch(input){
case 1:
percent= percent + 10;
fav = 1 + (rand()%percent);
again=1;
system("cls");
break;
default:
again=0;
cout<<"You played " <<rounds<< " rounds!\n";
if (highscorenumber < rounds) {
cout<<"You got A High Score!";
cout<<"Enter your Initials! Enter 3 Letters!\nIf you have 2 just add a space to the end!";
cin>>initials;
ofstream highscore ("high.txt");
highscore << initials <<"-" <<rounds<< endl;
cout<<"Thanks For Playing!";
cin.get();
return 0;
}
else {
cin.get();
cout<<"\nGame Over!";
cin.get();
return 0;
break;
}
}
}while (again == 1);
return 0;
}