help with code (adding if else...)

I am in a beginner programming class for C++, I am working on my final project we have been building on this semester. I am having some trouble fixing some things that will be "breakable" in my code.
I will post the code first then add comments to the specific areas of concern.

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

#include <cctype>  
#include <iostream>
#include <string>
#include<time.h>

using namespace std;

void start()
{
    string name;

    cout << "\nPlease enter your name then hit enter: ";


    getline(cin, name);

    cout << "\nWelcome to your Daily Intention, " << name << '\n';

    char age{},   // Are you 13 or older, Y or N
        Positive; // Are you in a Positive Headspace, Y or N

    // Is the user 13 or older and in a positive headspace?

    cout << "\nAnswer the following questions\n";

    cout << "with either Y for Yes or " ;

    cout << "N for No.\n";


    cout << "\nAre you 13 or older? " ;

    cin >> age;

    cout << "Do you want to add some positivity to your day? ";

    cin >> Positive;

    // Determine if the user is old enough and in a positive headspace

    if (std::toupper(age) == 'Y')
    {
        if (std::toupper(Positive) == 'Y')
        {

            cout << "You are old enough to receive a daily intention ";

            cout << "Positive vibes are coming your way.\n";

            int x, y;

            int sum;

            cout << "\nChoose a number between 1 and 10 then hit enter: ";

            cin >> x;

            // multiplies user input with a random number between 1 and 10
            srand(static_cast<unsigned int>(time(NULL)));
            y = rand() % 10 + 1;

            sum = x * y;

            cout << "Your Daily Intention number for the day is: " << sum;

        }
        else // Not looking for positivity, but of age
        {
            cout << "You should be in a positive headspace for the best results ";

            cout << "Take some time to meditate and come back later.\n";
        }
    }
    else // Not of age
    {
        cout << "You must be 13 or older to play.\n";
    }
}

// User finds the intention that fits their number.
void meaning()
{
    cout << " \n If your number is... \n 1-10 Take a walk and say something positive to yourself and someone else. \n 11-20 Go through each room of the house and find one thing you can donate or throw out that is no longer bringing you joy. \n 21-30 Dance in public and just feel free. \n 31-40 Give yourself gratitude. \n 41-50 Forgive and let go anything negative you have been holding onto. \n 51-60 Check in with an old friend or family member. \n 61-70 Take some time to read. \n 71-80 Take a break from all social media. \n 81-90 Do some yoga and/or meditation. \n 91-100 Do something that scares you.\n";
  
}

// The below explains what a daily intention is.
void about()
{
    cout << " \n What is a daily intention ? \n A daily intention is what you want to focus your energy on that particular day. \n By picking a theme or area that you want to focus on each day you can give every day a purpose.\n";

}

// User exits menu option with a goodbye message
void exit()
{
    for (int i = 1; i <= 5; ++i)
        cout << "Good Bye and Good Vibes! " << endl;
}

int main()
{

    int selection;

    do
    {
        std::cout <<
            "\nPlease make a selection then hit enter: \n\n"
            "1) Start\n"
            "2) What your Daily Intention number means\n"
            "3) About a Daily Intention\n"
            "4) Exit\n"
            " Enter choice: ";
        std::cin >> selection;

        if (selection < 1 || selection > 4)
        {
            std::cerr << "\n     Invalid selection! Try again.\n";
        }
    } //while (selection != 1 && selection != 2 && selection != 3 && selection != 4);
    while (selection < 1 || selection > 4);

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    switch (selection)
    {
    case 1:
        start();
        break;
    case 2:
        meaning();
        break;
    case 3:
        about();
        break;
    case 4:
        exit();
        break;
    default:
        break;
    }


    return 0;
}
Lines 110-115: It wont run if select another number besides 1-4 however a letter is still allowed and "breaks" it, anyway to adjust current code to prevent any characters other than 1-4?

Lines 25-40 they are Y or N options but it seems to still run if something other than that is typed

Line 65 - i want to be able to get them back to the menu after to choose option 2.

Those are the first things I want to try to remedy. I am hoping not to change the whole code because I do not want to lose some required elements per the project guidelines.

Thank so much!
Last edited on
read selection as a string, convert to integer if you want or just check to see if it == "1" and so on instead of 1 (string vs int). If its not "1" ... "4" then tell them to try again until they get it right (loop).

Y and N ... you check the Y side and *assume* if it isnt Y, it is N. Instead of else, check for N same as Y, and if its neither of those, else cout << "user is to annoying to play this" or loop back until you get a Y or N

not sure what menu you want to go back to on 65, in main?
not sure what you mean but I *think* you want to either add another do-while around the first do-while AND the switch, .. do ... all that... while (response != 2) ?
Sorry trying to follow... I don't "speak" code that well, I have pieced what I have together from google and the book lol. I will try to look all that up. Thanks!
I can be more helpful :)
string s;
cin >> s;
if(s == "y" || s=="Y")
cout << "yes!"
else if(s == "n" || s=="N")
else
cout << "Invalid!"

see how that is different from if yes, else something? The else something assumes they typed N but they could type z just as easily...

as for string to int, read this page and example code
http://www.cplusplus.com/reference/string/stoi/

after you get that part down, if you still need help with the menu question, explain exactly what you want and ask again.

Last edited on
Thank you!
srand() is usually placed at the beginning of main() and executed only once.
Topic archived. No new replies allowed.