Add while true loop and switch statement in main.

Change CMake generator to MinGW Makefiles.
Update gitignore.
This commit is contained in:
kelson8 2024-11-16 23:48:26 -05:00
parent 124bfb668e
commit 7da1908417
Signed by: kelson8
GPG Key ID: 3D738D9DD7239E13
3 changed files with 155 additions and 39 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
# New # New
build.tar build.tar
src/old
# ---> C # ---> C
# Prerequisites # Prerequisites

View File

@ -4,6 +4,8 @@
"pointer_test.h": "c", "pointer_test.h": "c",
"struct_test.h": "c", "struct_test.h": "c",
"stdio.h": "c", "stdio.h": "c",
"stdlib.h": "c" "stdlib.h": "c",
} "mysql.h": "c"
},
"cmake.generator": "MinGW Makefiles"
} }

View File

@ -1,4 +1,4 @@
#include <stdio.h> #include <stdio.h>
// #include <stdint.h> // #include <stdint.h>
#include <inttypes.h> #include <inttypes.h>
@ -11,7 +11,6 @@
#include "mysql_test.h" #include "mysql_test.h"
// Test // Test
// https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c // https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c
#ifdef _WIN32 #ifdef _WIN32
@ -20,7 +19,6 @@
#include "stdbool.h" #include "stdbool.h"
// To setup this project on windows // To setup this project on windows
// 1. Install CMake // 1. Install CMake
// 2. Install Ninja with winget in windows 11: "winget install --id=Ninja-build.Ninja -e" // 2. Install Ninja with winget in windows 11: "winget install --id=Ninja-build.Ninja -e"
@ -50,14 +48,24 @@
// TODO Look into getting current directory in Windows and Linux // TODO Look into getting current directory in Windows and Linux
// https://stackoverflow.com/questions/27585930/get-the-current-working-directory-in-c-on-windows // https://stackoverflow.com/questions/27585930/get-the-current-working-directory-in-c-on-windows
// This might be useful for all the preprocessors for each operating system:
// https://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor
// TODO Move this into a different project. // TODO Move this into a different project.
#ifdef _WIN32 #ifdef _WIN32
void guiTest(){ void guiTest()
{
// WIN32_
} }
#endif #endif //_WIN32
#ifdef __linux__
// TODO Test this
#include <pwd.h>
#endif //__linux__
// #define _FLIP_BOOLS // #define _FLIP_BOOLS
// Hmm, very evil // Hmm, very evil
@ -69,40 +77,166 @@ void guiTest(){
// #define false (rand() % 2) // #define false (rand() % 2)
#endif //_FLIP_BOOLS #endif //_FLIP_BOOLS
// https://www.reddit.com/r/C_Programming/comments/jjdl7s/difference_between_char_and_char/
// char welcomeMsg[] = "Welcome to KCNet";
const char *kcnetMsg = "[KCNet]:";
const char *welcomeMsg = "Welcome to KCNet";
const char *errorMsg = "Error:";
void flip_bools()
{
#ifdef _FLIP_BOOLS
bool testb = false;
if (testb)
{
printf("True");
}
else
{
printf("False");
}
#else
// Print the message that this isn't enabled
printf("%s %s This is not enabled!\n", kcnetMsg, errorMsg);
#endif //_FLIP_BOOLS
}
// TODO Look into making a gtk C program // TODO Look into making a gtk C program
// https://www.geeksforgeeks.org/how-to-create-gui-in-c-programming-using-gtk-toolkit/ // https://www.geeksforgeeks.org/how-to-create-gui-in-c-programming-using-gtk-toolkit/
// Print out the memory addresses for each item in an array. // Print out the memory addresses for each item in an array.
void array_test() { void array_test()
{
// https://www.tutorialspoint.com/cprogramming/c_memory_address.htm // https://www.tutorialspoint.com/cprogramming/c_memory_address.htm
// Int array // Int array
int numbers[5] = {1,2,3,4,5}; int numbers[5] = {1, 2, 3, 4, 5};
int i = 0; int i = 0;
printf("numbers = %p\n", numbers); printf("numbers = %p\n", numbers);
do { do
{
printf("numbers[%u] = %p\n", i, (void *)&numbers[i]); printf("numbers[%u] = %p\n", i, (void *)&numbers[i]);
i++; i++;
} while (i < 5); } while (i < 5);
printf("sizeof(numbers) = %lu\n", sizeof(numbers)); printf("sizeof(numbers) = %lu\n", sizeof(numbers));
} }
// Removed from path in windows: C:\mingw64\bin // Removed from path in windows: C:\mingw64\bin
// https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm // https://www.tutorialspoint.com/cprogramming/c_ternary_operator.htm
void check_even_odd(int num) { void check_even_odd(int num)
(num %2 == 0) ? printf("%d is even\n", num) : printf("%d is odd\n", num); {
(num % 2 == 0) ? printf("%d is even\n", num) : printf("%d is odd\n", num);
// return;
} }
void user_input()
{
int value; // Value for the switch loop
int number_to_check; // Number to check for even/odd check in the loop
int main() { // Pointer test
char welcomeMsg[] = "Welcome to KCNet"; int i = 22222; // Random value
// printf(welcomeMsg); int *i_ptr = &i; // Pointer to that random value
//
uint16_t test = -2; while (true)
{
// for (;;){
// Oops this was outside of the loop and breaking it...
// Well I fixed that part of it.
printf("\n%s What would you like to do? "
"1 (Array Test), "
"2 (Check Even or Odd), "
"3 (Print welcome msg), "
"4 (Flip bools {if enabled}), "
"5 (Read file), "
"6 (Print struct), "
"7 (Math test), "
"8 (Pointer test), "
"9 (Get User Profile): ",
kcnetMsg);
scanf("%d", &value);
switch (value)
{
case 1:
array_test();
break;
case 2:
printf("Enter a number to check: ");
scanf("%d", &number_to_check);
check_even_odd(number_to_check);
break;
case 3:
printf("%s\n", welcomeMsg);
break;
case 4:
flip_bools();
break;
case 5:
read_file("test.txt");
break;
case 6:
print_struct();
break;
case 7:
// printf("Test #7");
mathFunctions();
break;
case 8:
// printf("Test #8");
pointer_madness(i_ptr);
break;
case 9:
// printf("Test #8");
#ifdef _WIN32
printf("%s User profile path: %s\n", kcnetMsg, getenv("USERPROFILE"));
#endif // _WIN32
#ifdef __linux__
// https://stackoverflow.com/questions/1451825/c-programming-printing-current-user
// https://stackoverflow.com/questions/41976889/c-user-name-in-linux-get-current-user-name
// TODO Fix this
struct passwd *p = getpwuid(getuid());
if (p != NULL)
{
printf("%s %s\n", kcnetMsg, p->pw_name);
}
else
{
printf("%s The user seems to be invalid.", errorMsg)
}
#endif //__linux__
break;
default:
printf("Invalid option %d", &value);
break;
}
}
// printf("Enter a number: ");
// scanf("%d", &my_num);
// printf("You entered the number: \"%d\"", my_num);
}
int main()
{
// printf("%s\n", welcomeMsg);
// uint16_t test = -2;
user_input();
// TODO Fix this to work.
// connect_mysql();
// array_test(); // array_test();
// for (int i=0; i<5; i++) { // for (int i=0; i<5; i++) {
@ -113,7 +247,6 @@ int main() {
// print_struct(); // print_struct();
// read_file("test.txt"); // read_file("test.txt");
// This doesn't work // This doesn't work
// uint16_test(); // uint16_test();
@ -124,26 +257,6 @@ int main() {
// https://stackoverflow.com/questions/12120426/how-do-i-print-uint32-t-and-uint16-t-variables-value // https://stackoverflow.com/questions/12120426/how-do-i-print-uint32-t-and-uint16-t-variables-value
// printf("Number: %" PRIu32, test); // printf("Number: %" PRIu32, test);
// mathFunctions();
// print_struct();
int i = 22222;
int* i_ptr = &i;
// pointer_madness(i_ptr);
connect_mysql();
#ifdef _FLIP_BOOLS
bool testb = false;
if (testb) {
printf("True");
} else {
printf("False");
}
#endif //_FLIP_BOOLS
// printf_example(); // printf_example();
return 0; return 0;
} }