Stuck

---
Last edited on
Attempt to compile that produces:
43:12: warning: multi-character character constant [-Wmultichar]
49:12: warning: multi-character character constant [-Wmultichar]
 In function 'int convertToRegular(int&, int&)':
43:10: warning: overflow in implicit constant conversion [-Woverflow]
49:10: warning: overflow in implicit constant conversion [-Woverflow]
 In function 'void outputFunction(int&, int&)':
56:37: error: 'S' was not declared in this scope 


Warnings:
Line 40: S is a char.
Line 43/49: PM is not one character. Neither is AM.
Lines 51 and 38: function must return int, but you return S, which is char.

Error:
Name 'S' is unknown in function outputFunction.


Your main() does not call any of your other functions. Thus, your program would not produce any output, even if we fix outputFunction.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Lab Assignment #6, part 3: Time Conversion
//Program Name:
//Purpose of the program:
//Authors: Your and your partner’s names
//Date: write today’s date

#include <iostream>
#include <cmath>

using namespace std ;

void inputTime(int militaryTime, int& hours, int& mins) ;

int main() {

    
    int militaryTime = 0;
    cin >> militaryTime ;
    int regHours = 0;
    int regMins = 0;
    
    return 0;
}

void inputTime(int militaryTime, int& hours, int& mins)
{
    
    cout << " Please enter a time in military format: ";
    cin >> militaryTime;
    
    if (militaryTime < 0 ||  militaryTime > 2359) {
        cout << "Enter a valid time please." ;
    }
    
    else
    {
        hours = militaryTime / 100;
        mins = militaryTime % 100;
    }
    
    return  ;
}

int convertToRegular (int& hours, int& mins)
{
    string S;
   if (hours > 12)
   {
       S = 'PM';
       hours = hours - 12;
       
   }
   else
   {
       S = 'AM';
   }
    int time = hours * 100 + mins;
    return time;
}
void outputFunction (int &hours, int& mins)
{
    convertToRegular (hours, mins);
    cout << hours << ":" << mins << 'S' << endl;
}


I fixed some of the errors. What do you recommend to do next?
I have a question..

In inputTime(), if you want to the user to input the time, why would you pass parameters in the function? I would ask the user to input in int main().

So:

1
2
3
4
5
6
7
8
9
10
11
12
13
void inputTime(int militaryTime, int& hours, int& mins)
{
	if (militaryTime < 0 || militaryTime > 2359) {
		cout << "Enter a valid time please.";
	}
	else
	{
		hours = militaryTime / 100;
		mins = militaryTime % 100;
	}

	return;
}


Also, why did you pass hours and mins? aren't you going to read one integer and convert it to hours:minutes?

So,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void inputTime(int militaryTime)
{
        int hours = 0, mins = 0;
	if (militaryTime < 0 || militaryTime > 2359) {
		cout << "Enter a valid time please.";
	}
	else
	{
		hours = militaryTime / 100;
		mins = militaryTime % 100;
	}

	return;
}


Also, I think your method has a flaw.

If we divide 0550 by 100 to get the hours, which in this case is: 05 or 5, it will give us an answer of 5.5.

Which is wrong.

Also, do you HAVE to read an integer from the user? or can it be a string?
Last edited on
Here is the part of the code that is actually executed when you run the compiled executable:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std ;

int main() {
    int militaryTime = 0;
    cin >> militaryTime ;
    int regHours = 0;
    int regMins = 0;
    
    return 0;
}

Your code calls only one function: cin's operator>>

You do get a number into variable 'militaryTime' (unless input fails).
What should you do with that number?
Pass it as argument to some function?
Topic archived. No new replies allowed.