lundi 9 octobre 2017

C++ Program running, but not functioning correctly

I wanted to re-do my first project and apply the new things I learned in c++. My first project is to make a program that will have three variable, hours, minutes, and seconds.

The user will input an integer (cannot be a negative number) for hours, minutes, and seconds(For minutes, and seconds, the input cannot be a negative number, and the number cannot be more than 59). At the end of the program, the program will grab the 3 variables and add them together, to display a decimal number. Minutes, will be divided by 60, while seconds will be divided by 3600.

I wanted to use global variables or unary scopes, and prototype, and use the bool data type. The program run perfectly fine until it gets to the minutes/seconds section of the functions. The user input does not trigger the while, or if, and just continues the program until it asked me to restart it.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

//Global Types************
int hours;
int minutes;
int seconds;
bool valid=false;
char again = 'y';
//************************

//Protoypes***************
void Hours();
void Minutes();
void Seconds();
//************************


int main()
{
    while (again == 'y' || again == 'Y')
 {
    Hours();
    Minutes();
    Seconds();

    cout << hours + minutes + seconds << " hours" << endl;
    getchar();
    cout << "Restart Program? (y/n)" << endl;
    cin >> again;
  }
}


void Hours()
{
    cout << "Enter an input for hours: " << endl;
    cin >> ::hours;
    while (!::valid)
    {
        if (::hours < 0)
        {
            cout << "Input is incorrect, please input a positive integer." << endl;
            cin >> ::hours;
        }
        else
        {
            cout << "Input is correct" << endl;
            cout << ::hours << endl;
            ::valid = true;
        }
    }
}

void Minutes()
{
    cout<<"Enter an input for minutes: "<<endl;
    cin >> ::minutes;
    while (!::valid)
{
    if (::minutes<0||::minutes<=59)
    {
        cout<<"Input is incorrect, please input a positive integer, that is less than or equal to 59."<<endl;
        cin>>::minutes;
    }
    else 
    {
        cout<<"Input is correct"<<endl;
        ::minutes /= 60;
        ::valid = true;

    }
}
}


void Seconds() 
{
cout << "Enter an input for seconds: " << endl;
cin >> ::seconds;
while (!::valid) 
{
    if (::seconds < 0 || ::seconds <= 59)
    {
        cout << "Input is incorrect, please input a positive integer, that is less than or equal to 59." << endl;
        cin >> ::seconds;
    }
    else 
    {
        cout << "Input is correct" << endl;
        ::seconds /= 3600;
        ::valid = true;
    }

}
}

Aucun commentaire:

Enregistrer un commentaire