error

B

Bill Cunningham

This code absolutely will not compile. I have looked and looked at it
and can't see and error. This is what I have and the compiler's complaints.

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

int main(int argc, char **argv)
{
double radians, degrees;
radians = degrees = 0.0;
if (argc == 1 || argc > 3) {
fputs("Usage error\n", stderr); // not user friendly I know
exit(0);
} // but for my purpose fine.
if (argv[1] != "r" || argv[1] != "d") {
fputs(" Usage error\n Must be d for degrees or r for
radians\n", stderr);
exit(0);
}
if (argv[1] == "r") {
degrees = strtod(argv[2], NULL);
radians = degrees * 3.1415 / 180;
printf("%.4f\n", radians);
return 0;
}
if (argv[1] == "d") {
radians = strtod(argv[2], NULL);
degrees = radians * 180 / 3.1415;
printf("%.4f\n", degrees);
return 0;
}
return 0;
}

p.c: In function `main':
p.c:13: error: missing terminating " character
p.c:14: error: stray '\' in program
p.c:14: error: syntax error before "n"
p.c:14: error: missing terminating " character

The \n looks fine everywhere to me.

Bill
 
B

Billy Mays

This code absolutely will not compile. I have looked and looked at it
and can't see and error. This is what I have and the compiler's complaints.

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

int main(int argc, char **argv)
{
double radians, degrees;
radians = degrees = 0.0;
if (argc == 1 || argc> 3) {
fputs("Usage error\n", stderr); // not user friendly I know
exit(0);
} // but for my purpose fine.
if (argv[1] != "r" || argv[1] != "d") {
fputs(" Usage error\n Must be d for degrees or r for
radians\n", stderr);
exit(0);
}
if (argv[1] == "r") {
degrees = strtod(argv[2], NULL);
radians = degrees * 3.1415 / 180;
printf("%.4f\n", radians);
return 0;
}
if (argv[1] == "d") {
radians = strtod(argv[2], NULL);
degrees = radians * 180 / 3.1415;
printf("%.4f\n", degrees);
return 0;
}
return 0;
}

p.c: In function `main':
p.c:13: error: missing terminating " character
p.c:14: error: stray '\' in program
p.c:14: error: syntax error before "n"
p.c:14: error: missing terminating " character

The \n looks fine everywhere to me.

Bill


You can't have a line break in a c string. You can however concatenate
strings by placing two of them together like this:


if (argv[1] != "r" || argv[1] != "d") {
fputs(" Usage error\n Must be d for degrees or r for "
"radians\n", stderr);
exit(0);
}




Bill
 
J

Joachim Schmitz

Bill said:
This code absolutely will not compile. I have looked and looked at
it and can't see and error. This is what I have and the compiler's
complaints.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
double radians, degrees;
radians = degrees = 0.0;
if (argc == 1 || argc > 3) {
fputs("Usage error\n", stderr); // not user friendly I know
exit(0);
} // but for my purpose fine.
if (argv[1] != "r" || argv[1] != "d") {

This is not the way to compare strings. Use strcmp or copmare chars:

if (argv[1][0] != 'r' || argv[1][0] != 'd') {

But the compiler error is reported here, line 13:
fputs(" Usage error\n Must be d for degrees or r for
radians\n", stderr);

A string literal can't span mutiple lines. But string concatenation takes
place, so you could write (note the extra blank):

fputs(" Usage error\n Must be d for degrees or r for "
"radians\n", stderr);

Or 'hide' the linefeed (again, note the extra blank):

fputs(" Usage error\n Must be d for degrees or r for \
radians\n", stderr);
exit(0);
}
if (argv[1] == "r") {

This is not the way to compare strings, see above.
degrees = strtod(argv[2], NULL);
radians = degrees * 3.1415 / 180;
printf("%.4f\n", radians);
return 0;
}
if (argv[1] == "d") {

This is not the way to compare strings, see above.
radians = strtod(argv[2], NULL);
degrees = radians * 180 / 3.1415;
printf("%.4f\n", degrees);
return 0;
}
return 0;
}

p.c: In function `main':
p.c:13: error: missing terminating " character
p.c:14: error: stray '\' in program
p.c:14: error: syntax error before "n"
p.c:14: error: missing terminating " character

The \n looks fine everywhere to me.

Start looking at the 1st error it reports, the others are likely (but by far
not aleways' follow up errors.

Bye, Jojo
 
B

Bill Cunningham

Joachim said:
Bill said:
This code absolutely will not compile. I have looked and looked at
it and can't see and error. This is what I have and the compiler's
complaints.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
double radians, degrees;
radians = degrees = 0.0;
if (argc == 1 || argc > 3) {
fputs("Usage error\n", stderr); // not user friendly I know
exit(0);
} // but for my purpose fine.
if (argv[1] != "r" || argv[1] != "d") {

This is not the way to compare strings. Use strcmp or copmare chars:

if (argv[1][0] != 'r' || argv[1][0] != 'd') {

So you're saying the above is incorrect too then? How would one use
strcmp in this case?

[snip]
 
B

Bill Cunningham

Billy said:
You can't have a line break in a c string.

[snip]

Oh I see. I didn't know that. strcmp() is one I don't use so I guess I've
learned something for today.

Bill
 
B

Bill Cunningham

Joachim said:
Bill said:
This code absolutely will not compile. I have looked and looked at
it and can't see and error. This is what I have and the compiler's
complaints.
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
double radians, degrees;
radians = degrees = 0.0;
if (argc == 1 || argc > 3) {
fputs("Usage error\n", stderr); // not user friendly I know
exit(0);
} // but for my purpose fine.
if (argv[1] != "r" || argv[1] != "d") {

This is not the way to compare strings. Use strcmp or copmare chars:

if (argv[1][0] != 'r' || argv[1][0] != 'd') {

But the compiler error is reported here, line 13:
fputs(" Usage error\n Must be d for degrees or r for
radians\n", stderr);

A string literal can't span mutiple lines. But string concatenation
takes place, so you could write (note the extra blank):

fputs(" Usage error\n Must be d for degrees or r for "
"radians\n", stderr);

Or 'hide' the linefeed (again, note the extra blank):

fputs(" Usage error\n Must be d for degrees or r for \
radians\n", stderr);
....

I tried an extra space and two quotes it didn't work.

Bill
 
D

Denis McMahon

Joachim Schmitz wrote:
This is not the way to compare strings. Use strcmp or copmare chars:

if (argv[1][0] != 'r' || argv[1][0] != 'd') {

So you're saying the above is incorrect too then? How would one use
strcmp in this case?

No, that's valid, because he's comparing single characters:

argv[1][0] means the first character of string argv[1]
'r' means the character 'r'
"r" means the 1 character long string "r"

so a method of doing the check you want is seeing if the first character
of the string argv[1] is equal to the character 'r' or 'd'

Note also that there's an error in your logic:

Joachim derived:

if (argv[1][0] != 'r' || argv[1][0] != 'd')

From your:

if (argv[1] != "r" || argv[1] != "d")

However, this test will always be true, because argv[1][0] can only have
3 possibilities:

'r'
'd'
'something else'

if it's 'r', the != 'd' side will be true, and so the whole test is true
if it's 'd', the != 'r' side will be true, and so the whole test is true
if it's 'something else', both sides will be true, and so the whole test
is true

So the test you want is probably:

if (argv[1][0] != 'r' && argv[1][0] != 'd')

Rgds

Denis McMahon
 
B

Bill Cunningham

osmium said:
Rather than describe what you tired, why not *SHOW* what you tried?

I cut and paste the proposed correction into your original program
and that part worked for me. As often happens there are multiple
errors, focus on one error, the problem of long strings and short
lines. Note that the extra blank mentioned is for readability in the
English language, not some weird syntax rule of the C language.

IOW:
"stuff"
"more stuff"

Ok I will try that. It might make things more readable for others.

Bill
 
B

blmblm

Denis said:
So the test you want is probably:

if (argv[1][0] != 'r' && argv[1][0] != 'd')

Indeed it is!

Be advised, however, that this checks only the first letter of
the argument, and if you want to allow "r" but not "random",
you'll have to compare the whole string, using strcmp -- i.e.,

if (((strcmp(argv[1], "r") != 0)) && ((strcmp(argv[1], "d") != 0)))

Notice that to use strcmp you will need

#include <string.h>
 
B

Bill Cunningham

Be advised, however, that this checks only the first letter of
the argument, and if you want to allow "r" but not "random",
you'll have to compare the whole string, using strcmp -- i.e.,

if (((strcmp(argv[1], "r") != 0)) && ((strcmp(argv[1], "d") != 0)))

Notice that to use strcmp you will need

#include <string.h>

Ok that looks even better. Would there be a way to allow only oe char to
test? Such as 'r' and anymore after that would exit me?

Bill
 
B

blmblm

Be advised, however, that this checks only the first letter of
the argument, and if you want to allow "r" but not "random",
you'll have to compare the whole string, using strcmp -- i.e.,

if (((strcmp(argv[1], "r") != 0)) && ((strcmp(argv[1], "d") != 0)))

Notice that to use strcmp you will need

#include <string.h>

Ok that looks even better. Would there be a way to allow only oe char to
test? Such as 'r' and anymore after that would exit me?

I'm not sure I understand your question, but ....

strcmp("r", "r") returns 0

but

strcmp("r", "random") returns 1

so if you want to accept "r" but reject "random", the above test is
what you need.
 
B

Bill Cunningham

I'm not sure I understand your question, but ....

strcmp("r", "r") returns 0

but

strcmp("r", "random") returns 1

so if you want to accept "r" but reject "random", the above test is
what you need.

I wasn't sure if I was too clear or not. But the strcmp() option you
mentioned fits like a glove.

Bill
 
D

Dann Corbit

... etc.

Now that you solved your errors, it might be time to use a better
value of PI.

Use the math.h header and see if M_PI works in place of 3.1415 for
your purposes. If M_PI is undefined you will get an error. Then just
copy this and insert the following:

#ifndef M_PI
#define M_PI 3.14159265358979323846 // from gcc v2 math.h
#endif

This is perhaps more precise than you need but some compilers provide
it as M_PI in math.h

Compilers are not allowed to put a macro called M_PI into math.h (even
though many do it).

From the C FAQ:

14.8: The predefined constant M_PI seems to be missing from my
machine's copy of <math.h>.

A: That constant (which is apparently supposed to be the value of
pi, accurate to the machine's precision), is not standard. If
you need pi, you'll have to define it yourself, or compute it
with 4*atan(1.0) or acos(-1.0).

References: PCS Sec. 13 p. 237.
 
T

Tim Prince

(e-mail address removed) says...

Compilers are not allowed to put a macro called M_PI into math.h (even
though many do it).

From the C FAQ:

14.8: The predefined constant M_PI seems to be missing from my
machine's copy of<math.h>.

A: That constant (which is apparently supposed to be the value of
pi, accurate to the machine's precision), is not standard. If
you need pi, you'll have to define it yourself, or compute it
with 4*atan(1.0) or acos(-1.0).

References: PCS Sec. 13 p. 237.
In typical implementations of math.h, such as those available to most
implementations of gcc and compatible compilers, it's protected by
#if !defined __STRICT_ANSI__
so as to comply with the prohibition against defining it in strict
standards compliance modes.
Microsoft also provides for making it invisible on demand.
The most common definitions (as quoted in this thread) define it as a
double constant.
The most widespread CPUs include a firmware constant accurate to at
least long double precision. The requirement for inline asm to access
it clearly distances it from standard C.
 
B

Ben Pfaff

Dann Corbit said:
14.8: The predefined constant M_PI seems to be missing from my
machine's copy of <math.h>.

A: That constant (which is apparently supposed to be the value of
pi, accurate to the machine's precision), is not standard. If
you need pi, you'll have to define it yourself, or compute it
with 4*atan(1.0) or acos(-1.0).

It's wrong to say that M_PI is not standard, even though it is
not part of the C standard. M_PI is specified by POSIX:
http://www.opengroup.org/onlinepubs/009695399/basedefs/math.h.html
 
B

Barry Schwarz

Billy said:
You can't have a line break in a c string.

[snip]

Oh I see. I didn't know that. strcmp() is one I don't use so I guess I've
learned something for today.

Strange. You participated in a long thread discussing it on 5
December 2009. You even coded it in earlier thread on 6 January 2008.
You must be running out of bait if you have to rehash old issues.
 
N

Nobody

In typical implementations of math.h, such as those available to most
implementations of gcc and compatible compilers, it's protected by
#if !defined __STRICT_ANSI__

In GNU libc, the various M_??? constants are conditionalised upon:

#if defined __USE_BSD || defined __USE_XOPEN

In turn, these macros are defined by default, but disabled by e.g. -ansi
or -std=c89 or -std=c99 (the default is -std=gnu89, which turns on most of
the POSIX/SVID/BSD/etc features as well as the GNU extensions).
 

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

Members online

No members online now.

Forum statistics

Threads
473,952
Messages
2,570,111
Members
46,695
Latest member
Juliane58C

Latest Threads

Top