Giter Site home page Giter Site logo

guessing_game_with_vector's Introduction

Guessing_Game_with_Vector

This Program allows user to guess a number between 0-250. User gets unlimited guesses until they guess the right number. A text file saves the lowest number of guesses to guess the right number (Best Score).

#include <iostream>
#include <string>
#include <cstdlib> 	    
#include <ctime>
#include <vector>
#include <fstream>

using std::cout;
using std::string;
using std::endl;
using std::cin;
using std::getline;
using std::vector;
using std::ifstream;


//Saves Best Score
void save_score(int count)
{
ifstream infilevar("bestScore.txt");

if(!infilevar.is_open())	//If input file isn't open
{
    cout << "Unable to read file\n";
}

int best_score;
infilevar >> best_score;

std::ofstream output("bestScore.txt");	
if (count < best_score)
{
    output << "The best score is: " << count;
}
else
{
    output << "The best score is: " << best_score;
}
}


void print_vector(vector<int> vector)		//Function to count guesses
{
cout << "Your guesses were: ";
for (int i = 0; i < vector.size(); i++)
{
    cout << vector[i] << ",  ";
}
cout << endl;
}

void play_game()
{
int guess;			//User guesses number
vector <int> guesses;	   	//Counts/stores guesses

int count = 0;			//count counts how many guesses
int random = rand() % 251;		

cout << "You chose to play!\n";
cout << "Guess a number: ";

while(true)		//Indefinite loop (Ends manually)
{
    cin >> guess;		//cins for "Guess a number"
    count++;	//Counts every guess
    guesses.push_back(guess);		//Saves each guessed number

    if (guess == random)
    {
   		cout << "You guessed it correctly!\n";
	break;	//Ends while loop
    }
    else if (guess < random)
    {
	cout << "Guess is too low! Guess again: ";
    }
    else
    {
	cout << "Guess is too high! Guess again: ";
    }
}

save_score(count);

print_vector(guesses);	
}



int main()
{
//Menu to select games
cout << "MENU OPTIONS:\n------------------------\n";
cout << "0: Exit" << endl << "1: Play Game\n";

int choice;
srand(time(NULL));	//Seeds random number. Only needs to be done once

//do while contains menu and switch statement
do
{
    cout << "\nMENU OPTIONS:\n------------------------\n";
    cout << "0: Quit" << endl << "1: Play Game\n";
    cout << "\nENTER HERE: ";

    cin >> choice;	//Gets user menu choice



    switch(choice)
    {
	case 0:
	    cout << "Goodbye! Thanks for playing!\n";
	    return 0;
	case 1:
	    play_game();
	    break;

    }
}
while (choice != 0);
}

guessing_game_with_vector's People

Contributors

cameronthedeveloper avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.