help on a program

Good Evening-
Needing help on a program. Any suggestions would be greatly appreciated. Please see the following:

I am struggling to get the correct output..here's the code I have written:

13 #include <iostream>
14 using namespace std;
15
16 int main()
17 {
18 int input, hnumber, midnight, noon;
19 string days;
20
21 input = 0-167;
22 hnumber = input % 24;
23 midnight = 24;
24 noon = 12;
25
26 cout<<"Enter hours:";
27 cin>> input;
28
29 if (input <= 23) cout<<"Monday\n";
30 else if (input <= 47) cout<<"Tuesday\n";
31 else if (input <= 71) cout<<"Wednesday\n";
32 else if (input <= 95) cout<<"Thursday\n";
33 else if (input <= 119) cout<<"Friday\n";
34 else if (input <= 143) cout<<"Saturday\n";
35 else if (input <= 167) cout<<"Sunday\n";
36
37
38 if (hnumber == 0) cout<<"midnight\n";
39 else if (hnumber == 12) cout<<"noon\n";
40 else if (hnumber >= 0 && hnumber <= 12) cout<<"am\n";
41 else if (hnumber >= 12 && hnumber <= 12)
42 cout<<"pm\n";
43
44
45 cout<< "Result:" << days << " ," << hnumber << endl;
46
47 cout<< "Program Ends"<< endl;
48 }

The output I am getting is:

Enter hours:23
Monday
Result: ,-23
Program Ends

I am supposed to get the following:

Enter hours:23
Result: Monday, 11 pm
Program Ends

Thanks!

Some questions to help you to the answers ...

1. Where are you setting the value of 'days'?
2. Where are you setting the value of hnumber? What will it be?

Cheers,
Jim
I guess that's where I am screwed up.

1. I thought the string days would go there because that's where I need the day of the week to go.
2.I thought the value for hnumber was input % 24

Thanks

Josh
Did you sort it out?

I could reword my questions as statements ...

1. Your use of 'days' in line 45 is fine - you just aren't setting the value of 'days' anywhere (like in line 30, instead of outputting it at that point)

2. You're setting 'hnumber' from 'input' (line 22, which is correct math, BTW) before you've got the value of 'input' from the user at line 27. Move hnumber's assignment after that point.

Jim
Not quite yet. I really appreciate your tips but i still can't get it to compile. If you got a sec could you please take a look at the changes. Thanks!


16 int main()
17 {
18 int input, hnumber, midnight, noon;
19 int days;
20
21 input = 0-167;
22
23 cout<<"Enter hours:";
24 cin>> input;
25
26 if (input <= 23) days ="Monday\n";
27 else if (input <= 47) days ="Tuesday\n";
28 else if (input <= 71) days ="Wednesday\n";
29 else if (input <= 95) days ="Thursday\n";
30 else if (input <= 119) days = "Friday\n";
31 else if (input <= 143) days ="Saturday\n";
32 else if (input <= 167) days ="Sunday\n";
33
34 hnumber = input % 24;
35 midnight = 24;
36 noon = 12;
37
38 if (hnumber == 0) cout<<"midnight\n";
39 else if (hnumber == 12) cout<<"noon\n";
40 else if (hnumber >= 0 && hnumber <= 12) cout<<"am\n";
41 else if (hnumber >= 12 && hnumber <= 12)
42 cout<<"pm\n";
43
44
45 cout<< "Result:" << days << " ," << hnumber << endl;
46
47 cout<< "Program Ends"<< endl;
48 }
At a quick look I think this would compile fine, what error are you getting?

The only problem I can spot is on line 41
else if (hnumber >= 12 && hnumber <= 12)
which is the same as
else if (hnumber == 12)
maybe it should be <=24 for line 41?
Thanks for getting back to me..here is the error message i am receiving:


jhprogram3closer.cpp:25: error: invalid conversion from `const char*' to `int'
jhprogram3closer.cpp:26: error: invalid conversion from `const char*' to `int'
jhprogram3closer.cpp:27: error: invalid conversion from `const char*' to `int'
jhprogram3closer.cpp:28: error: invalid conversion from `const char*' to `int'
jhprogram3closer.cpp:29: error: invalid conversion from `const char*' to `int'
jhprogram3closer.cpp:30: error: invalid conversion from `const char*' to `int'
jhprogram3closer.cpp:31: error: invalid conversion from `const char*' to `int'
oh yes.. I see.
days should be a std::string not an int if you want to put a text string into it.
I'm not super familiar with that...like this?


26 if (input <= 23)std::string days ="Monday\n";
27 else if (input <= 47)std::string days ="Tuesday\n";
28 else if (input <= 71) std::string days ="Wednesday\n";
29 else if (input <= 95) std::string days ="Thursday\n";
30 else if (input <= 119) std::string days = "Friday\n";
31 else if (input <= 143) std::string days ="Saturday\n";
32 else if (input <= 167) std::string days ="Sunday\n";
33
34 hnumber = input % 24;
35 midnight = 24;
36 noon = 12;
37
38 if (hnumber == 0) cout<<"midnight\n";
39 else if (hnumber == 12) cout<<"noon\n";
40 else if (hnumber >= 0 && hnumber <= 12) cout<<"am\n";
41 else if (hnumber >= 12 && hnumber <= 12)
42 cout<<"pm\n";
43
44
45 cout<< "Result:" << days << " ," << hnumber << endl;
Morning!

Not quite, no - your original code had 'days' defined as string days;, which you've changed to an int. If you change it back to be a string, things should be fine ...

1
2
3
4
5
6
7
string days;

...

if (input <= 23) days = "Monday\n";

...


Jim
Topic archived. No new replies allowed.