# Beginnings of a Scattergories program

I wrote this program as a start towards implementing the game Scattergories. I had a problem with the _sleep command so I used Sleep in the windows.h library.

Eventually, I would like to add a way to either check if a word is correct, or to suggest words, and to activate a sound when the timer is done.

// A program to keep track of points and time and to give a random letter for the game scattergories

#include<iostream>
#include<Windows.h>
using std::cout;
using std::cin;

void ltr()    //gives a random letter
{
char letter;
letter = rand() % 27 + 64;         //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}

void clock()   //timer
{
cout << "You got 1.5 minutes to finish\n";
for (int i = 90; i > 0; i--)
{
if (i % 5 == 0)
cout << i << "\n";
Sleep(1000);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}

int points()        //points per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n";          //worth 15 points
cin >> a;
cout << "How many words only you got?\n";       //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n";    //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum;           //Note: It doesn't matter how many sections there are.
}

int act()
{
int Points;
ltr();
clock();
Points=points();
cout << "You have earned " << Points << " this round\n\n";
return Points;
}

int main()
{
int Points;
cout << "Starting in five seconds\n";
Sleep(5000);
Points = act();
for (;;)          //inf loop
{
int ph;
cout << "Press 1 to continue or 2 to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act();
}
else
{
break;
}
}
cout << "You have earned a total of " << Points << " great job!\n\n";
system("pause");
}

• Are you able to use c++11 features? If so, I'd see a number of things to improve. – πάντα ῥεῖ Dec 29 '16 at 17:32
• Please don't change the question after receiving input. Editing the code invalidates the answers. – t3chb0t Dec 31 '16 at 9:06
• I've already rolled it back. But you can do it yourself anytime too. Now it's no longer necessary, but maybe next time. – t3chb0t Dec 31 '16 at 10:49
• I have rolled back the last edit. Please see What to do when someone answers. – Mast Dec 31 '16 at 10:51
• Feel free to write a follow-up question instead. – Mast Dec 31 '16 at 10:52

## Use portable code as much as possible

You should avoid

#include <Windows.h>


when you can use portable c++ features (according the current c++ standard) instead:

Sleep(1000);


should be

std::this_thread::sleep_for(1s); // needs to #include <thread>


As you see that's much more readable, and will compile on other operating system environments as well.

## Use a better random number generator

letter = rand() % 27 + 64;


Doesn't provide a good random number generator (besides you missed to initialize the seed with srand() and your program will produce the same sequence of random numbers with every run).

C++ provides easy to use and configurable (regarding the wanted number distribution requirements) random generators like listed here.

## Do not use system("pause");

Besides that statement isn't portable again, it introduces security problems. If there's a program existent and accessible at the system called pause that doesn't do what's intended (but some malicious things instead), that one will be executed.

If you wan't to wait for some input rather use std::cin >> dummy; or such.

## Check the state of input operations

 cin >> ph;


You never checked that the user inputted a valid number. If something like xxxx is entered, your loop will never stop.

Better do something like:

bool stop = false;
do
{
int ph;
cout << "Press 1 to continue or 2 to stop\n";
if(cin >> ph) {
if (ph == 1)
{
Points += act();
}
else if(ph == 2)
{
stop = true;
}
}
else {
cin.clear();
std::string dummy;
std::getline(cin,dummy); // Consume the invalid input
}
} while(!stop);


I would like to add a way to either check if a word is correct, or to suggest words but it would mean I need to connect it to a dictionary of some sort (which I don't know how to do).

You can use std::map or std::unordered_map to create dictionaries.

I can't give you any sample code how to integrate these in your program, since it's unclear for me how you wan't to make use of these exactly.

• @L.F. Of course it was. Thanks for noticing. – πάντα ῥεῖ Jul 21 '19 at 15:39

After taking the input I added srand() with the time.h header to prevent getting the same pattern of letters every time starting the program. I also removed the windows.h header and used _sleep (which now works, still have no idea why) and removed the system("pause"). Also a possible way to add a table

The new code:

/* To do list:
*Convert to arduino
*Make timer work in background of of table
*Check if words in the table (for differant players) are the same and give points accordingly
*Check if words are actual words (connect an online dictonary?)
*Make interface? (if possible and I have time to learn how)
*Think of what to do with Hardwear
*Comment rest of the code
*/

// A program to keep track of points and time and to give a random letter for the game scattergories
#include "stdafx.h"
#include<iostream>
#include<time.h>
#include<string>
using std::cout;
using std::cin;
using std::string;
using std::getline;

void ltr()    //gives a random letter
{
srand(time(NULL));    //gives a differant pattern every time
char letter;
letter = rand() % 27 + 64;         //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}

void timer()
{
cout << "You got 1.5 minutes to finish\n";
for (int i = 90; i > 0; i--)
{
if (i % 5 == 0)
cout << i << "\n";
_sleep(1000);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}

void table()
{
int plr, ctr;
string lst[5][20];           //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin>>plr>>ctr;       //parameters for later
cin.ignore();                  //To avoid the "getline" reading the last input
for(int x=0;x<plr;x++)       //the player changes only after the previus player finishes
{
timer();
for(int i=0;i<ctr;i++)        //changing catagory
{
getline(cin,lst[x][i]);
}
system("cls");
cout<<"Next player\n";
}
for(int x=0;x<plr;x++)                   //this part (the whole "for" loop) is for confirming
{
cout<<"Player number "<<x+1<<": ";
for(int i=0;i<ctr;i++)
{
cout<<lst[x][i]<<"    ";
}
cout<<"\n";
}
_sleep(5000);
}

int points()        //points per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n";          //worth 15 points
cin >> a;
cout << "How many words only you got?\n";       //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n";    //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum;           //Note: It doesn't matter how many sections there are.
}

int act()
{
int Points;
ltr();
table();
Points = points();
cout << "You have earned " << Points << " this round\n\n";
return Points;
}

int main()
{
int Points;
cout << "Starting in five seconds\n";
_sleep(5000);
Points = act();
for (;;)          //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act();
}
else
{
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
_sleep(5000);       //time to read the last text
return 0;
}