Code help

!!!
Last edited on

I need to change one of my if/else to a conditional statement

What? An if()/else is a conditional statement.

I would like one of my numbers in the multiplication to be random between one and ten

What number are you talking about? What have you tried?

I need to add a for loop.

Where? For what purpose?


Please edit your post to add code tags around your code.

I have to make these changes to meet assignment requirements

I am not sure how or where to make these changes that's why i am asking- i am in a beginner class and have been messing with it the past few days and I am having trouble

"Add a condition operator. You can replace an existing if/else statement or use a conditional operator as a new addition to your program." (So i need to add something else- the shorter version conditional expression)

Need to have a for loop for requirement purposes I do not know how or where to add that makes sense


The below no longer pops up to enter name it goes straight to the next code
cout << "Please enter your name then hit enter: ";

// This isnt popping up will need to fix this

getline(cin, name);


The below is where i want to change one of the inputs to just be a random number 1-10 (so it multiples that against user input)
int x, y;



int sum;



cout << "Choose a number then hit enter: ";



cin >> x;



cout << "Choose a number then hit enter: ";
// I ideally want to change this to multiply by a random number no greater than 10


cin >> y;



sum = x * y;

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


Also I am unsure what these means
Please edit your post to add code tags around your code.
Last edited on
Hello yogagirl,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


One thing I see in the "main" function until I have a chance to go over all of the code.
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
int main()
{
    // selection must be declared outside do/while loop
    int selection;

    do
    {
        std::cout << "Please make a selection: \n";
        std::cout << "1) Start\n";
        std::cout << "2) What your Daily Intention number means\n";
        std::cout << "3) Exit\n";
        std::cin >> selection;
    } while (selection != 1 && selection != 2 && selection != 3);

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Clears the input buffer.

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

    return 0;
}


Your code is mixing formatted input std::cin >> selection; with unformatted input getline(cin, name);.

The problem with doing this is that the formatted input leaver the "\n" in the input buffer and the unformatted input reads this without waiting for something new.

An advantage to using "getline" is that it will read everything including the "\n", but then discard the "\n" leaving the input buffer empty.

Andy
Hello yogagirl,


I have to make these changes to meet assignment requirements.



What assignment requirements? Are you expecting people to guess at this? Please post the assignment requirements, so everyone will know what you have to do.

Add a condition operator.
What exactly do you mean? Is this an if/else or making use of the ternary operator? I have the idea it means the latter.

When you say that you need a random number what do you have in mind and what can you use?

Andy
Hello yogagirl,

I made some adjustments and additions to your code:
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
#include <cctype>  // <--- Added.
#include <iostream>
#include <string>

using namespace std;

void start()
{
    string name;

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

    // This isnt popping up will need to fix this

    getline(cin, name);

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

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

    // Is the user 18 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? ";  // <--- Does not match the comment for "char age".

    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')  // <--- Changed.
    {
        if (std::toupper(Positive) == 'Y') // Nested if  // <--- Changed.
        {

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

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

            int x, y;  // <--- Good choice for variable names if they refer to a graph.

            int sum;

            cout << "\nChoose a number then hit enter: ";

            cin >> x;

            cout << "Choose a number then hit enter: ";
            // I ideally want to change this to multiply by a random number no greater than 10

            cin >> y;

            sum = x * y;  // <--- Never used.

            //User will look up number to see what intention it is matched with on the menu screen.
        }
        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 18 or older to play.\n";
    }
}

void meaning()
{
    cout << "If your number is...\n";
    // Intentions for the numbers are needed here
}

void exit()
{
    cout << "\n\n       Goodbye.\n";
}

int main()
{
    // selection must be declared outside do/while loop
    int selection;

    // <--- Needs a loop to keep the following lines going.
    do
    {
        std::cout <<
            "\nPlease make a selection: \n\n"
            "1) Start\n"
            "2) What your Daily Intention number means\n"
            "3) Exit\n"
            " Enter choice: ";
        std::cin >> selection;

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

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    // <--- Needs to be done before any "getline".

    switch (selection)
    {
        case 1:
            start();
            break;
        case 2:
            meaning();
            break;
        case 3:
            exit();
            break;
        default:
            break;
    }
    // <--- Loop should end here.

    return 0;
}

One change I madeif (std::toupper(age) == 'Y'). This is 1 way to deal with this or you could use if (age == 'Y' || age == 'y'). It is best to check for both cases if you do not change the case in as in the first example.

Most of the changes are to the output. The output I get is:


Please make a selection:

1) Start
2) What your Daily Intention number means
3) Exit
 Enter choice: 4

     Invalid selection! Try again.

Please make a selection:

1) Start
2) What your Daily Intention number means
3) Exit
 Enter choice: 1

Please enter your name then hit enter: John Doe

Welcome to your Daily Intention, John Doe

Answer the following questions
with either Y for Yes or N for No.

Are you 13 or older? y
Do you want to add some positivity to your day y
You are old enough to receive a daily intention Positive vibes are coming your way.

Choose a number then hit enter: 5
Choose a number then hit enter: 10



Andy
Thank you so much for your help!
Topic archived. No new replies allowed.