Function to generate a random number in a random range

Can someone please give me tips on how to accomplish generating a random number within a random range. See 'roll_dice'

#include <iostream>
#include <limits>
#include <ctime>
#include <iomanip>
#include <cstdlib>
using namespace std;
using std::cout;
using std::cin;


struct Sanity {

    unsigned int turns = std::numeric_limits<int>::max();
    unsigned int sanity = std::numeric_limits<int>::max();
    int randNum = 0;
    int choice = 0;
    int low = 0;
    int high = 0;

};

void game_greeting();
int roll_dice(Sanity& s1);
int even_odd(Sanity& s1);

int main()
{
    Sanity s1;

    game_greeting();
    roll_dice(s1);
    even_odd(s1);


}

int roll_dice(Sanity &s1)
{
    srand(time(NULL));    
    s1.randNum = rand() % roll_dice(s1) + roll_dice(s1);
    return s1.randNum;
}

Pointers & Ubuntu

Hello Dani Web,

I have written a program that needs to run on Ubuntu 14.0. It runs on Windows 10 and will compile in Replit. But I get this error in Replit. Can anyone figure out why? I assume it's in relation to my pointer "highScore". Can anyone figure out why. Also where is the most efficient place to delete "highScore."

repit_error.JPG

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

// My struct to control the game
struct Game {

    int lo = 10;
    int hi = 20;
    int seed = 3;
    int randNum = 0;
    int randNum2 = 0;
    int humanScore = 0;
    int computerScore = 0;
    int overallHumanScore = 0;
    int overallComputerScore = 0;
    bool humanTurn = true;
    bool gameOver = false;
    bool validChoice = false;
    string playerName;
    int choice = 0;
    int* highScore = 0;
    char yesno = ' ';

};

int D6(Game &game1);
int RandomNumber(Game &game1);
int ProtectRandomNumber(Game &game1);
int ProtectRandomNumber2(Game &game1);
int fixed_seed(Game &game1);
void get_name(Game &game1);
void program_greeting();
int get_game_choice(Game &game1);
int protect_choice(Game &game1);
void pig_game(Game &game1);
int high_score(Game &game1);


int main()
{
    Game game1;

    get_name(game1);
    program_greeting();
    cout << "C1 Spec: " << fixed_seed(game1) << endl;
    cout << "A2 Spec: " << RandomNumber(game1) << endl;
    cout << "A3 Spec: " << ProtectRandomNumber(game1) << endl;
    cout << "A4 Spec: " << ProtectRandomNumber2(game1) << endl;
    do {
        while (!game1.gameOver)
        {
            pig_game(game1);
        }

        cout << "B3 & B4 Spec  - High score: " << high_score(game1) << endl;
        cout << "Would you like to play again? Press Y to continue or any ";
        cout << "other key to quit: ";
        cin >> game1.yesno;
        if ((game1.yesno == 'y') || (game1.yesno == 'Y')) {
            game1.yesno == 'Y';
            game1.gameOver = false;
        }
        else {
            exit(0);
        }
        delete game1.highScore;
        cout << high_score(game1) << endl;
    } while (game1.yesno != 'Y');

    return 0;
}


// Spec A1 - random number generator & D6
int D6(Game &game1)
{
    srand(time(NULL));
    game1.randNum = rand() % 6 + 1;
    return game1.randNum;
}

// Spec A2 - random number generator between lo and high
int RandomNumber(Game &game1)
{

    srand(time(NULL));
    game1.randNum2 = rand() % (game1.hi - game1.lo) + game1.lo;
    return game1.randNum2;
}

// Spec A3 - random number generator between lo and high with error checking -1
int ProtectRandomNumber(Game &game1)
{
    game1.lo = 1;
    game1.hi = 100;

    if ((game1.lo >= game1.hi) || (game1.lo < 1) || (game1.hi > 100))
        return -1;
    else
        game1.randNum2 = rand() % (game1.hi - game1.lo) + game1.lo;
    return game1.randNum2;

}

// Spec A4 - random number generator between lo and high with error checking -1 & -2
int ProtectRandomNumber2(Game &game1)
{
    game1.lo = 1;
    game1.hi = 100;

    if ((game1.lo >= game1.hi) || (game1.lo < 1) || (game1.hi > 100))
        return -1;
    else
        game1.randNum2 = rand() % (game1.hi - game1.lo) + game1.lo;

    if (game1.randNum2 > 100)
        return -2;
    else
        return game1.randNum2;

}

// Spec C1 - fixed seed function
int fixed_seed(Game &game1)
{
    srand(game1.seed);
    game1.randNum = rand() % 6 + 1;
    return game1.randNum;
}

// C2 Spec - Student/player name
void get_name(Game &game1)
{
    cout << "Please enter your first and last name >> ";
    getline(cin, game1.playerName);

}

// Spec B2 - game greeting with due date
void program_greeting()
{
    cout << endl;
    cout << "Welcome to Pig! ~ Due 09.04.2022" << endl;
    cout << "--------------------------------" << endl;
}

// Specs C3 - Numeric menu
int get_game_choice(Game &game1)
{
    cout << "-------------------------------" << endl;
    cout << "1. Roll die" << endl;
    cout << "2. Hold" << endl;
    cout << "3. Quit" << endl;
    cout << "-------------------------------" << endl;
    cout << "Enter your choice: ";
    cin >> game1.choice;

    return game1.choice;
}


// Spec C4 - Bulletproof menu
int protect_choice(Game &game1)
{
    if (game1.choice == 1)
    {

        cout << "You rolled a " << D6(game1) << "." << endl;
        if (game1.randNum != 1)
        {
            game1.humanScore += game1.randNum;
            cout << "Your turn score is " << game1.humanScore << "." << endl;
        }
        else
        {
            cout << "Your turn score is " << game1.humanScore << "." << endl;
            game1.humanTurn = false;
            game1.validChoice = true;
        }
    }
    else if (game1.choice == 2)
    {
        game1.overallHumanScore += game1.humanScore;
        cout << "Your turn score is " << game1.humanScore << "." << endl;
        cout << "Your overall score is " << game1.overallHumanScore << "." << endl;
        game1.humanTurn = false;
        game1.validChoice = true;
    }
    else if (game1.choice == 3)
    {
        exit(0);
    }
    else
    {
        cout << "Invalid choice. Please enter 1, 2, or 3." << endl;
    }
}


// The actual game 
void pig_game(Game &game1)
{

    if (game1.humanTurn)
    {
        cout << endl;
        cout << game1.playerName << "'s game." << endl;
        game1.humanScore = 0;
        game1.validChoice = false;
        while (!game1.validChoice)
        {
            get_game_choice(game1);
            cout << endl;
            protect_choice(game1);

        }
    }
    else
    {
        cout << "Computer's turn." << endl;
        game1.computerScore = 0;
        game1.validChoice = false;
        while (!game1.validChoice)
        {

            game1.randNum = rand() % 3 + 1;
            if (game1.randNum == 1)
            {
                game1.overallComputerScore += game1.computerScore;
                cout << "The computer's turn score is " << game1.computerScore << "." << endl;
                cout << "The computer's overall score is " << game1.overallComputerScore << "." << endl;
                game1.humanTurn = true;
                game1.validChoice = true;
            }
            else
            {
                game1.randNum = rand() % 6 + 1;
                cout << "The computer rolled a " << game1.randNum << "." << endl;
                game1.computerScore += game1.randNum;
                cout << "The computer's turn score is " << game1.computerScore << "." << endl;
            }
        }
    }
    if (game1.overallHumanScore >= 100)
    {
        game1.gameOver = true;
        cout << "-------------------------------" << endl;
        cout << game1.playerName << " wins!" << endl;
        cout << "~ Final score ~ " << endl;
        cout << game1.playerName << ": " << game1.overallHumanScore << endl;
        cout << "Computer: " << game1.overallComputerScore << endl;
    }
    else if (game1.overallComputerScore >= 100)
    {
        game1.gameOver = true;
        cout << "-------------------------------" << endl;
        cout << "The computer wins!" << endl;
        cout << "~ Final score ~" << endl;
        cout << game1.playerName << ": " << game1.overallHumanScore << endl;
        cout << "Computer: " << game1.overallComputerScore << endl;
    }

}

// Spec B3 - Store high score on the heap and delete afterwards.
int high_score(Game &game1)
{
    game1.highScore = game1.overallHumanScore > game1.overallComputerScore ?
        new int(game1.overallHumanScore) : new int(game1.overallComputerScore);

    return *game1.highScore;

}

file input and structures with c++

am doing a project with I/O and structs. My text file is below. I need to make my program store each string in a different part of the array of structs I have created. I am having a problem making it separate them in the array when it senses a blank line.

Steps for the program: 1. Read each line with data and store it in the struct array until it reaches a blank line. 2. Output each string in a different group or on a different line.

Text file:

ecl:gry pid:860033327 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm

iyr:2013 ecl:amb cid:350 pid:028048884
hcl:#cfa07d byr:1929

hcl:#ae17e1 iyr:2013 cid:150
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm

hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in cid:230
My code:


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

const int SIZE = 4;

struct Passport{

    std::string singlePass;

};

int main()
{
    Passports records[SIZE];
    std::string fileName = "some_file.txt";
    std::ifstream inFile(fileName);
    std::string line, data;
    if (inFile.is_open()){
        while (!inFile.eof()) {
            getline(inFile, line);
            std::istringstream ss(line);
            for (int i = 0; i < SIZE; i++) {
                while (ss >> records[i].singlePass) {
                    std::cout << records[i].singlePass;
                }

            }
        }
    }
    else {
        std::cout << "Error opening file! " << std::endl;
    }   
}