big mess

B

Bill Cunningham

Well I read 5.10 page 114 and didn't learn a thing. I wrote some code
the compiled with warnings. This program is supposed to take an h or NULL as
its first argument and if there's no h as the first arg it subtacts
decimals. If there's an h subtracts hex numbers. The ouput I get is 0.00 or
segmentation fault. What a mess.

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
int i;
long a, b;
double x, y;
if (argv[1] != 'h') {
x = strtod(argv[2], NULL);
y = strtod(argv[3], NULL);
printf("%.2f\n", x - y);
} else if (argv[1] == 'h') {
a = strtol(argv[2], NULL, 16);
b = strtol(argv[3], NULL, 16);
printf("%ld\n", a - b);
}
return 0;
}

embarrassing or not I would like the 'h' to show up as any argv[] whether 1
or 3 if hex subtraction. If it's not present decimal subtraction.

Bill
 
K

Kojak

Le Sun, 22 Feb 2009 19:13:43 -0500,
Bill Cunningham a écrit :
Well I read 5.10 page 114 and didn't learn a thing. I wrote some
code the compiled with warnings. This program is supposed to take an
h or NULL as its first argument and if there's no h as the first arg
it subtacts decimals. If there's an h subtracts hex numbers. The
ouput I get is 0.00 or segmentation fault. What a mess.

Try something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
long a, b;
double x, y;

/* Insert argument checking here */

if (!strcmp(argv[1], "h")) {
a = strtol(argv[2], NULL, 16);
b = strtol(argv[3], NULL, 16);
printf("%ld\n", a - b);
} else {
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n", x - y);
}

return EXIT_SUCCESS;
}
 
K

Kojak

Le Mon, 23 Feb 2009 02:10:41 +0100,
Kojak a écrit :
Le Sun, 22 Feb 2009 19:13:43 -0500,
Bill Cunningham a écrit :
Well I read 5.10 page 114 and didn't learn a thing. I wrote some
code the compiled with warnings. This program is supposed to take an
h or NULL as its first argument and if there's no h as the first arg
it subtacts decimals. If there's an h subtracts hex numbers. The
ouput I get is 0.00 or segmentation fault. What a mess.

Try something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
long a, b;
double x, y;

/* Insert argument checking here */

if (!strcmp(argv[1], "h")) {
a = strtol(argv[2], NULL, 16);
b = strtol(argv[3], NULL, 16);
printf("%ld\n", a - b);
} else {
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n", x - y);
}

return EXIT_SUCCESS;
}

If you don't want 'strcmp', juste remove
#include <string.h>
and replace
if (!strcmp(argv[1], "h")) {
by
if (*argv[1] == 'h') {

Yours,
 
K

Keith Thompson

Bill Cunningham said:
Well I read 5.10 page 114 and didn't learn a thing. I wrote some code
the compiled with warnings. This program is supposed to take an h or NULL as
its first argument and if there's no h as the first arg it subtacts
decimals. If there's an h subtracts hex numbers. The ouput I get is 0.00 or
segmentation fault. What a mess.

If you get warnings, then show us the warnings. Or, better yet, fix
them.
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
int i;
long a, b;
double x, y;
if (argv[1] != 'h') {

argv[1] is a char*. Comparing it to 'h' makes no sense. In fact,
it's a constraint violation; if the compiler accepts the code anyway,
the behavior is undefined.

But the most likely result is that the test will fail (or rather, that
the "!=" test will succeed) , and you'll execute the following lines:
x = strtod(argv[2], NULL);
y = strtod(argv[3], NULL);
printf("%.2f\n", x - y);

You're not checking for failures in strtod. But if argv[2] and
argv[3] happen to point to valid strings, this should work.
} else if (argv[1] == 'h') {

You've already compared argv[1] against 'h'; why repeat the test?
Just use a simple "else".
a = strtol(argv[2], NULL, 16);
b = strtol(argv[3], NULL, 16);
printf("%ld\n", a - b);

Again, you're not checking for errors -- but since this code won't be
executed (until you fix the test), that doesn't matter yet.
}
return 0;
}


embarrassing or not I would like the 'h' to show up as any argv[] whether 1
or 3 if hex subtraction. If it's not present decimal subtraction.

You say you get "0.00" or a segmentation fault -- but for *what
inputs*? You have plenty of information; you know what warnings you
got (and didn't bother to correct), and how you executed the program.
Tell us.

I suggest you get this version working (checking just argv[1]) before
worrying about making it more flexible.

In the meantime, what happens if you execute the above program
like this?

prog foo 5 3
 
B

Bill Cunningham

Try something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
long a, b;
double x, y;

/* Insert argument checking here */

if (!strcmp(argv[1], "h")) {
a = strtol(argv[2], NULL, 16);
b = strtol(argv[3], NULL, 16);
printf("%ld\n", a - b);
} else {
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
printf("%.2f\n", x - y);
}

return EXIT_SUCCESS;
}

If you don't want 'strcmp', juste remove
#include <string.h>
and replace
if (!strcmp(argv[1], "h")) {
by
if (*argv[1] == 'h') {

Yours,

What about looping around to find an h. For example.

sub h 3 4
sub 3 4 h
sub 3 h 4

If h is in any argv I would like the program to subtract hex numbers.
Whithout an h decimal numbers.

Bill
 
K

Keith Thompson

Bill Cunningham said:
[snip]
In the meantime, what happens if you execute the above program
like this?

prog foo 5 3

segmentation fault

That's surprising. Other than the undefined behavior of the
(argv[1] != 'h')
comparison, I don't see anything that should cause a segmentation
fault. Did you get that result from the *exact* code that you posted?

Ok, try this:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
long a, b;
double x, y;
if (argc != 4) {
fputs("Usage: $0 h num num\n", stderr);
exit(EXIT_FAILURE);
}
if (argv[1][0] != 'h') {
x = strtod(argv[2], NULL);
y = strtod(argv[3], NULL);
printf("%.2f\n", x - y);
} else {
a = strtol(argv[2], NULL, 16);
b = strtol(argv[3], NULL, 16);
printf("%ld\n", a - b);
}
return 0;
}

I've made the following changes:

1. Fix the invalid comparison.
2. Check the value of argc.
3. Delete the unused variable i.

Try running it as:
prog h 5 3
prog x 5 3

And a note about what you're doing here. With an 'h' argument, you're
subtracting two integer entered in hexadecimal; with anything else,
you're subtracting two floating-point numbers. Is that really what
you want?
 
B

Barry Schwarz

Well I read 5.10 page 114 and didn't learn a thing. I wrote some code

I give up. What is 5.10?
the compiled with warnings. This program is supposed to take an h or NULL as
its first argument and if there's no h as the first arg it subtacts
decimals. If there's an h subtracts hex numbers. The ouput I get is 0.00 or
segmentation fault. What a mess.

Why are you executing a program that didn't compile cleanly?
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
int i;
long a, b;
double x, y;
if (argv[1] != 'h') {

What is the type of argv[1]? What is the type of 'h'? Are they in
any way compatible?
x = strtod(argv[2], NULL);
y = strtod(argv[3], NULL);
printf("%.2f\n", x - y);
} else if (argv[1] == 'h') {

After you correct the first if is there any reason to include an if in
the else?
a = strtol(argv[2], NULL, 16);
b = strtol(argv[3], NULL, 16);
printf("%ld\n", a - b);
}
return 0;
}

embarrassing or not I would like the 'h' to show up as any argv[] whether 1
or 3 if hex subtraction. If it's not present decimal subtraction.

You don't even have broken code to test argv[3] for an 'h'.

At least in your previous thread you tested argc to see if there were
enough arguments to use.
 
K

Kojak

Le Sun, 22 Feb 2009 21:44:37 -0500,
Bill Cunningham a écrit :
What about looping around to find an h. For example.

sub h 3 4
sub 3 4 h
sub 3 h 4

Better use 'h' as an optional argument like this

sub [-h] <x> <y>

That said, if you want option walking around arguments, juste
check them, argv by argv. but, I think it's a bad idea.
If h is in any argv I would like the program to subtract hex numbers.
Whithout an h decimal numbers.

Sorry, not understood.

yours,
 
B

Bill Cunningham

Ok, try this:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv)
{
long a, b;
double x, y;
if (argc != 4) {
fputs("Usage: $0 h num num\n", stderr);
exit(EXIT_FAILURE);
}
if (argv[1][0] != 'h') {
x = strtod(argv[2], NULL);
y = strtod(argv[3], NULL);
printf("%.2f\n", x - y);
} else {
a = strtol(argv[2], NULL, 16);
b = strtol(argv[3], NULL, 16);
printf("%ld\n", a - b);
}
return 0;
}

I've made the following changes:

1. Fix the invalid comparison.
2. Check the value of argc.
3. Delete the unused variable i.

Try running it as:
prog h 5 3
prog x 5 3

And a note about what you're doing here. With an 'h' argument, you're
subtracting two integer entered in hexadecimal; with anything else,
you're subtracting two floating-point numbers. Is that really what
you want?

I'm not quite sure what you mean here.

I'll compile the code and see what happens it looks at first glance like
what I wanted.

prog 3 5 //subtracts 3 from 5
prog h f ff //subtracts f from ff

This is the output I want. Also I wanted to make the program as a learning
experience to accept the h from anyway a do the hex subtraction. Example:

prog f ff h
prog f h ff
prog h f ff

And use decimal subtract in the absence of the h. Most would probably use -h
as a switch for this type of thing.


Bill
 
K

Keith Thompson

Bill Cunningham said:
Ok, try this:
[snip]

I've made the following changes:

1. Fix the invalid comparison.
2. Check the value of argc.
3. Delete the unused variable i.

Try running it as:
prog h 5 3
prog x 5 3

And a note about what you're doing here. With an 'h' argument, you're
subtracting two integer entered in hexadecimal; with anything else,
you're subtracting two floating-point numbers. Is that really what
you want?

I'm not quite sure what you mean here.

I'll compile the code and see what happens it looks at first glance like
what I wanted.

You know, you could have tried it before posting. It would have taken
just a minute or so.
prog 3 5 //subtracts 3 from 5
prog h f ff //subtracts f from ff

This is the output I want. Also I wanted to make the program as a learning
experience to accept the h from anyway a do the hex subtraction. Example:

prog f ff h
prog f h ff
prog h f ff

And use decimal subtract in the absence of the h. Most would probably use -h
as a switch for this type of thing.

You're subtracting integers in one case, floating-point values in the
other. Maybe that's what you want, but, for example, you have no
option to subtract integers expressed in decimal.
 
K

Kojak

Le Mon, 23 Feb 2009 15:51:55 -0500,
Bill Cunningham a écrit :
This is the output I want. Also I wanted to make the program as a
learning experience to accept the h from anyway a do the hex
subtraction. Example:

prog f ff h
prog f h ff
prog h f ff

And use decimal subtract in the absence of the h.

As I had suggested, check for each argument.

Try to code something like this

if 1st arg is h then
set a and b or x and y vars according to your need,
set hex mode and continue
else if 2nd arg then
... and so no ...

It's not the best way, but to start, this should be sufficient.
Most would probably use -h as a switch for this type of thing.

Yes, indeed, but don't worry about that for now.

Just a last point, as your were advised, compile, check for compiler
insults, edit, correct, and loop until it works and you understand
why.

Try to think as computer, step by step. in other words, try to
figure out what will happen on each statement.

That is, feel free to ask if you're stuck.

sincerely,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Why does strcat mess up the tokens in strtok (and strtok_r)? 92
code question 74
percentage 8
code works 11
errno 8
pow type problem 6
sh?tpile of errors 82
Adding adressing of IPv6 to program 1

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top