ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
char helper[4];
sscanf(argv[1],"%s",&helper);
char helptest[4] = "help";
printf("%s\n", helptest);
exit(0);
int compare = strcmp(helper, helptest);
if ( compare = 0 ) {
print_help();
} else if ( argc != 4 ) {
printf ("usage : %s num | op | num\n", argv[0]);
return 0;
}
I was adding this to my c to print a help page if needed.
I added the prinf and exit(0) to test the output because the if else wasn't working. as it turns out the helptest is coming back with this result
Code:
helphelp
So clearly not matching the test rules. Why is that and or how can I fix that?
Of course changing the compare to check for a 1 instead of a 0 makes it only print the help and never do anything beyond that. Stuck well.
entire source
Code:
/*
* author - Jason Gibson
* version - 1.01
* a simple non decimal point calculator
*
* this source code is licensed under the GPLv3 or any previous version.
* a copy of this license is found in the proper git location or can be found
* online at https://www.gnu.org/licenses/gpl-3.0.en.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// functions declaration
int print_func(int solution);
int print_help();
int math_func(int num1, char op, int num2);
// main funtion
int main( int argc, char *argv[]) {
// exit if wrong number of arguements supplied
/*
if ( argc != 4 ) {
printf ("usage : %s num | op | num\n", argv[0]);
return 0;
}
*/
char helper[4];
sscanf(argv[1],"%s",&helper);
char helptest[4] = "help";
printf("%s\n", helptest);
exit(0);
int compare = strcmp(helper, helptest);
if ( compare = 0 ) {
print_help();
} else if ( argc != 4 ) {
printf ("usage : %s num | op | num\n", argv[0]);
return 0;
}
int num1, num2, ret;
char op;
sscanf(argv[1],"%i",&num1);
sscanf(argv[3],"%i",&num2);
sscanf(argv[2],"%c",&op);
// send variables to math_function
ret = math_func(num1, op, num2);
}
// print function
int print_func(int solution) { // print answer as needed
printf("answer is %i\n", solution);
return 0;
}
int print_help() {
printf("This program does simple non decimal point math\n");
printf("Pattern is Number Operator Number\n");
printf("Examples:\n");
printf("Addition = 4 + 4\n");
printf("Subtraction = 4 - 4\n");
printf("Multiplication = 4 \"*\" 4\n");
printf(": you need to quote the operator for multiplication with\n");
printf("a typical shell\n");
printf("Division = 4 / 2\n");
printf(": it will calulate a remainder as needed\n");
exit(0);
}
// math function
int math_func(int num1, char op, int num2) {
int solution;
int math_ret;
switch (op) {
case '+': // addition working
solution = num1 + num2;
break;
case '-': // subtraction working
solution = num1 - num2;
break;
case '*': // need to quote * from the shell due to expansion - working
solution = num1 * num2;
break;
case '/': // division working
if ( num1 == 0 || num2 == 0 ) { // prevent divide by 0
printf("you cannot divide by zero!\n");
exit(1);
} else if ( num1 % num2 != 0 ) { // run solution if odd remainder
int remainder = num1 % num2;
solution = num1 / num2;
printf("answer is %i with a remainder of %i\n", solution, remainder);
return 0;
}
solution = num1 / num2; // solution if no remainder
break;
}
math_ret = print_func(solution);
return 0;
}
Last edited by jmgibson1981; 01-07-2023 at 08:54 PM.
lq-calc.c: In function 'main':
lq-calc.c:27:7: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
27 | if ( compare = 0 ) {
| ^~~~~~~
Thank you. I was able to simplify it just now down to just this from what I started with.
Code:
if ( strcmp(argv[1],"help") == 0 ) {
print_help();
}
int print_help() {
printf("This program does simple non decimal point math\n");
printf("Pattern is Number Operator Number\n");
printf("Examples:\n");
printf("Addition = 4 + 4\n");
printf("Subtraction = 4 - 4\n");
printf("Multiplication = 4 \"*\" 4\n");
printf(": you need to quote the operator for multiplication with\n");
printf("a typical shell\n");
printf("Division = 4 / 2\n");
printf(": it will calulate a remainder as needed\n");
exit(0);
}
The C if statement is working properly, otherwise the language could not be used at all. (That is just nonsense).
The -Wall (and other compiler flags) are used to detect common mistakes like this one (= is assignment, == is comparator).
Thank you. Makes more sense. I used many of your suggestions in the last day or so as I keep making and learning.
In this case though I found a slightly less complicated way to go about it though. I open my main with this.
Code:
// run help output if needed
if (argc != 4) {
print_help();
exit(1);
}
and I just modify per program for expected arguements. Between this and my existing error checks for proper input I think it's set. final version my in gitlab for c in sig. jointcalc.c is where I combined both my float and int calcs into one.
Last edited by jmgibson1981; 01-09-2023 at 06:58 PM.
It is better not to call functions this way. This piece of code does not simplify, but complicates the understanding of the program.
Use a separate variable, where the result of the comparison is saved, and then check the value of the variable. For example like this:
Code:
cmp = strcmp(argv[1], "help");
if (cmp == 0)
/* your code */
And I don't know who started the crazy '{' placement idea but much more readable by blocking instead:
Code:
int main(int argc, char *argv[])
{
if ((argc >= 2) && (strcasecmp(argv[0], "help") == 0))
{
print_help();
return 0;
}
else if (argc != 4)
{
printf("usage : %s num | op | num\n", argv[0]);
return 0;
}
// 'else' here is not needed, but was in original code.
return(1);
}
We tried all different ways back in the 80s and by the 90s the above form is what we settled on as a company when writing C/C++ code. Very easy to see what brace is associated, what will be executed, and is much easier to read by the next person that comes along.
We didn't (and I still don't) use tabs (just spaces) either as some editors had tabs defined differently which screwed up the code listings.
We never relied on precedence either but use parenthesis liberally. No second guessing the meaning of statements.
Anyway just my two cents .
BTW, My background was real time programming of SCADA and Automation of Hydro/Substation/Comm applications before moving to maintaining Energy Management Systems with a utility. I majored in Computer Science Applied back then....
You are correct. I haven't posted because I've had a lot going on but I did pick up a sale at Udemy. Got Beginner C, Advanced C, and something else. Can't recall. But well worth it thus far. Learning quick. Just don't have as much time the last few days.
That was one of the things they mentioned in one of the lecture things. The braces should be like that. I guess the way I was doing it is more consistent with JavaScript. At least in this guys viewpoint for his class or whatever this thing is.
Yep, definitely line parentheses up vertically and never rely on knowing(!) precedence - use brackets/parentheses as needed.
Some precedence rules aren't what you expect, and even if you can remember them all and not make a mistake the next guy to look at your code will not and will curse you
BTDT and got multiple T-shirts...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.