I'm new to C++, teaching myself via youtube and some books I bought. I can not for the life of me figure out why the 2nd while statement will not work. from a mathematical stand point I feel it should work. If month does not = june or july then do the if else statement. But even when I run the right answer it always runs the if not the else. I feel it has something to do with it being a string, so I tested it without the or "||" and it worked. So maybe it has to do with combining strings and or statements. So did research on using these together and could not find much. Thanks for the help.
int main()
{
int year;
int day = 0;
string month = "x";
do
{
if (day == 0)
{
cout << "hello" << endl;
cout << "Please Enter your B-Day as Day, Month, Year" << endl;
cout << "day" << endl;
cin >> day;
}
else
{
cout << "Please enter a correct day" << endl;
cin >> day;
}
} while (day > 31 || day < 1);
do
{
if (month == "x")
{
cout << "Please enter the month you were born" << endl;
cin >> month;
}
else
{
cout << "Please Enter a correct Month." << endl;
cin >> month;
}
}
**while (month != "june" || month != "july");**
return 0;
}
If you do:
while (month != "june" && month != "july");
Or alternatively,
while (!(month == "june" || month == "july"));
Rather than:
while (month != "june" || month != "july");
Your program will be working fine even with multiple logical OR.
while (month != "june" || month != "july");
There is something called short-circuit evaluation in C++ which will in this case will not evaluate the right of || if the first operand returns true. Likewise for &&, it will not evaluate right operand if first is false. So be sure how you want the logic to behave and write the code.
THE IDEA (C++):
The idea is simple, if you're under 21 and in full time education, you're eligible (no idea for what, it's just homework). If you're not eligible, you have to tell the user why.
int main()
{
string education;
int age;
cout << "Are you in full time education? (y/n): ";
cin >> education;
cout << "\nEnter your age: ";
cin >> age;
system("cls");
if (((education == "yes" || education == "y")) && (age <= 21))
{
cout << "You are eligible.";
}
else if (((education == "yes" || "y")) && (age > 21))
{
cout << "You are not eligible because you are over 21.";
}
else if (((education == "no" || "n")) && (age <= 21))
{
cout << "You are not eligible because you are not in full time education.";
}
else if (((education == "no" || "n")) && (age > 21))
{
cout << "You are not eligible because you are not in full time education and you are over 21.";
}
else
{
cout << "There is a problem with your input.";
}
}
THE PROBLEM:
Now, if I input that I'm NOT in fulltime education AND over 21, the output is "You are not eligible because you are over 21.", which is technically true, but it should be giving me "You are not eligible because you are not in full time education and you are over 21." instead!
Things to note:
My #include statements are cut out of the screenshot, but don't worry about them, I know they're fine.
All the "else if" statements were originally just "if", but I made them this way to try and fix the issue.. to no avail clearly.
You can't use the or operator like this
a == 'first' || 'second' // education == 'yes' || 'y'
in order to say "if a is equal to first or second", you have to repeat the a== also on the right hand side:
a == 'first' || a == 'second' // education == 'yes' || education == 'y'
So I'm trying to create a calendar that asks for the user's chosen month and date numerically using switch statement and then I tried to use if statement to check the user's date input and if it matches it displays the day.
When i run it it always prints Sunday even though i inputted the date for a Monday. What am I missing?
Here is my code
void UserInput()
{
cout << "\nPlease enter a numerical value of a month e.g. May=5, December=12: \n";
cin >> Month;
cout << "\nPlease enter a Date: \n";
cin >> Date;
}
void MagicCalendar()
{
switch(Month)
{
case 1:
if(Date == 6 || 13 || 20 || 27)
{
cout << " It's Sunday!!!";
break;
}
if(Date == 7 || 14 || 21 || 28 ) <-- the problem is probably here idk if its the operand or the if statement itself
{
cout << " It's a MOFO MONDAY GO KYS NOW!";
break;
}
else if(Date ==)
{
cout << " It's a Tuesday and kinda like a monday but yeah...";
break;
}
else if(Date ==)
{
cout << " It's Wednesday and I know that you can feel it!";
break;
}
else if(Date ==)
{
cout << " It's a THURSDAY!! getting warmer :D";
break;
}
else if(Date ==)
{
cout << " It's FRIDAY!! GO GET SHITFACED YOU DESERVE IT :D";
break;
}
else if(Date ==)
{
cout << " It's SATURDAY BABY! go cry in a corner while suffering from Hungover...";
break;
}
break;
}
}
if(Date == 6 || 13 || 20 || 27)
Wrong syntax
Correct:
if(Date == 6 || Date == 13 || Date == 20 || Date == 27)
Similar to other lines in your code.
I'm required to write a program that gives a person’s day and month of birth and will then figure out the person’s horoscope sign. The program should prompt the user for a number of persons. When the user responds, the program needs to automatically generate day and month and figure out and display the corresponding horoscope sign.
The program also needs to track number of persons for each sign and display that statistics. After that, the program needs to prompt the user if they want to go again. If the user responds with either a ‘Y’ or a
‘y’ the program needs to repeat the process until the user says that s/he is done.
I have a learning disability and i find it hard to understand sometimes what the questions that are being asked. I do not know how to answer them sometimes. So please be patience with me.
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
int month, date, i, nOfPeople;
i = 0;
cout << "How many people: " << endl;
cout << "Born on Horoscope Sign" << endl;
cout << "-------------------------" << endl;
cin >> nOfPeople;
while (i < nOfPeople) {
// initialize random seed:
srand(time(NULL))
// generate month and date
month = rand() % 12 + 1;
date = rand() % 28 + 1;
if (month == 3 && date >= 21 || month == 4 && date <= 19) {
cout << "4/19 Aries" << endl;
} else if (month == 4 && date >= 20 || month == 5 && date <= 20) {
cout << "5/7 Taurus" << endl;
} else if (month == 5 && date >= 21 || month == 6 && date <= 21) {
cout << "Gemini" << endl;
} else if (month == 6 && date >= 22 || month == 7 && date <= 22) {
cout << "7/8 Cancer" << endl;
} else if (month == 7 && date >= 23 || month == 8 && date <= 22) {
cout << "8/17 Leo" << endl;
} else if (month == 8 && date >= 23 || month == 9 && date <= 22) {
cout << "Virgo" << endl;
} else if (month == 9 && date >= 23 || month == 10 && date <= 22) {
cout << "Libra" << endl;
} else if (month == 10 && date >= 23 || month == 11 && date <= 21) {
cout << "11/3 Scorpio" << endl;
} else if (month == 11 && date >= 22 || month == 12 && date <= 21) {
cout << "12/11 Saguittarius" << endl;
} else if (month == 12 && date >= 22 || month == 1 && date <= 19) {
cout << "12/22 Capricorn" << endl;
} else if (month == 1 && date >= 20 || month == 2 && date <= 18) {
cout << "Aquarius" << endl;
} else if (month == 2 && date >= 19 || month == 3 && date <= 20) {
cout << "2/21 Pieces" << endl;
}
i++;
}
return 0;
}
std::rand and std::srand are defined in <cstdlib>. Include that header then you should be able to use those functions.
Question - A year with 366 days is called a leap year. A year is a leap year if it is divisible by four (for example, 1980), except that it is not a leap year if it is divisible by 100 (for example, 1900); however, it is a leap year if it is divisible by 400 (for example, 2000). There were no exceptions before the introduction of the Gregorian calendar on October 15, 1582. Write a program that asks the user for a year and computes whether that year is a leap year.
This is what I have so far, and the program doesn't seem to run for years greater than 1582. Could someone help me out why? Thanks a bunch
using namespace std;
int main()
{
cout<< "Pleas enter a year: " <<endl;
int year = 0;
cin >> year;
if (year <= 1581)
{
if (year % 4 == 0)
{
cout << "It's a leap year, wow! " << endl;
}
else
{
cout << "It's not a leap year " << endl;
}
}
else if (year > 1581)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
cout<< "It is not a leap year " << endl;
}
else if (year % 400 == 0)
{
cout<< "It is a leap year, Wow!" << endl;
}
}
}
else
{
cout<< "You entered a wrong year number "<< year<< endl;
}
return 0;
}
You are making it very complex. I don't think you need to care about whether the year is greater than 1582 or not (for a 4 digit number) provided that a leap year is one which is:
• Divisible by 400
OR
• NOT divisible by 100 AND divisible by 4.
Using unnecessary nested ifs can make your code long and error prone. Try this method:
#include<iostream.h>
int main(){
int y=0;
cout << "Enter a year\n";
cin >> y;
cout <<"\n"<<y;
if(y%400==0 || (y%100!=0 && y%4==0))
cout <<" is a leap year";
else
cout <<" is not a leap year";
return 0;
}
Without check if year > 1582
#include<iostream>
using namespace std;
int main()
{
int year;
cout<< "Please enter a year: " <<endl;
cin >> year;
if( year%25 && !(year&3) || !(year&15) )
cout << "It's a leap year!" << endl;
else
cout << "It's not a leap year!" << endl;
return 0;
}
You are missing a number of cases in the handling of years after 1581.
printing "Not leap year" unless (year % 4 == 0)
The third case where a year divisible by 4 is neither divisible by 100 nor 400
It as simple as you have just not written any code that is run when year is 2004, for example.
You are missing 2 else statement
using namespace std;
int main()
{
cout<< "Pleas enter a year: " <<endl;
int year = 0;
cin >> year;
if (year <= 1581)
{
if (year % 4 == 0)
{
cout << "It's a leap year, wow! " << endl;
}
else
{
cout << "It's not a leap year " << endl;
}
}
else if (year > 1581)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
cout<< "It is not a leap year " << endl;
}
else if (year % 400 == 0)
{
cout<< "It is a leap year, Wow!" << endl;
}
// <----------- Here you are missing an else
}
// <----------- Here you are missing an else
}
else
{
cout<< "You entered a wrong year number "<< year<< endl;
}
return 0;
}
I suggest
if ( ((year % 400) == 0)
|| ( ((year % 4) == 0) // or (year & 0x3) == 0
&& ( (year <= 1581)
|| ((year % 100) != 0) )))
cout << "It's a leap year, wow! " << endl;
else
cout << "It's not a leap year " << endl;
or
if ( ((year % 4) == 0) // or year & 0x3 == 0
&& ( (year <= 1581)
|| ((year % 100) != 0)
|| ((year % 400) == 0) ) )
cout << "It's a leap year, wow! " << endl;
else
cout << "It's not a leap year " << endl;
It’s a good idea to simplify your conditionals. A general method for this is to convert to a normal form—either conjunctive or disjunctive—and put the tests that are most likely to short-circuit, first. For simple cases such as this, you can just eyeball it.
In this case, conjuctive normal form is extremely simple:
year%4 == 0 &&
( year < 1582 || year%100 != 0 || year%400 == 0 )
That is, the year is divisible by four and any of the three conditions of the Gregorian calendar reform do not hold. Since the first && term that is false, and the first || term that is true, short-circuit the expression, we want to put the clauses that are most likely to short-circuit, first.
Note that you can code-golf year%100 != 0 to year%100 inside a conditional expression, and year%2000 == 0 to !(year%2000), if you find that easier to read.
It makes sense to move this to a helper function. We can mark it constexpr to give the compiler a hint that it can calculate whether constants are leap years or not at compile-time.
I don't like to post complete answers to what look like homework problems, but that ship has sailed.
#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
constexpr bool is_leap(const int year)
{
// Conjunctive normal form:
return ( year%4 == 0 &&
( year < 1582 || year%100 != 0 || year%400 == 0 ) );
}
int main()
{
int year = 0; // Used only within the body of the loop that sets it.
while (cin >> year)
cout << year
<< ( is_leap(year) ? " is a leap year.\n"
: " is not a leap year.\n" );
return EXIT_SUCCESS;
}
Even for a trivial program such as this, there’s a design decision worth thinking about: do we declare int year; uninitialized, or initialize it to a bad value such as int year = 0;? In this case, it’s safe either way, because year is only used inside the body of the loop that sets it. If we don’t initialize it, though, we might later refactor the code to use year outside the loop, and then there might be a code path where year is used before it’s initialized (causing undefined behavior!) On the other hand, if we do initialize year, we might prevent the compiler from noticing that there’s a path where it’s used before it was initialized for real. I personally prefer to initialize to an invalid value and assert that it has been updated before use.
Or you can simply write the following program:
#include<iostream>
using namespace std;
int main()
{
int year;
cout << "Enter year: ";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
cout << "It is a leap year" << endl;
}
else{
cout << "It is not a leap year" << endl;
}
return 0;
}