Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > big mess

Reply
Thread Tools

big mess

 
 
Bill Cunningham
Guest
Posts: n/a
 
      02-23-2009
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


 
Reply With Quote
 
 
 
 
Kojak
Guest
Posts: n/a
 
      02-23-2009
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;
}


--
Jacques.

 
Reply With Quote
 
 
 
 
Kojak
Guest
Posts: n/a
 
      02-23-2009
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,

--
Jacques.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-23-2009
"Bill Cunningham" <(E-Mail Removed)> writes:
> 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

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-23-2009

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

[snip]

> In the meantime, what happens if you execute the above program
> like this?
>
> prog foo 5 3


segmentation fault


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-23-2009

"Kojak" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-23-2009
"Bill Cunningham" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> [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?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      02-23-2009
On Sun, 22 Feb 2009 19:13:43 -0500, "Bill Cunningham"
<(E-Mail Removed)> wrote:

> 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.

--
Remove del for email
 
Reply With Quote
 
Kojak
Guest
Posts: n/a
 
      02-23-2009
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,

--
Jacques.

 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-23-2009

"Barry Schwarz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> I give up. What is 5.10?


kandr2 page 114 5.10

Bill


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf ASP .Net 0 12-26-2008 09:29 AM
GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf ASP .Net Web Controls 0 12-26-2008 06:11 AM
GIDS 2009 Java:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf Python 0 12-24-2008 07:35 AM
GIDS 2009 Java:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf Ruby 0 12-24-2008 05:07 AM
Big Mess, what should I do? =?Utf-8?B?Q3JhaWc=?= ASP .Net 2 04-28-2005 08:20 PM



Advertisments