Programming Questions
int CountingMinutesI(string str) { // code goes here // First create two substrings from the original string by finding the position // of the hyphen int hyphen = str.find('-'); string time1 = str.substr(0,hyphen); string time2 = str.substr(hyphen+1); int hour1 = 0; int hour2 = 0; int minutes1 = 0; int minutes2 = 0; int totalMinutes = 0; //Traverse through the first substring to find the total number of minutes that //have passed for time #1 for(int i = 0; i < time1.length(); i++){ //The number that appears after the colon is the number of minutes if(time1[i] == ':'){ minutes1 = time1[i+1]; } //If time is in pm, then the number of hours is increased by 12 if(time1[i] == 'p'){ if(time1[0] < 12){ hour1 = (12 + time1[0]); } else{ hour1 = 12; } } //If time is in am, then the number of hours remains the same else if(time1[i] == 'a'){ if(time1[0] < 12){ hour1 = time1[0]; } else{ hour1 = 0; //12:00 am = 0 minutes (starting point) } } } //Doing exactly the same thing we did with time #1 for time #2 for(int i = 0 ; i < time2.length(); i++){ if(time1[i] == ':'){ minutes2 = time2[i+1]; } if(time2[i] == 'p'){ if(time2[0] < 12){ hour2 = (12 + time2[0]); } else{ hour2 = 12; } } else if(time2[i] == 'a'){ if(time2[0] < 12){ hour2 = time2[0]; } else{ hour2 = 0; } } } //Now that we have collected the amount totalMinutes = (hour2 * 60 + minutes2) - (hour1 * 60 + minutes1); if(totalMinutes < 0) totalMinutes += 1440; return totalMinutes; }
int hour1, minutes1; char sym; istringstream iss(time1); time1 >> hour1 >> sym >> minutes1; // assert(sym == ':')
Qlogin
answered on 12/03/15
Qlogin
answered on 12/03/15
minutes1 = time1[i+1];you assign character ASCII code ('char' type) to 'int' variable.
int a = 1; // a = 1 int b = '1'; // b = 49
Qlogin
answered on 12/03/15
if(time2[0] < 12) { // always false cause digit characters have codes from 48 to 57 hour2 = (12 + time2[0]); }To convert digit character to integer you can use:
int charToInt(char ch) { return ch - '0'; }Secondly, in the same places you use only one character for hours and minutes. When 'time1' is equal to "10:35":
hour1 = charToInt(time1[0]) * 10 + charToInt(time1[1])
Qlogin
answered on 12/03/15
Gamer4Life
answered on 12/04/15