How would I write this if statement - if-statement

The condition is that a product gets a discount if it is bought on september 15th through october 15th. How would I write an if statement for this?
You could start off with:
if ((month != 9 || month != 10) && (day....

I would say:
if (month == 9 && day >= 15) || (month == 10 && day <=15)

if((month == 9 && day >= 15) || (month == 10 && day <= 15)) { }

if ((month == 9 && day >= 15) || (month == 10 && day <= 15))

Related

Can the else clause be simplified in this if\else ladder?

I have thiselse clause:
else if (iItemIndex == 1 || iItemIndex == 3 || iItemIndex== 5 || iItemIndex == 7 ||
iItemIndex == 10 || iItemIndex == 12 || iItemIndex == 14 || iItemIndex == 16 ||
iItemIndex == 19 || iItemIndex == 21 || iItemIndex == 23 || iItemIndex == 25)
Can it be simplified in some way? Nothing wrong with the code. Just curious if there is a less verbose way of doing the test. In context I have:
if(iItemIndex == 0 || iItemIndex == 9 || iItemIndex == 18)
{
// Do something
}
else if (iItemIndex == 1 || iItemIndex == 3 || iItemIndex== 5 || iItemIndex == 7 ||
iItemIndex == 10 || iItemIndex == 12 || iItemIndex == 14 || iItemIndex == 16 ||
iItemIndex == 19 || iItemIndex == 21 || iItemIndex == 23 || iItemIndex == 25)
{
// Do something else
}
else
{
// Do something else
}
You can write
const bool rng=i>=0 && i<27;
if(const auto r=i%9; rng && !r) …
else if(rng && (r&1)) …
else …
This can of course be much simplified if the value may be assumed to be in the relevant range:
if(const auto r=i%9; !r) …
else if(r&1) …
else …

Verifying that inputs are in range

I'm having a problem implementing this.. the program is meant to take date input in the format dd.mm.yyyy, it does that already but I've been trying to add the code that checks
if the date is more than 31 or February is more than 28
if the month is more than 12
It should return an error since the input is incorrect
And also if a user start input with . then the program shouldn't allow more than 2 inputs of .. so that the user can just just go back with the arrow buttons and type in the correct input
#include "examplevalidator.h"
ExampleValidator::ExampleValidator(QObject* parent)
: QValidator (parent)
{
}
ExampleValidator::~ExampleValidator()
{
}
QValidator::State ExampleValidator::validate( QString & input, int & pos ) const
{
if (input.isEmpty()) return Intermediate;
bool ok;
QString str;
str = input[pos-1];
if(input.size() == 10
&& ((input[2] == '.'))
&& ((input[5] == '.'))
&& input[0].isDigit()
&& input[1].isDigit() && (input.toInt(&ok, 10) <= 31)
&& input[3].isDigit()
&& input[4].isDigit() && (input.toInt(&ok, 10) <= 12)
&& input[6].isDigit()
&& input[7].isDigit()
&& input[8].isDigit()
&& input[9].isDigit() && (input.toInt(&ok, 10) <= 2020)
// && (pos == 1 )&& (input.toInt(&ok, 10) <= 3)
// && (pos == 2 )&& (input.toInt(&ok, 10) <= 31)
)
return Acceptable;
if(input.size() > 10)
return Invalid;
if((pos == 1 )&& (input.toInt(&ok, 10) <= 3) && input[0].isDigit())
{return Intermediate;}
if((pos == 2) && (input.toInt(&ok, 10) <= 31) && input[pos-1].isDigit())
{return Intermediate;}
if((pos == 3) && (input.toInt(&ok, 10) <= 1) && input[pos-1].isDigit())
{return Intermediate;}
if((pos == 4) && (input.toInt(&ok, 10) <= 1) && input[3].isDigit())
{return Intermediate;}
if((pos == 5) && (input.toInt(&ok, 10) <= 12) && input[4].isDigit())
{return Intermediate;}
// if((pos == 6) && (input.toInt(&ok, 10) <= 999999) && input[pos-1].isDigit())
// {return Intermediate;}
if((pos == 7) && (input.toInt(&ok, 10) <= 9) && input[6].isDigit())
{return Intermediate;}
if((pos == 8) && (input.toInt(&ok, 10) <= 99) && input[7].isDigit())
{return Intermediate;}
if((pos == 9) && (input.toInt(&ok, 10) <= 999) && input[8].isDigit())
{return Intermediate;}
if((pos == 10) && (input.toInt(&ok, 10) < 99999) && input[9].isDigit())
{return Intermediate;}
if (input[0] == '.' )
{return Intermediate;}
if(pos > 1)
{
if (input.isEmpty()) return Intermediate;
if((pos > 1) || (pos <=2 ) && (input[pos-1] == '.'))
return Intermediate;
if(pos > 8 && pos <= 10 && input[pos-1].isDigit())
{return Intermediate;}
else {return Invalid;}
}
}
You can use a regular expression to validate dd.mm.yyyy
Have a look at https://regex101.com/ for trying them out.
Something like the code below should get you started
#include <regex>
std::smatch match;
const std::regex ddmmyyyyRegExp(R"((([0-9]{2})\.([0-9]{2})\.([0-9]{4})))");
std::string dd;
std::string mm;
std::string yyyy;
if (std::regex_search(input, match, ddmmyyyyRegExp) && match.size() == 4)
{
dd= match.str(1);
mm= match.str(2);
yyyy= match.str(3);
}
I've been able to fix the code, but still I'm unable to check if the inputs are in a particular range. I'm trying to check if the date limit isn't more than 31 and month isn't greater than 12
ExampleValidator::ExampleValidator(QObject* parent)
: QValidator (parent)
{
}
ExampleValidator::~ExampleValidator()
{
}
QValidator::State ExampleValidator::validate( QString & input, int & pos ) const
{
if (input.isEmpty()) return Intermediate;
bool ok;
QString str;
str = input[pos-1];
if(pos > 0 && input[0] != '.')
{
if (input.isEmpty()) return Intermediate;
if(((pos == 1 )&& (input.toInt(&ok, 10) <= 9) && input[0].isDigit() && input.size() != 10))
{return Intermediate;}
if(((pos == 2) && (input.toInt(&ok, 10) <= 99) && input[1].isDigit() && input.size() != 10))
{return Intermediate;}
if(((pos == 3) && (input.toInt(&ok, 10) <= 999)) && (input[2].isDigit() || input[2] == '.') && input.size() != 10)
{return Intermediate;}
if((pos == 4) && (input.toInt(&ok, 10) <= 9999) && input[3].isDigit() && input.size() != 10)
{return Intermediate;}
if((pos == 5) && (input.toInt(&ok, 10) <= 99999) && input[4].isDigit() && input.size() != 10)
{return Intermediate;}
if(((pos == 6) && (input.toInt(&ok, 10) <= 999999)) && (input[5].isDigit() || input[5] == '.') && input.size() != 10)
{return Intermediate;}
if((pos == 7) && (input.toInt(&ok, 10) <= 99999999) && input[6].isDigit() && input.size() != 10)
{return Intermediate;}
if((pos == 8) && (input.toInt(&ok, 10) <= 999999999) && input[7].isDigit() && input.size() != 10)
{return Intermediate;}
if((pos == 9) && (input.toInt(&ok, 10) <= 999999999) && input[8].isDigit() && input.size() != 10)
{return Intermediate;}
if((pos == 10) && (input.toInt(&ok, 10) < 2020) && input[9].isDigit() && input[2] == '.' && input[5]=='.' )
{
return Acceptable;}
}
if(((pos == 1) && (input[0] == '.')) || ((pos == 2) && (input[1] == '.')))
{
return Intermediate;
}
else if (input.isEmpty()) {return Intermediate;}
}```

Resolve day of the year c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
i'm new at programming and i'm currently taking C++
I have this code and for example if i'm input 2018 2 32 the output must be 0, but now the output is 62 and i don't know how to resolve this. I need help and wonder if someone can take a look at my code and tell me what is wrong with it?
#include <iostream>
using namespace std;
// pointer
const int january = 31;
const int february = 28;
const int isLeapFeb = 29;
const int march = 31;
const int april = 30;
const int may = 31;
const int june =30;
const int july = 31;
const int august = 31;
const int september = 30;
const int october = 31;
const int november = 30;
const int december = 31;
bool isLeap(int year) {
// menentukan tahun kabisat dan biasa
return (((year % 4) == 0) && (((year % 100) != 0) || ((year % 400) == 0)));
}
int monthLength(int year, int month, int day) {
// bulan dengan hari 31
if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month || 8) || (month == 10) || (month || 12))
{
if((day > 0) && (day < 32))
return true;
else
return false;
}
// bulan dengan hari 30
if((month == 4) || (month == 6) || (month == 9) || (month == 11))
{
if((day > 0) && (day < 31))
return true;
else
return false;
}
if(month == 2) // februari 28 hari
{
if((day > 0) && (day < 29))
return true;
else
return false;
}
if((isLeap(year))) // februari 29 hari
{
if((month == 2) && ((day > 0) && (day < 30)))
return true;
else
return false;
}
}
int dayOfYear(int year, int month, int day) {
// tahun 1582 Gregorian kalender mulai
int dayTotal = 0;
if(monthLength(year, month, day))
{
if(month == 1)
{
dayTotal = 0 + day;
}
if(month == 2)
{
dayTotal = january + day;
}
if((month == 3) && (year > 1582))
{
dayTotal = january + february + day;
if((month == 3) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + day;
}
}
if((month == 4) && (year > 1582))
{
dayTotal = january + february + march + day;
if((month == 4) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + day;
}
}
if((month == 5) && (year > 1582))
{
dayTotal = january + february + march + april + day;
if((month == 5) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + april + day;
}
}
if((month == 6) && (year > 1582))
{
dayTotal = january + february + march + april + may + day;
if((month == 6) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + april + may + day;
}
}
if((month == 7) && (year > 1582))
{
dayTotal = january + february + march + april + may + june + day;
if((month == 7) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + april + may + june + day;
}
}
if((month == 8) && (year > 1582))
{
dayTotal = january + february + march + april + may + june + july + day;
if((month == 8) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + april + may + june + july + day;
}
}
if((month == 9) && (year > 1582))
{
dayTotal = january + february + march + april + may + june + july + august + day;
if((month == 9) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + april + may + june + july + august + day;
}
}
if((month == 10) && (year > 1582))
{
dayTotal = january + february + march + april + may + june + july + august + september + day;
if((month == 10) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + april + may + june + july + august + september + day;
}
}
if((month == 11) && (year > 1582))
{
dayTotal = january + february + march + april + may + june + july + august + september + october + day;
if((month == 11) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + april + may + june + july + august + september + october + day;
}
}
if((month == 12) && (year > 1582))
{
dayTotal = january + february + march + april + may + june + july + august + september + october + november + day;
if((month == 5) && (year > 1582) && (isLeap(year)))
{
dayTotal = january + isLeapFeb + march + april + may + june + july + august + september + october + november + day;
}
}
}
return dayTotal;
}
int main(void) {
int day, month, year;
cout << "Enter year month day: ";
cin >> year >> month >> day;
cout << dayOfYear(year, month, day) << endl;
return 0;
}
It compile but the output is not correct. I really appreciate your help and feedback. Thank you very much
That is because your are passing days as 31 and when this line executed:
if(month == 2)
{
dayTotal = january + day;
}
Then
dayTotal = 31 + 31; // = 62 which is what you see.
You need to use the debugger to discover such errors, beside you need to add some validations to prevent invalid data such as 02/31/2018
EDIT:
Based on suggestions from #Slava, I have revised my answer. This implementation uses a single regular expression match to validate and to extract the components of the date. Here is the code:
#include <iostream>
#include <string>
#include <regex>
#include <exception>
using namespace std;
void parseDate(const string& date, int& year, int& month, int& day)
{
regex dateValidateRe(R"(^(\d{4})\-(\d{1,2})\-(\d{1,2})$)");
smatch matches;
if (!regex_search(date, matches, dateValidateRe))
{
throw invalid_argument("Date format is incorrect");
}
year = stoi(matches[1]);
month = stoi(matches[2]);
day = stoi(matches[3]);
}
int main()
{
int year, month, day;
string date;
cin >> date;
try
{
parseDate(date, year, month, day);
}
catch (std::exception& ex)
{
cout << "Invalid input: " << ex.what() << endl;
}
cout << "The date entered was Year = " << year << " Month = " << month << " Day = " << day << endl;
return 0;
}
Here is a working demo
** Original Response **
You're not parsing your input correctly. Your input is 2018-2-31 is being parsed as year 2018, month -2, and day -31. You need to parse your date as a string and then break that string apart by the token - to extract year, month, and day.
Here is a quick and dirty function that you can use to parse the input correctly:
void parseDate(const string& date, int& year, int& month, int& day)
{
auto ypos = date.find("-");
string syear = date.substr(0, ypos);
auto mpos = date.find("-", ypos+1);
string smonth = date.substr(ypos+1, mpos-ypos-1);
string sday = date.substr(mpos+1);
year = stoi(syear);
month = stoi(smonth);
day = stoi(sday);
}
Update your main as follows:
int main(void) {
int day, month, year;
cout << "Enter year month day: ";
string date;
cin >> date;
parseDate(date, year, month, day);
cout << dayOfYear(year, month, day) << endl;
return 0;
}
This should give you the right results. Please update parseDate so it validates the input string before it proceeds to tokenize it.

Why doesn't this output "Please try again" when loop runs again?

Hello I am trying to learn c++ and I wanted to give a little practice with a program. However I'm having trouble using cout within the loop.
This is the loop I'm trying to output text from. When the user enters a number that isn't valid it is supposed to say "Sorry try again!"
while (datecheck)
{
bool check(false);
if (check)
std::cout<<"Sorry try again!"<<std::endl;
std::cin>>c;
if (c >= 1)
{
if (b == 2 && c <= 28)
datecheck = false;
if (b == 2 && a % 4 == 0 && c <= 29)
datecheck = false;
if (b == 4 || b == 6 || b == 9 || b == 11 && c <= 30)
datecheck = false;
if (c <= 31)
datecheck = false;
}
check = true;
}
When it outputs and I purposely keep myself in the loop it doesn't output anything
Year: -20
-20
-20
You declare a fresh new variable check at every iteration. And you initialize that variable to false every time. So move that declaration before the while loop.
Change this:
while (datecheck)
{
bool check(false);
...
check = true;
}
to this:
bool check(false);
while (datecheck)
{
...
check = true;
}
The problem is with declaration of bool check(false);. This keeps on re-assigning value to false at beginning of each iteration.
A simple fix could be to get-rid of use of check variable and use only datecheck.
bool datecheck(true);
while (true)
{
std::cin>>c;
if (c >= 1)
{
if (b == 2 && c <= 28)
datecheck = false;
if (b == 2 && a % 4 == 0 && c <= 29)
datecheck = false;
if (b == 4 || b == 6 || b == 9 || b == 11 && c <= 30)
datecheck = false;
if (c <= 31)
datecheck = false;
}
if (datecheck)
{
std::cout<<"Sorry try again!"<<std::endl;
}
else
{
break;
}
}

Control may reach end of non-void function (checking for valid date)

bool isValidDate(int month, int day, int year) {
if(year <= 1752){
if(month <= 9 && month <= 13){
return false;
}
else{
}
}
else{
if((month == 9 || month == 4 || month == 6 || month == 11) && (day <= 30)){
return true;
}
else if((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31)){
return true;
}
else if(isLeapYear(year)){
if((month == 2) && (day <= 29)){
return true;
}
}
else if(!isLeapYear(year)){
if((month == 2) && (day <= 28)){
return true;
}
}
else{
return false;
}
}
}
I'm trying to make it so that if users enter any date before Sept. 13, 1752, it will return false. And also if the days have to be correct according to the month while taking into account leap years for the month of February. But I keep getting an error. Help much needed.