I need helping mainly on reading and writing to a file based on an array or arra
ID: 3877052 • Letter: I
Question
I need helping mainly on reading and writing to a file based on an array or arrays keeping records of first name, last name, and score. You play a number guessing game which the program generates randomly between 1 and 100 and depending on the number of guesses the user takes, if its higher than the scores before, it stores your name and score to the 'array' in the file and keeps it for the next game. I'm having a lot of trouble making this function to work. Please help.. The following is my code to give you more or less of a view of where I'm stuck on. Thank you. Will rate as well! :)
/*****************************************IN C++******************************************************************/
//records.h
#ifndef records_h
#define records_h
using namespace std;
class records
{
private:
public:
string name;
int score;
//default constructor
records()
{
name = "Blank";
score = 0;
}
//parameterized constructor
records(string n, int s)
{
name = n;
score = s;
}
};
*/
/***********************************************************************************************************/
//main.cpp
#include <iostream>
#include <stdlib.h>
//#include "records.h"
using namespace std;
//should i read or write first?
//When is it accurate to file.close();
//should a void function be better?
//for loop?
#include <fstream>
int main() {
cout<<"Welcome to the Guessing Game! The top 10 best scores are:"<<endl;
ifstream file("Records.txt");
string firstName[10];
string lastName[10];
//int score[10];
if(file.is_open())
{
for(int i = 0; i < 10; i++)
{
file >> firstName[i];
}
for(int j = 10; j < 20; j++)
{
file >> lastName[j];
}
}
for(int i=0;i<10;++i)
{
cout<< "First Name is: " << firstName[i] << endl;
}
for(int j=10;j<20;++j)
{
cout<< "Last Name is: " << lastName[j] << endl;
}
/*
cout<<"Think you’ve got what it takes to make the top ten? Let’s find out!"<<endl;
cout<<"What is your name?"<<endl;
string name;
getline(cin,name);
int num_tries=0;
int guessedNumber;
srand (time(NULL));
int randomNumber=rand() % 100 + 1;
do
{
cout << "Guess a Number between 1 and 100: "<<endl;
cin >> guessedNumber;
num_tries++;
if (guessedNumber > randomNumber)
{
cout << "Too high!! ";
}
else if (guessedNumber < randomNumber)
{
cout << "Too low!! ";
}
else
{
cout << " You Win!!"<<endl;
cout<<"It took you " << num_tries << " guesses "<<name<<"!"<<endl;
}
}while(guessedNumber != randomNumber);
if (-1<num_tries && num_tries < 5)
{
cout<<"Amazing! Or was it luck?"<<endl;
}
else if(4<num_tries && num_tries < 7)
{
cout<<"That's a very good score..."<<endl;
}
else if(6<num_tries && num_tries < 9)
{
cout<<"That's pretty good but you can do better..."<<endl;
}
else if(8<num_tries && num_tries < 11)
{
cout<<"Not too shabby, but not too good either..."<<endl;
}
else{
cout<<"What a terrible score!..."<<endl;
}*/
return 0;
}
/***********************************************************************************************************/
//records.txt
Ed Sheeran 4
Camilla Cabello 4
G Easy 5
Robbie Schweller 6
Lil Pump 7
Post Malone 7
Cardi B 9
Bebe Rexha 10
Dua Lipa 12
Portugal Theman 20
//Feel free to rearrange the order if easier to sort, I've attempted this method as well but no luck
//As in First 10 lines the first name, followed by the last.. thanks again. Shouldn't be too difficult for a fluent programmer in C++
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
int main(void) {
srand(time(NULL)); // To not have the same numbers over and over again.
while(true) { // Main loop.
// Initialize and allocate.
int number = rand() % 99 + 2; // System number is stored in here.
int guess; // User guess is stored in here.
int tries = 0; // Number of tries is stored here.
char answer; // User answer to question is stored here.
//std::cout << number << " "; // Was used for debug...
while(true) { // Get user number loop.
// Get number.
std::cout << "Enter a number between 1 and 100 (" << 20 - tries << " tries left): ";
std::cin >> guess;
std::cin.ignore();
// Check is tries are taken up.
if(tries >= 20) {
break;
}
// Check number.
if(guess > number) {
std::cout << "Too high! Try again. ";
} else if(guess < number) {
std::cout << "Too low! Try again. ";
} else {
break;
}
// If not number, increment tries.
tries++;
}
// Check for tries.
if(tries >= 20) {
std::cout << "You ran out of tries! ";
} else {
// Or, user won.
std::cout<<"Congratulations!! " << std::endl;
std::cout<<"You got the right number in " << tries << " tries! ";
}
while(true) { // Loop to ask user is he/she would like to play again.
// Get user response.
std::cout << "Would you like to play again (Y/N)? ";
std::cin >> answer;
std::cin.ignore();
// Check if proper response.
if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
} else {
std::cout << "Please enter 'Y' or 'N'... ";
}
}
// Check user's input and run again or exit;
if(answer == 'n' || answer == 'N') {
std::cout << "Thank you for playing!";
break;
} else {
std::cout << " ";
}
}
// Safely exit.
std::cout << " Enter anything to exit. . . ";
std::cin.ignore();
return 0;
}
Edit & Run