Hey everyone :)

I've been working on the first part of a program which I'm doing in 4 steps.
Here's the first step which I have completed:

Function inputAndValidate

This function inputs the time. The time should be entered as two separate values for the hours and minutes. Do not accept a value of less than 0 or more than 24 for the hour, or a value of less than 0 or more than 59 for the minutes.

Here's what I've done:

``````# include <iostream>
using namespace std;

void inputAndValidate (int entranceHourP, int entranceMinutesP, int exitHourP, int exitMinutesP)
{
do
{
cout << "Enter the hour and minutes the parking garage was entered (each separated by a space): " << endl;
cin >> entranceHourP >> entranceMinutesP;
}
while((entranceHourP > 0) || (entranceHourP < 24) && ((entranceMinutesP > 0) || (entranceMinutesP < 59)));
do
{
cout << "Enter the hour and minutes the parking garage was exited (each separated by a space): " << endl;
cin >> exitHourP >> exitMinutesP;
}
while((exitHourP > 0) || (exitHourP < 24) && ((exitMinutesP > 0) || (exitMinutesP < 59)));
}

int main()
{
for (int i = 1; i <= 10; i++)
{
int entranceHour, entranceMinutes;
int exitHour, exitMinutes;
int entranceTimeInMins, exitTimeInMins, minutesParked; // will use function to convertToMinutes later
int parkHours, parkedMinutes; // will use function to convertBackTime later (e.g. 570 min =  9h30)
float charge, totalCharges; // will use function calcCharge later
inputAndValidate(entranceHour, entranceMinutes, exitHour, exitMinutes);

system("pause");
return 0;
}
}``````

The compiler error I get using MS Visual Studio is this: The variable exitMinutes is being used without being initialised. ??

Anyone know why this is happening ?

It runs in devc++ tho ? Strange

It is not strange that it runs, however, the problem is that exitMinutes is uninitialized.

That means that when the value in exitMinutes is read, it could be ANYTHING. To see how that came about, consider your program.

In the main program you write `int exitMinutes;` which creates a variable called exitMinutes BUT does not actually set its value. So it is whatever that memory location previously had in it.

Then you have this:

``````void inputAndValidate(int entranceHourP, int entranceMinutesP,
int exitHourP, int exitMinutesP)
{
do
{
cout << "Enter the hour and minutes the parking garage was entered (each separated by a space): " << endl;
cin >> entranceHourP >> entranceMinutesP;
}
while((entranceHourP > 0) || (entranceHourP < 24) && ((entranceMinutesP > 0) || (entranceMinutesP < 59)));``````

Now under NORMAL operation, you pass a value to the function and then set the values before you test it. Fine, BUT what happens when you fail to enter numbers into cin, but instead, enter letters (for example).

The line `cin>>entranceHourP >> entranceMinutesP;` exits WITHOUT setting entranceMinutesP and as std::cin sets its own error state. THEN you use it uninitialized in the while loop. Hence the compiler warning/error.

Finally, the values you pass to the function are completely unused, you could just as easily write `void inputAndValidate()` and define your variables below.

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.