program confusion

S

someone

I tried to make a program that's called "Don't Shout", that reads from
a file "input.dat", and all letters should be lower-case, expect for
the first letter, and any letter after a period should be capitalized.
It's just, after I finished, it seems that compilers produce all these
strange errors. I tried it on DJGPP, MVC++ and Borland C++, with no
luck. Help would be greatly appreciated! Here is the code:
/*dontshout.c*/
#include <stdio.h>
#include <stdlib.h>

#define PER 2
#define CAPS 1
#define LOW 0

int main(void)
{
FILE *fp;
if( (fp=fopen("input.dat","r"))==NULL)
{
fprintf(stderr,"Error Opening File!");
exit(-1);
}
else
{
int marker=CAPS;
while(1)
{
int ch;
ch =fgetc(fp);
if(!feof(fp))
{
/*Checking for shouting cases*/
if((marker==CAPS) && ((ch=>'A' && ch=<'Z')||(ch=>'a' && ch=<'z'))
{
prinf("%c",ch);
marker=LOW;
continue;
}
if(marker==PER && ch=>'a' && ch=<'z')
{
ch+=32;
marker=LOW;
continue;
}
if(ch=='.'|| ch=='?')
{
marker=PER;
continue;
}
if(marker==LOW && ch=>'A' && ch=<'Z')
{
ch+=32;
prinf("%c",ch);
continue;
}
printf("%c",ch);
}
else
{
break;
}
}
}
fclose(fp);
return 0;
}
Thanks
 
O

osmium

someone said:
I tried to make a program that's called "Don't Shout", that reads from
a file "input.dat", and all letters should be lower-case, expect for
the first letter, and any letter after a period should be capitalized.
It's just, after I finished, it seems that compilers produce all these
strange errors. I tried it on DJGPP, MVC++ and Borland C++, with no
luck. Help would be greatly appreciated! Here is the code:
/*dontshout.c*/
#include <stdio.h>
#include <stdlib.h>

#define PER 2
#define CAPS 1
#define LOW 0

int main(void)
{
FILE *fp;
if( (fp=fopen("input.dat","r"))==NULL)
{
fprintf(stderr,"Error Opening File!");
exit(-1);
}
else
{
int marker=CAPS;
while(1)
{
int ch;
ch =fgetc(fp);
if(!feof(fp))
{
/*Checking for shouting cases*/
if((marker==CAPS) && ((ch=>'A' && ch=<'Z')||(ch=>'a' && ch=<'z'))
{
prinf("%c",ch);
marker=LOW;
continue;
}
if(marker==PER && ch=>'a' && ch=<'z')
{
ch+=32;
marker=LOW;
continue;
}
if(ch=='.'|| ch=='?')
{
marker=PER;
continue;
}
if(marker==LOW && ch=>'A' && ch=<'Z')
{
ch+=32;
prinf("%c",ch);
continue;
}
printf("%c",ch);
}
else
{
break;
}
}
}
fclose(fp);
return 0;
}

printf spelled at prinf - two places
 
S

someone

I changed it, and the compilers didn't really say anything. Thanks
anyways. Here are the error messages (from DJGPP's gcc):

dontshout.c: In function `main':
dontshout.c:27: parse error before '>' token
dontshout.c:27: parse error before '>' token
dontshout.c:33: parse error before '>' token
dontshout.c:39: `ch' undeclared (first use in this function)
dontshout.c:39: (Each undeclared identifier is reported only once
dontshout.c:39: for each function it appears in.)
dontshout.c:44: parse error before '>' token
dontshout.c:42: continue statement not within a loop
dontshout.c:48: continue statement not within a loop
dontshout.c: At top level:
dontshout.c:52: parse error before "else"
dontshout.c:58: warning: parameter names (without types) in function
declaration

dontshout.c:58: warning: data definition has no type or storage class
dontshout.c:59: parse error before "return"
dontshout.c:60:2: warning: no newline at end of file
 
O

osmium

someone said:
I changed it, and the compilers didn't really say anything. Thanks
anyways. Here are the error messages (from DJGPP's gcc):

dontshout.c: In function `main':
dontshout.c:27: parse error before '>' token
<snip>

Did you understand that when I said "and so on" I meant numerous places
where you used =< instead of <=?
It compiled and ran for me with the changes I described. I suggest posting
the revised code if you still have problems.

Please provide context in your posts; that is a longstanding tradition on
Usenet..
 
J

J. J. Farrell

someone said:
I changed it, and the compilers didn't really say anything. Thanks
anyways. Here are the error messages (from DJGPP's gcc):

dontshout.c: In function `main':
dontshout.c:27: parse error before '>' token
dontshout.c:27: parse error before '>' token
dontshout.c:33: parse error before '>' token
dontshout.c:39: `ch' undeclared (first use in this function)
dontshout.c:39: (Each undeclared identifier is reported only once
dontshout.c:39: for each function it appears in.)
dontshout.c:44: parse error before '>' token
dontshout.c:42: continue statement not within a loop
dontshout.c:48: continue statement not within a loop
dontshout.c: At top level:
dontshout.c:52: parse error before "else"
dontshout.c:58: warning: parameter names (without types) in function
declaration

dontshout.c:58: warning: data definition has no type or storage class
dontshout.c:59: parse error before "return"
dontshout.c:60:2: warning: no newline at end of file

The compiler is reporting errors because you have errors in your
source. The compiler helpfully tells you each line you have errors on.
Start with the first error which the compiler reports, and fix it: go
to line 27, and look for something wrong on that line. It's probably
close to where you've used a '>' source character, but it can sometimes
be something earlier in the line, or at the end of the previous line,
which has confused the compiler. When you have found and fixed that
error, go on to the next one reported by the compiler.

It can often be worth recompiling after fixing a few errors, since
compilers sometimes get confused when they've seen a lot of errors.
This is particularly true if the errors are to do with missing headers,
since that can often cause many errors.
 
S

Snis Pilbor

J. J. Farrell said:
It can often be worth recompiling after fixing a few errors, since
compilers sometimes get confused when they've seen a lot of errors.
This is particularly true if the errors are to do with missing headers,
since that can often cause many errors.

This is so true. One thing I joke about with other programmers of C:
If you get 3 pages of warnings, it's all good, just a missing bracket
or something. But if the compiler prints 1 lone error and halts, pray.
Pray hard.

=)
 
T

Thomas Matthews

someone said:
I tried to make a program that's called "Don't Shout", that reads from
a file "input.dat", and all letters should be lower-case, expect for
the first letter, and any letter after a period should be capitalized.
It's just, after I finished, it seems that compilers produce all these
strange errors. I tried it on DJGPP, MVC++ and Borland C++, with no
luck. Help would be greatly appreciated! Here is the code:
/*dontshout.c*/
#include <stdio.h>
#include <stdlib.h>

#define PER 2
#define CAPS 1
#define LOW 0

int main(void)
{
FILE *fp;
if( (fp=fopen("input.dat","r"))==NULL)
{
fprintf(stderr,"Error Opening File!");
exit(-1);
}
else
{
int marker=CAPS;
while(1)
{
int ch;
ch =fgetc(fp);
if(!feof(fp))
{
/*Checking for shouting cases*/
if((marker==CAPS) && ((ch=>'A' && ch=<'Z')||(ch=>'a' && ch=<'z'))
{
prinf("%c",ch);
marker=LOW;
continue;
}
if(marker==PER && ch=>'a' && ch=<'z')
{
ch+=32;
marker=LOW;
continue;
}
if(ch=='.'|| ch=='?')
{
marker=PER;
continue;
}
if(marker==LOW && ch=>'A' && ch=<'Z')
{
ch+=32;
prinf("%c",ch);
continue;
}
printf("%c",ch);
}
else
{
break;
}
}
}
fclose(fp);
return 0;
}
Thanks

1. Try using isupper() and islower() functions.
2. Don't add or subtract 32. This what's called a magic number.
Prefer library functions like "tolower()" or "toupper".
3. Try "printf" instread of "prinf".
4. Have you used a debugger?
5. Have you used pencil or pen and paper?


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
B

Bill Pursell

someone said:
I tried to make a program that's called "Don't Shout", that reads from
a file "input.dat", and all letters should be lower-case, expect for
the first letter, and any letter after a period should be capitalized.
It's just, after I finished, it seems that compilers produce all these
strange errors. I tried it on DJGPP, MVC++ and Borland C++, with no
luck. Help would be greatly appreciated! Here is the code:
if( (fp=fopen("input.dat","r"))==NULL)
{
fprintf(stderr,"Error Opening File!");
exit(-1);
}


Others have commented on several of the errors, and I'm
just going to make a stylistic comment. Given the
error message above, I have no way of knowing why
the file didn't open. Does it not exist? Do I not have
permission? Something else? A much kinder error
message is provided with:

if ( (fp = fopen(filename, "r")) == NULL) {
perror(filename);
exit(EXIT_FAILURE);
}
 
A

atpalmer

someone said:
I tried to make a program that's called "Don't Shout", that reads from
a file "input.dat", and all letters should be lower-case, expect for
the first letter, and any letter after a period should be capitalized.
It's just, after I finished, it seems that compilers produce all these
strange errors. I tried it on DJGPP, MVC++ and Borland C++, with no
luck. Help would be greatly appreciated! Here is the code:

[snip]

A fresh take...

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

#define FILE_PATH "input.dat"
#define PUNCT ".?!"

int main()
{
FILE *fp;
int ch;
int cap=1;

fp=fopen(FILE_PATH,"r");
if(fp==NULL)
{
perror(FILE_PATH);
exit(EXIT_FAILURE);
}

while( (ch=getc(fp)) != EOF )
{
if(cap)
{
putc( toupper(ch),stdout );
if( isalnum(ch) ) cap=0;
}
else
{
putc( tolower(ch),stdout );
if(strchr(PUNCT,ch)) cap=1;
}
}

fclose(fp);
return 0;
}
 
N

Nick Keighley

Bill Pursell wrote:

where'd the attributions go?

Others have commented on several of the errors, and I'm
just going to make a stylistic comment. Given the
error message above, I have no way of knowing why
the file didn't open. Does it not exist? Do I not have
permission? Something else? A much kinder error
message is provided with:

if ( (fp = fopen(filename, "r")) == NULL) {
perror(filename);
exit(EXIT_FAILURE);
}

technically not portable. fopen() isn't guaranteed to set errno
(spelling?)
when an error occurs. Of course many implementations do (including
most (all?) Unixen)
 
B

Barry Schwarz

I tried to make a program that's called "Don't Shout", that reads from
a file "input.dat", and all letters should be lower-case, expect for
the first letter, and any letter after a period should be capitalized.
It's just, after I finished, it seems that compilers produce all these
strange errors. I tried it on DJGPP, MVC++ and Borland C++, with no
luck. Help would be greatly appreciated! Here is the code:
/*dontshout.c*/
#include <stdio.h>
#include <stdlib.h>

#define PER 2
#define CAPS 1
#define LOW 0

int main(void)
{
FILE *fp;
if( (fp=fopen("input.dat","r"))==NULL)
{
fprintf(stderr,"Error Opening File!");
exit(-1);
}
else
{
int marker=CAPS;

Many with a C89 compiler will not be able to help you because you
define variables after executable statements.



Remove del for email
 
E

Eric Sosman

Barry said:
Many with a C89 compiler will not be able to help you because you
define variables after executable statements.

What in the name of Dennis are you talking about?
 
A

Andrew Poelstra

On 13 Aug 2006 14:42:42 -0700, "someone" <[email protected]>
wrote:

Many with a C89 compiler will not be able to help you because you
define variables after executable statements.

He did so at the beginning of a block, though, so it is legal.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top