Various questions....

Working on a program for a class assignment and I've ran across a few questions that I was curious about and haven't been able to find answers to.
I have taken the time to search, but when you don't know exactly what you're searching for it's rather hard to find :-x.

Anyhow, the code is technically "unfinished" but it is in a working condition. Please keep in mind that I am in a CS 142 class (meaning first year new CS class, etc).

I prefer to learn on my own, so links to pages, hints, or little pieces would be helpful :-). Not looking for someone to write my code for me :-x.

Anyhow, first question.
1 - At the beginning of my code it asks for the user to choose either a, b, or c. I have it set up so that it will loop until the correct input is placed, and to accept either the capitol or lower case form of each. However, if a number is placed in instead of a string, it blows up :-x. I can't seem to find a fix. The closest thing I found was 'isdigit()' command, but I couldn't get it to work properly. Any thoughts?

2 - I also need to find out how to randomly add or subtract .5. That will be for the part that is labeled <insert program here>. But basically in a Plinko machine it will bounce from the top down, and at each intersection it has a 50-50 chance to go either way. I think I have the gist of writing it and using a loop with a count++ command (sorry for my lack of terminology) and having it run itself 8 times (for the number of times it would bounce down), but I can't seem to find anything to do with randomly adding or subtracting .5

Anyhow, question 1 is more important than question 2. I'll keep researching that one.

Below is my code.

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <math.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
using namespace std;

int main(){

    string option_select;
    int slot;

    cout <<"********  PLINKO  SIMULATOR  ********\n\n";
    cout <<"Please select from the following options:\n";
    cout <<"A: Drop one token\n";
    cout <<"B: Drop multiple tokens\n";
    cout <<"C: Quit the program\n";
    cin >> option_select;


    while (option_select != "A" && option_select !="a" && option_select !="B"
           && option_select !="b" && option_select !="C" && option_select !="c")
    {
        cout <<"That is not a valid input, please try again.\n";
        cout <<"Please select from the following options:\n";
        cout <<"A: Drop one token\n";
        cout <<"B: Drop multiple tokens\n";
        cout <<"C: Quit the program\n";
        cin >> option_select;
    }


    if (option_select == "A" || option_select == "a")
    {
        cout <<"\nThere are 8 slots in which you can drop your token!\n";
        cout <<"In which slot will we be dropping your token?\n";
        cout <<"Slot: ";
        cin >> slot;

        while (slot >=9 || slot <=0)
        {
            cout <<"That is not a valid input, please try again\n";
            cout <<"\nThere are 8 slots in which you can drop your token!\n";
            cout <<"In which slot will we be dropping your token?\n";
            cout <<"Slot: ";
            cin >> slot;
        }

        while (slot <=8 || slot >=1)
        {
            cout <<"\n<instert program here>\n";
            break;
        }

    }


//C's option
    if (option_select == "C" || option_select == "c")
    {
        cout <<"\n\n****************************************\n";
        cout <<"May the odds forever be in your favor!!!\n";
        cout <<"****************************************\n\n";

    }







    return 0;
}
1-Handing user input is one of the most difficult things in programming because you literally have to be prepared to handle any input :p The google question would be along the lines of: "how to handle/catch/ bad/wrong user input c++"
http://www.cplusplus.com/forum/general/50775/

2-Random number generators usually return integer values. This is because you can adjust their ranges to fit your need including decimals.
double result = (randomnum % 2) - 0.5; //% modulus operator
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
Last edited on
Topic archived. No new replies allowed.