I need help with if and else statements.

Duo

Registered
I can't get the message to show up in my program. You see, my program is supposed to tell you what day it is. For example;

The program will ask "Enter a day and a hour"

A stands for Saturday, so when I enter in A the cout should display Today is saturday.

Also, if someone inserts 25 for hour then the program cout will display it as "Invalid"

I'm also supposed to do this for the other days. M stands for Monday and T stands for Tuesday, S stands for Sunday and etc. Also on weekdays if someone inserts a specific time like 17, the cout should display "out at work" or something like that, but that isn't important. My current problem is that I can't get my cout to display the right message. Instead of saying "Today is Saturday" it just shows A..... Also, my time message doesn't get displayed at all.

#include <iostream.h>

int main()

{

int day, hour;

char S, M, T, W, R, F, A, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday;

S = Sunday;

M = Monday;

T = Tuesday;

W = Wednesday;

R = Thursday;

F = Friday;

A = Saturday;

cout << "C Program 3 CSCE 1020.002" << endl

<< "Corey Jones Cjtx6362@yahoo.com" << endl << endl;

cout << "Enter the day and time." << endl << endl;

cout << "What day is it? ";
cin >> day, hour;
if ( day == 'A' && hour == 1-24) {
cout << "Today is Saturday." << endl; }
else if (day == 'A' && hour >= 25) {
cout << "Time is invalid" << endl; }
 
The condition "hour == 1-24" is read as "hour == -23" because '-' is the subtraction operator. What you want is "hour>=1 && hour <= 23".
 
That's some pretty horrible code right there:

You're defining a series of meaningless variables (all those chars), then assigning them all to null variables. Plus, you're defining 'day' as an integer, then passing it a character.

You don't need to have all that stuff there at all. If your task involved day being a letter, it should be of type "char", and hour should be an "int".

Those are all the variables you need. One character, one integer.

Then you need a switch/case statement (or six if/else statements) to analyse the input and output accordingly.

The way you're coding makes sense if you're explaining it in plain English, but it's non-sensical as code. deliciousMammal's comment is true, too. You really can't define ranges in an if/else statement.
 
I would look into arrays, and the 'switch' control statement. You're clearly a student, which is cool. These will be helpful things to learn.
 
Code:
#include <iostream.h>

int main (int argc, char * const argv[]) {
	int hour;
	char day;

	cout << "C Program 3 CSCE 1020.002" << endl << "Corey Jones Cjtx6362@yahoo.com" << endl << endl
	<< "Enter the day and time." << endl << endl
	<< "... with some help from 'http://www.macosx.com'," << endl
	<< "(compiled with 'Xcode' v. 2.2.1.)." << endl << endl;
	
	cout << "Enter 'day': ";
	cin >> day; // Obtain 'integer' value.

	cout << "Enter 'hour': ";
	cin >> hour; // Obtain 'char' value.
	
	cout << endl << "Day is: '";
		
	switch (day){ // Perform 'switch' test on 'day's entered value.
		case 'S': cout << "Sunday"; break;
		case 'M': cout << "Monday"; break;
		case 'T': cout << "Tuesday"; break;
		case 'W': cout << "Wednesday"; break;
		case 'R': cout << "Thursday"; break;
		case 'F': cout << "Friday"; break;
		case 'A': cout << "Saturday"; break;
		default: cout << "Warning Will Robinson!!!"; break;
	}
		
	cout << "'." << endl;
		
	if (hour >= 0 && hour <= 23) {// Perform 'if' test on 'hour's entered value.
		cout <<  "Time: '" << hour << "' is valid." << endl << endl;
	}
	else{
		cout <<  "Time: '" << hour << "' is invalid." << endl << endl;
	}
}
 
Back
Top