Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Something is wrong, and I have spent hours on this

+3 votes
asked Nov 19 by Peter Fischi (320 points)
This is seemingly very simple, but I cannot think of anything wrong with it. https://onlinegdb.com/Mc9ni9Qmf in lines 325 - 636 of algebra.h. when you go through the menu, choose do_algebra, then 1_variable, then equation, then enter 15 + 45 = x, or any other operator. follow the directions, and it will output x=0. 15 + 45 is not 0. I have isolated that something is wrong when setting al_1_solution at the end of each operation module, using debug. the datatypes line up, both are long double. I have tried everything I could think of. Also, another module of my calculator, the detect_simple_operation, uses the same concept, yet it works fine. this is puzzling. (located in functions.h @ lines 96 - 250).

If someone could help me that would be great, I am self taught at c++, so I might have missed something critical...

thanks,

Peter Fischi

1 Answer

+2 votes
answered Nov 20 by Peter Minarik (40,320 points)
selected Nov 20 by Peter Fischi
 
Best answer

The Solution For Your Question

Line 322 in algebra.h declares a local variable:

322: long double al_1_solution = 0;

, which hides the original al_1_solution variable declared in line 58:

58: long double al_1_solution = 0;

This is wrong, you should simply remove line 323 and your problem is solved.

What You Didn't Ask For

Some tips to make your code more maintainable
  • Header files are there for function declarations. Function definitions (the body of the functions) should go into the corresponding .c or .cpp source file. (Except for templates, which only can go to the header files.)
  • Your variable names are very confusing. If every variable has the same prefix, then the whole prefix is meaningless. Keep your variable names smart.
  • Do not have multiple instructions in a single line (e.g. algebra.h: 57)
  • You can use existing functions, such as isdigit() (from <cctype>) to determine if a character is actually a digit. You don't need to reinvent the wheel every time. ;)
  • You can create functions and reuse them, e.g. to extract certain data from a string. No need to keep repeating the same few lines of code over and over again.
  • You can use a switch-case, probably more readable than a series of if-else cascades.
Good luck, keep on coding! :)

commented Nov 20 by Peter Fischi (320 points)
Thank you, I have definitely learned a lot!
commented Nov 21 by Peter Fischi (320 points)
also, I could not fit all of the function bodies into one file, GDB has a line limit apparently.
commented Nov 21 by Peter Minarik (40,320 points)
What I meant is that you should have 2 files per "package". E.g. algebra.h and algebra.cpp

Basically what you'd have now (algebra.h) should be renamed to algebra.cpp and a new file created: algebra.h with the following content:

#pragma once
#include <string>
std::string posc(std::string suit[], std::string fc, int sz, int termpos_posinterm_both);
void algebra_1();
void algebra_2();
void algebra_3();

And that's all. The header (.h) file should only contain function declarations (prototypes), type definitions, macros, etc. The actual implementations should go into the corresponding source (.cpp) files.

Then add the
#include "algebra.h"
in your algebra.cpp and any other file where you would like to access these functions.

It wouldn't work immediately, as you'll be missing some identifiers, e.g. colours, in your algebra.cpp. So you'll need to separate those to header and source files too. But in the end, everything will fit together just like a nice puzzle when all the pieces are pieces correctly.

The benefit is that you can include only the prototypes, no need to copy the functions over and over everywhere in your project. By the way, this would sooner or later end up in a compilation error as you can only have one function definition. Including header files multiple times is not a problem as
a) they only contain prototypes -- you can declare a prototype multiple times
b) #pragma once ensures that you won't actually include the file multiple times in the same compilation process if you've already included it.

Right now really you have only one giant file. You keep including all the files (hierarchically) from your main.cpp. All the #include really does is to copy the full content of the file to the location of the #include.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...