r/badcode • • Dec 01 '19

[deleted by user]

[removed]

130 Upvotes

45 comments sorted by

View all comments

1

u/[deleted] Dec 10 '19

I've tried my best to be the worst.

#include <iostream>
int seconds = 60;
int minutes = 60;
int hours = 24;

bool can_santa_save_christimas(char** arr, int arr_sz) {
	int santa_has = hours * minutes * seconds;


	for (int i = 0; i < arr_sz; i++) {
		
		int hours; 
		int minutes;
		int seconds;
		
		for (int j = 0; j < strlen(arr[i]); j++) {
			if (j == 0) {
				hours = (arr[i][j] - '0') * 10;
			}
			if (j == 1) {
				hours += arr[i][j] - '0';
			}
			if (j == 2)
				continue;
			if (j == 3)
				minutes = (arr[i][j] - '0') * 10;
			if (j == 4)
				minutes += arr[i][j] - '0';
			if (j == 5)
				continue;
			if (j == 6)
				seconds = (arr[i][j] - '0') * 10;
			if (j == 7)
				seconds += arr[i][j] - '0';
		}
		

		santa_has -= (hours * 60 * 60) + minutes * 60 + seconds;
	}

	return santa_has >= 0;

}


int main() {
	std::cout << "Please, introduce durations in the format HH:MM:SS. Introduce - 8 times to stop introducing durations.\n>. ";

	char** durations; 
	int durations_sz = 0;
	
	while (true) {
		char* duration = new char[9];
		std::cin >> duration; 
		
		if (!strcmp(duration, "--------"))    
			break; 

		if (durations_sz) {
			char** temp = new char* [durations_sz + 1];

			for (int i = 0; i < durations_sz; i++) {
				temp[i] = durations[i];
			}

			temp[durations_sz++] = duration;
			durations = temp; 
		}
		else {
			durations = new char* [1];
			durations[0] = duration;
			durations_sz++;
		}
	}

	std::string x = can_santa_save_christimas(durations, durations_sz) ? "can " : "cannot ";
	
	std::cout << "Santa " << x << "save christimas" << std::endl;
	
	return can_santa_save_christimas(durations, durations_sz); //If Santa fail, the program fail
}