C/C++ >> Program Errors.
Posted by speedy11 on 20:51:00 02-13-2002
/* There are errors in this program,, I do not want the program altered in any way
as I won't learn that way */
/* All I need is to know is what line the errors are on, and explain why they are
errors please so I can correct the program myself
THANKS */

/* Base Converter.C*/
1.#include <string.h>
2.#include <stdio.h>
3.#include <stdlib.h>
4.#include <ctype.h>
5.long convert(char *str_ptr int base);
6.int check_string(char *str_ptr, int base);
7.
8.void main(int argc; char *argv[])
9.{
10. long result;
11. if( argc != 3)
12. {
13. printf("\nError. SYNTAX is CONVERT <number> <base>\n");
14. exit(1);
15. }
16. result = convert(argv[1], atoi(argv[2]));
17. printf("\n%s in base %s is equal to %ld in base 10",
18. argv[1], argv[2], result);
19.}
20.
21.long convert(char *str_ptr, int base)
22.{
23. long digit;
24. if( chckstring(str_ptr, base) )
25. {
26. printf("\nThere is an illegal digit in the input");
27. exit(1);
28. }
29. for( ;*str_ptr; str_ptr++ )
30. {
31. digit = isdigit( *str_ptr ) ? *str_ptr - '0':
32. toupper(str_ptr) - 'A' + 10;
33. result = result * base + digit;
34. }
35. return result;
36.)
37.
38.int check_string(char *str_ptr, int base );
39.{
40. char hi_value;
41. if( *base < 10 )
42. hi_value = '0' + base - 1 ;
43. else
44. hi_value = 'A' + base - 11;
45. for( ; *str_ptr; str_ptr++ )
46. if ( *str_ptr < '0' || *str_ptr > hi_value )
47. return (1)
48. return 0;
49.}
50.
Posted by Driver on 05:07:00 02-14-2002
Um, firstly you shouldn't be referencing to pointers in the way that you are. Like, when you put:
46. if ( *str_ptr hi_value )

or even some of your other code, you shouldn't be putting *str_ptr, rather just str_ptr. . . . or at least the way I understand it. . . .
Posted by fsvara on 12:29:00 02-14-2002
ok, I've tried compiling it and got the following compiler warnings/errors... You could've given us those, cause it makes finding the prob a lot easier.

----------------
speedy.c:6: parse error before `int'
speedy.c:9: parameter `argc' has just a forward declaration
speedy.c: In function `main':
speedy.c:10: warning: return type of `main' is not `int'
speedy.c: In function `convert':
speedy.c:33: warning: passing arg 1 of `toupper' makes integer from pointer without a cast
speedy.c:34: `result' undeclared (first use in this function)
speedy.c:34: (Each undeclared identifier is reported only once
speedy.c:34: for each function it appears in.)
speedy.c:37: parse error before `)'
speedy.c:42: invalid type argument of `unary *'
speedy.c:49: parse error before `return'
speedy.c:51: parse error at end of input
--------------

1. there's some punctuation missing in the prototype of convert().

2. check your puntuation in "void main(int argc; char *argv[]).

3. In C, main() shouldn't return void.

4. You can't just declare variables in main() and use them in another function (see "result").

5. There's somethng wrong with how you call toupper(). The prototype of toupper is "int toupper(int c);"

6. What kind of brace is that supposed to be on line 36?

7. line 41: *base... why the '*'?

8. line 47: semicolon, anybody?

9. line 38: semicolon problems, again...

Ok, i think you should be able to fix it yourself with that info...
Posted by KaGez on 12:50:00 02-14-2002
parse errors all over the place
maybe you should also check if the arguments are numbers. it's no error, but it could make the program better
just a suggestion tho
[addsig]
Posted by speedy11 on 02:40:00 02-15-2002
Thanks for the help.
Might learn to write code one day.
Thanks again.