Trying to write a code that asks the user to enter a number that corresponds wit
ID: 3586004 • Letter: T
Question
Trying to write a code that asks the user to enter a number that corresponds with a day in the week such that sunday=1, monday=2 ect. Then the program has to validate that the user used a number from 1-7. But i am not sure im i did wrong here! Please help
#include <iostream>
using namespace std;
int main()
{
int y,n;
int dayNum;
double Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday;
char response;
cout << "Enter a day of week value:";
cin >> dayNum >> endl;
while (dayNum > 7)
{
cout << dayNum << "is invalid" << endl;
cout << " Do you wish to continue?" << response ;
}
if (response == y)
{ cout << "Enter a day of the week value" << endl;
}
else if(response == n)
{ cout << "Goodbye! Thank you for using our program!" << endl;
}
if (dayNum == 0)
{ cout << "Sunday" << endl;
else if (dayNum == 1)
cout << "Monday" << endl;
else if (dayNum == 2)
cout << "Tuesday" << endl;
else if (dayNum == 3)
cout << "Wednesday" << endl;
else if (dayNum == 4)
cout << "Thursday" << endl;
else if (dayNum == 5)
cout << "Friday" << endl;
else if (dayNum == 6)
cout << "Saturday" << endl;
}
}
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int y,n;
int dayNum;
double Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday;
char response = 'y';
while(response == 'y') {
cout << "Enter a day of week value:"<<endl;
cin >> dayNum ;
while (dayNum < 1 || dayNum > 7)
{
cout << dayNum << "is invalid" << endl;
cout << "Enter a day of week value:"<<endl;
cin>> dayNum;
}
if(dayNum == 1) {
cout<<"Sunday"<<endl;
} else if(dayNum == 2) {
cout<<"Monday"<<endl;
} else if(dayNum == 3) {
cout<<"Tuesday"<<endl;
} else if(dayNum == 4) {
cout<<"Wednesday"<<endl;
} else if(dayNum == 5) {
cout<<"Thursday"<<endl;
} else if(dayNum == 6) {
cout<<"Friday"<<endl;
} else {
cout<<"Saturday"<<endl;
}
cout<<"Do you want to continure (y or n): "<<endl;
cin >> response;
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.