Help me please !

Write a function that simulates a slot machine by printing three randomly chosen strings
as the values displayed by the slot machine. Each string should be chosen randomly from
among four different choices, such as "orange", "lemon", "lime", "cherry"
(but any words will do). Choose the random values eight times and display each choice
of three as shown in the following sample run. If the strings are all the same or are all
different when the final sequence of these strings appears, then print a message that the
user wins; otherwise the user loses.


My teacher asked me to do this but I had no idea what to code but I tried to do apstring then cin the name but i don't really know what to do next any suggestion or help?
#include <iostream>
#include "apstring"

int main()
{
apstring orange
cin orange
apstring lemon
cin lemon
apstring lime
cin lime
apstring cherry
cin cherry
Last edited on
I wouldn't be realy helping you in long run if i would write the code you need but, as far as i understand english, what it asks is to make 4 strings that contain values of, "orange", "lemon", "lime" and "cherry".

Then make the program asks user 8 times for input(try again? y/n), and then each time it randomly chooses 3 strings, if there was no match after 8 attempts program ends and displays you lost.

Meaning it will randomly choose 3 strings each time user inputs, in mean while if it happens there are 3 equal strings(similar as with slot machine) in atempt then user wins, and program ends.

If there is no string match in 8 attempts user loses.

you will need these preprocessor directives to work with rand and time function to create pseudo rand number:

#include <ctime>
#include <cstdlib>

hope it helps
Last edited on
First off, unless your instructor really wants you to use the apstring class, I would recommend switching to your standard STL string. I did a little digging and it looks like apstring is a "poorly implemented string class used by the college board for AP computer science exams".

Now, if you're struggling with the concept, sometimes it helps to ask yourself some questions and to draw a model of the program flow on a piece of paper.
How much of the program flow depends on user input (if at all)?
How can I map a value/number to a string?
And so on...
Right I'm afraid this question is a little two easy for me to try and help with certain parts of the code without giving it all away, After all we learn the best from our own mistakes so I think you should try a few different things first.

However I will agree with @xismn and recommend that you use standard strings (use the <string> header) rather than apstring.
Also I'd recommend after you've created the flowchart (which is a good piece of advice as it lets you clearly see the flow of your program and how you want it to work) you should create what's called psuedocode [probably not spelled right]...

It's basically like a plain English version of your code, what you want to do, It really helps if you're struggling with the code or ideas of where you put little bits here and there.

1
2
3
4
5
6
7
8
string called lemon = "lemon"
string called lime = "lime"
string called orange = "orange"
string called cherry = "cherry"

function to getRandomCombination()
function to checkIfWon()
function to askPlayAgain()


Okay you don't actually need all these functions but it might help if once you've got your ideas to build up each little piece separately and then join them all together rather than dive strait in and get your ifs and ands muddled up!

Hope This helps a little!
Good luck
Topic archived. No new replies allowed.