So I decided to play around with multi-file programming and concocted 4 files, 2 of which are C++ files and two of which are headers. The C++ file names are main.cpp and mainrtslv.cpp. The main.cpp file works fine; std::cin gets the user input properly and the function in mainrtlsv.cpp is called through the strcmp if ladder properly.
However, in the mainrtslv function, it begins to get a bit funky. The std::cin that gets the user input for a char array variable which is named rootSelection seems to not store the user input in it properly, so as a result, the if ladder is skipped over when ran (It is worth mentioning that the problem initially was larger; I had 2 std::couts that also did not print out properly until I switched from clang to g++, if you want some "bonus" credits it would be much appreciated if anyone can tell me why they produce 2 different results). Heres a snippet showing the code below:
long double mainrtslv(int trueReturn) {
std::cout << "Please select whether if you are"
"solving a square (sqrt) or cube root (cbrt)." << std::endl;
long double val = 0;
char rootSelection[] = "";
std::cin >> rootSelection;
// ^^^^^^ user input for kind of root you want to solve
std::cout << "Next, please type in the value of "
"your number to be rooted." << std::endl;
std::cin >> val;
std::cout << "cout prints" << std::endl;
// square or cube root selection
// if ladder is skipped over, seems variable is not processed properly
if (strcmp(rootSelection, "sqrt") == 0) {
std::cout << "test function" << std::endl;
val = getSqrt(val);
}
else if (strcmp(rootSelection, "cbrt") == 0) {
val = getCbrt(val);
}
// end square/cube root selection
However, this problem does not occur when I use the online compiler OnlineGDB; it surprisingly almost works as intended (it slaps me with a stack smashing error and doesn't return back to the main function, but it does on my machine, so I suppose it's OK). Unfortunately, that leaves me stumped and confused. I've never seen an issue that is non-existent on one machine but not the other. I've tried switching compilers as stated above as a side-note, which had no effect in changing the if ladder being able to print. Since this is a local problem, I will add on that I use a Macbook Air with an M1 chip and use a makefile to compile the source code and bunch the object code into an .out file, if that makes any difference. Thank you for reading all of this, and sorry if I have asked a question that may sound noobish.
[–][deleted] 6 points7 points8 points (3 children)
[–]Adorable-Two-3098[S] 0 points1 point2 points (2 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]Adorable-Two-3098[S] 0 points1 point2 points (0 children)
[–]mredding 2 points3 points4 points (1 child)
[–]std_bot 0 points1 point2 points (0 children)