hello there, im given an assignment to basically prompt user to enter name, start time, end time, am or pm, and the pay rate and from that information i need to calculate number of hours worked and how much they get paid for the day, but im very stuck.


please excuse the completely unfinished program lol, but im just stuck, im want to getline for the start time and end time, but when im running the program, it skips the first (start) and only allows me to enter the end time, thus only getting my ending time. help! please

#include <iostream>
#include <string>

using namespace std;

int main()
{
string workerName; 
string start;                              // MY TWO VARIABLES 
string end;

int start_hour;
int start_min;
int end_hour;
int end_min;
float payRate;
float payCheck;
int totalHour;
int totalMin;
float overtime;
char amPm;
char amPm2;

cout << " Time Clock Program" << endl;

cout << "Enter worker name: " << endl;
cin >> workerName;



cout << "Enter Start time (hh:mm A/P): " << endl; // RIGHT HERE!
getline(cin, start);

cout << "Enter Stop time (hh:mm A/P): " << endl;
getline(cin, end);

cout << "Enter pay rate: " << endl;
cin >> payRate; 





/*
if (start_hour <= 12) 
{
if (end_hour <= 12)
totalHour = end_hour - start_hour;
}
else 
cout << "Invalid time! Time set to 0:00" << endl;

if (start_min <= 59)
{
if (end_min <=59)
totalMin = end_hour - end_min;
}
else 
cout << "Invalid time! Time set to 0:00" << endl;




cout << "Employee " << workerName << " has worked " << totalHour << " hours" << endl;
cout << overtime << endl;
cout << "The paycheck is for $" << payCheck << endl;
*/

cout << start << endl;
cout << end << endl;


return 0;


}

i want to be able to, when prompting the user and they enter 8:30 A i want to be able to store the '8' into a int, '30' into another int and 'A' into a char.

from the string i got from getline

edit: i figured that the problem was that i should of included a cin.ignore(); after [workerName] but still stuck on how to pull out each individual numbers and convert them to two ints and a char

Edited 5 Years Ago by fabricetoussain: n/a

try using stringstream class

#include <sstream>

...
std::string line = "8:30 AM";

int hour, minute;
std::string ampm;
char temp;
stringstream s(line);
s >> hour >> temp >> minute >> ampm;

Edited 5 Years Ago by Ancient Dragon: n/a

Perhaps try something like below...

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int start;
    int end;

    cout << " Time Clock Program" << endl;

    cout << "Enter Start time (hh:mm A/P): ";
    cin >> start;

    cout << std::endl << "Enter Stop time (hh:mm A/P): ";
    cin >> end;

    cout << std::endl << "start=[" << start << "] end=[" << end << "]" << std::endl;

    return 0;
}

You can try something like this: Have the user enter an int(for the hour),another int(minutes),a char(for ':') and another char('A' or 'P') with the SAME cin .i.e

cout<<"Enter time(hh : min A/P): ";
cin>>hour>>colonChar>>min>>amChar>>pmChar;

try using stringstream class

#include <sstream>

...
std::string line = "8:30 AM";

int hour, minute;
std::string ampm;
char temp;
stringstream s(line);
s >> hour >> temp >> minute >> ampm;

im very new to C++ and yet to go over stingstream classes, i've figured the getline problem, but do would you have a solution to picking out the numbers in the string and converting them to ints? such as the user enters " 8:30 A" so that i can put 8 into int start_hour, 30 start_min, and 'A' into char ap

edit: sorry i missed the last few post for some reason they didnt come up on my computer, thank you everyone!!

Edited 5 Years Ago by fabricetoussain: n/a

This article has been dead for over six months. Start a new discussion instead.