C++ Days of the week
Sep 23, 2014 at 10:08pm UTC
I need to write a program that deals with applying a certain discount only on Mondays and Tuesdays. How do i let the user type in a day of the week then apply an if statement to it? Do i use int?
Sep 23, 2014 at 10:28pm UTC
yes.
1 2 3 4 5 6 7 8 9 10 11 12
int dayOfTheWeek;
cout<<"Enter day of today: " ;
cin>>dayOfTheWeek;
if (dayOfTheWeek == 2 || dayOfTheWeek == 3) // 1 == Sunday
{
discount.....
}
else
{
no discount...
}
Sep 23, 2014 at 10:56pm UTC
@shadowCODE
what if I wanted to use words instead of int, how can I do that?
Here are my 2 cents, somehow it doesn't work:
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
#include <iostream>
#include <string>
using namespace std;
void Sales(string dayOfWeek)
{
string Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday;
Saturday = "Saturday" ;
Sunday = "Sunday" ;
Monday = "Monday" ;
Tuesday = "Tuesday" ;
Wednesday = "Wednesday" ;
Thursday = "Thursday" ;
Friday = "Friday" ;
if (dayOfWeek!=Monday || dayOfWeek!=Tuesday)
{
cout << "no discount" << endl;
}
else if (dayOfWeek==Monday || dayOfWeek==Tuesday)
{
cout << "discount" << endl;
}
}
int main()
{
string dayOfWeek;
cout << "Please enter a day:" ;
cin >> dayOfWeek;
Sales(dayOfWeek);
system("Pause" );
return 0;
}
Last edited on Sep 23, 2014 at 11:15pm UTC
Sep 23, 2014 at 11:46pm UTC
Here is my sexy little program.. Im sure you can modify it to use with user input...whats the point of having us do all the work for you ;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
using namespace std;
int main()
{
const int NUM_OF_DAYS = 7;
int daysOfTheWeek[NUM_OF_DAYS];
for (int i = 0; i < NUM_OF_DAYS; i++) {
daysOfTheWeek[i] = i+1; //monday = 1, tues = 2.....sunday = 7
cout << i+1 << " " ; // line counter for output
if (daysOfTheWeek[i] == 1 || daysOfTheWeek[i] == 2) {
cout << "Discount" << endl;
}
else {
cout << "No Discount" << endl;
}
}
return 0;
}
Last edited on Sep 23, 2014 at 11:48pm UTC
Topic archived. No new replies allowed.