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.
#include <cctype>
#include <iostream>
#include <string>
#include<time.h>
usingnamespace 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<unsignedint>(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.
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!