Fixed memory alloc

This commit is contained in:
garrett 2024-09-19 12:36:28 -04:00
parent 1c16d83027
commit db90a9f122
2 changed files with 54 additions and 31 deletions

View File

@ -1,9 +1,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
void header(int width, int margin, char lineChar, char headerTitle[50], char inputMessage[500]); void header(int width, int margin, char lineChar, char *headerTitle, char *inputMessage, int meslen);
int main(){ int main(){
@ -16,9 +18,10 @@ int main(){
// Char to store the gender with max length of 7 and a line string for formatting // Char to store the gender with max length of 7 and a line string for formatting
char gender[7]; char gender[7];
char message[] = "This program will calculate your BMI, Life Expectancy, and BSA(multiple methods) with just your weight(kg), height(cm), and gender.";
// Display Program Purpose // Display Program Purpose
header(80, 4, '*', "Purpose", "This program will calculate your BMI, Life Expectancy, and BSA(multiple methods) with just your weight(kg), height(cm), and gender."); header(80, 4, '*', "Purpose", message, (int)strlen(message));
// Read in weight, height, and gender from user // Read in weight, height, and gender from user
printf("Please enter your weight(kg): "); printf("Please enter your weight(kg): ");
@ -54,7 +57,7 @@ int main(){
scanf("%s", gender); scanf("%s", gender);
// Loop through all chars in gender and convert to lower case // Loop through all chars in gender and convert to lower case
for (int i = 0; i < strlen(gender); i++) { for (int i = 0; i < (int)strlen(gender); i++) {
gender[i] = tolower(gender[i]); gender[i] = tolower(gender[i]);
} }
@ -106,15 +109,13 @@ int main(){
return 0; return 0;
} }
void header(int width, int margin, char lineChar, char headerTitle[50], char inputMessage[500]) { void header(int width, int margin, char lineChar, char *headerTitle, char *inputMessage, int meslen) {
int TITLE_MARGIN = 3; int TITLE_MARGIN = 3;
int CONTENT_MARGIN = margin; int CONTENT_MARGIN = margin;
int rows = 1; int rows = 1;
char message[500];
strcpy(message, inputMessage);
char line[width + 1]; char line[width + 1];
char topLine[width + 1]; char topLine[width + 1];
@ -125,36 +126,53 @@ void header(int width, int margin, char lineChar, char headerTitle[50], char inp
line[i] = lineChar; line[i] = lineChar;
} }
line[width + 1] = '\0'; line[width] = 0;
strcpy(topLine, line); strcpy(topLine, line);
// Add header title // Add header title
for (int i = TITLE_MARGIN; i < (strlen(headerTitle) + TITLE_MARGIN); i++) { for (int i = TITLE_MARGIN; i < ((int)strlen(headerTitle) + TITLE_MARGIN); i++) {
topLine[i] = headerTitle[i - TITLE_MARGIN]; topLine[i] = headerTitle[i - TITLE_MARGIN];
} }
topLine[width + 1] = '\0'; topLine[width] = 0;
char *message = (char *)malloc((meslen + 100) * sizeof(char));
if (message == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
strncpy(message, inputMessage, meslen + 100);
int rowCheck = 1;
char tempMessage[500];
while(1) { while(1) {
if (message[(contentWidth * rows)] == '\0') { break; } if ((contentWidth * rows) > (int)strlen(message)) { break; }
if (message[(contentWidth * rows)] != ' ') { if (message[(contentWidth * rows)] != ' ') {
int i = 1; int i = 1;
int tempCounter = 0;
while (1) { while (1) {
if (message[(contentWidth * rows) - i] == ' ') { if (message[(contentWidth * rows) - i] == ' ') {
int start = ((contentWidth * rows) - i) + 1; int start = ((contentWidth * rows) - i) + 1;
char tempMessage[(int)strlen(message)];
int k = 0; int k = 0;
for (int j = 0; j < (int)strlen(message); j++) {
tempMessage[j] = ' ';
}
for (int j = start; j < strlen(message); j++) { for (int j = start; j < strlen(message); j++) {
tempMessage[k] = message[j]; tempMessage[k] = message[j];
tempCounter = k;
k++; k++;
} }
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
message[start + j] = ' '; message[start + j] = ' ';
} }
for (int j = 0; j < strlen(tempMessage); j++) { for (int j = 0; j < tempCounter + 2; j++) {
message[(contentWidth * rows) + 1 + j] = tempMessage[j]; message[(start + i) + j] = tempMessage[j];
if (tempCounter + 1 == j) {
message[(start + i) + j] = 0;
}
} }
break; break;
} }
@ -164,6 +182,7 @@ void header(int width, int margin, char lineChar, char headerTitle[50], char inp
rows++; rows++;
} }
printf("%s\n", topLine); printf("%s\n", topLine);
for (int i = 0; i < rows; i++) { for (int i = 0; i < rows; i++) {
@ -174,7 +193,7 @@ void header(int width, int margin, char lineChar, char headerTitle[50], char inp
} }
for (int j = 0; j < contentWidth; j++) { for (int j = 0; j < contentWidth; j++) {
if (charCount == 0 && (message[j + (i * contentWidth)] == ' ')) {j++;} if (charCount == 0 && (message[j + (i * contentWidth)] == ' ')) {j++;}
if (j + (i * contentWidth) == (int)strlen(message)) { if (!(message[j + (i * contentWidth)])) {
break; break;
} }
printf("%c", message[j + (i * contentWidth)]); printf("%c", message[j + (i * contentWidth)]);
@ -190,4 +209,6 @@ void header(int width, int margin, char lineChar, char headerTitle[50], char inp
} }
printf("%s\n", line); printf("%s\n", line);
free(message);
} }

View File

@ -1,18 +1,18 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
void header(int width, int margin, char lineChar, char *headerTitle, char *message); void header(int width, int margin, char lineChar, char *headerTitle, char *inputMessage, int meslen);
int main() { int main() {
char message[] = "Testing this out some more. okay i think its working now DHOWEIFH HDSOIHTOHNDS HSODUIHT. This program will calculate your life expecantacy and bmi and BSA by just your weight(kg), height(cm), and gender."; char message[] = "Testing this out some more. okay i think its working now DHOWEIFH HDSOIHTOHNDS HSODUIHT. This program will calculate your life expecantacy and bmi and BSA by just your weight(kg), height(cm), and gender.";
printf("%d\n", (int)strlen(message)); header(80, 4, '*', "Purpose", message, (int)strlen(message));
header(80, 4, '*', "Purpose", message);
return 0; return 0;
} }
void header(int width, int margin, char lineChar, char *headerTitle, char *message) { void header(int width, int margin, char lineChar, char *headerTitle, char *inputMessage, int meslen) {
int TITLE_MARGIN = 3; int TITLE_MARGIN = 3;
int CONTENT_MARGIN = margin; int CONTENT_MARGIN = margin;
@ -29,34 +29,36 @@ void header(int width, int margin, char lineChar, char *headerTitle, char *messa
line[i] = lineChar; line[i] = lineChar;
} }
line[width + 1] = '\0'; line[width] = 0;
strcpy(topLine, line); strcpy(topLine, line);
// Add header title // Add header title
for (int i = TITLE_MARGIN; i < (strlen(headerTitle) + TITLE_MARGIN); i++) { for (int i = TITLE_MARGIN; i < ((int)strlen(headerTitle) + TITLE_MARGIN); i++) {
topLine[i] = headerTitle[i - TITLE_MARGIN]; topLine[i] = headerTitle[i - TITLE_MARGIN];
} }
topLine[width + 1] = '\0'; topLine[width] = 0;
printf("%d\n", (int)strlen(message));
int rowCheck = 1; char *message = (char *)malloc((meslen + 100) * sizeof(char));
if (message == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
strncpy(message, inputMessage, meslen + 100);
while(1) { while(1) {
printf("%d > %d = %d\n", contentWidth * rows, (int)strlen(message), (contentWidth * rows) > (int)strlen(message));
if ((contentWidth * rows) > (int)strlen(message)) { break; } if ((contentWidth * rows) > (int)strlen(message)) { break; }
if (message[(contentWidth * rows)] != ' ') { if (message[(contentWidth * rows)] != ' ') {
int i = 1; int i = 1;
int tempCounter = 0; int tempCounter = 0;
while (1) { while (1) {
printf("previous char = %c\n", message[(contentWidth * rows) - i]);
if (message[(contentWidth * rows) - i] == ' ') { if (message[(contentWidth * rows) - i] == ' ') {
int start = ((contentWidth * rows) - i) + 1; int start = ((contentWidth * rows) - i) + 1;
char tempMessage[(int)strlen(message)]; char tempMessage[(int)strlen(message)];
printf("check char = %c\n", message[contentWidth * rows]);
printf("start = %d\n", start);
printf("char after start = %c\n", message[start]);
int k = 0; int k = 0;
for (int j = 0; j < (int)strlen(message); j++) { for (int j = 0; j < (int)strlen(message); j++) {
tempMessage[j] = ' '; tempMessage[j] = ' ';
@ -83,8 +85,6 @@ void header(int width, int margin, char lineChar, char *headerTitle, char *messa
rows++; rows++;
} }
printf("%c\n", message[230]);
printf("%s\n",message);
printf("%s\n", topLine); printf("%s\n", topLine);
@ -112,4 +112,6 @@ void header(int width, int margin, char lineChar, char *headerTitle, char *messa
} }
printf("%s\n", line); printf("%s\n", line);
free(message);
} }