473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

program confusion

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

Aug 13 '06 #1
14 1545
"someone" writes:
>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
>= spelled as =(and so on) many places

Aug 13 '06 #2
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

Aug 13 '06 #3
Whoops, sorry, i didn't read the last line.

Aug 13 '06 #4
"someone" writes:
>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..
Aug 14 '06 #5

someone wrote:
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.

Aug 14 '06 #6

J. J. Farrell wrote:
>
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.

=)

Aug 14 '06 #7
someone wrote:
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

Aug 14 '06 #8

someone wrote:
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>
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);
}

--
Bill Pursell

Aug 14 '06 #9
someone wrote:
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;
}

Aug 14 '06 #10
Bill Pursell wrote:

where'd the attributions go?

<snip>
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);
}
technically not portable. fopen() isn't guaranteed to set errno
(spelling?)
when an error occurs. Of course many implementations do (including
most (all?) Unixen)
--
Nick Keighley

Aug 14 '06 #11
On 13 Aug 2006 14:42:42 -0700, "someone" <ra************@gmail.com>
wrote:
>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
Aug 15 '06 #12
Barry Schwarz wrote:
On 13 Aug 2006 14:42:42 -0700, "someone" <ra************@gmail.com>
wrote:

>>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.
What in the name of Dennis are you talking about?

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 15 '06 #13
On 2006-08-15, Barry Schwarz <sc******@doezl.netwrote:
On 13 Aug 2006 14:42:42 -0700, "someone" <ra************@gmail.com>
wrote:
<snip>
>> 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.
He did so at the beginning of a block, though, so it is legal.

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 15 '06 #14
On Mon, 14 Aug 2006 22:06:03 -0400, Eric Sosman
<es*****@acm-dot-org.invalidwrote:
>Barry Schwarz wrote:
>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 brain dead remark I wish I could withdraw.
Remove del for email
Aug 15 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find...
52
by: piaseckiac | last post by:
I am producing a website on air and need a link to change the entire website from standard to metric for temperature, pressure, miles-kilometers, and volume. Thank you.
24
by: Charles Ulrich | last post by:
Greetings, I hope my greenness isn't showing too bad by asking this, but I ran across this trivial program today that left me flabbergasted: #define MESSAGE "This account is currently not...
1
by: SK | last post by:
Hi I am trying a simpler version of a program I was trying to write. The idea is to have someone enter the information and then at the end of the program display all of the info. in a table. I...
14
by: electrician | last post by:
While running a program that exceeds the array limits it issues an alert. I want to then stop the program after filling in the output boxes with blanks. How do you stop the program? I have...
42
by: Sheldon | last post by:
Hi, This program works when tested with gdb ( see results 1) but when used in a larger program where 12 becomes 1500 there exists a problem when freeing the memory ( see results 2). Can anyone...
10
by: vonbreslau | last post by:
Hello C-landers, can you find any improvement or critique to this program? #include <stdio.h> #define ZER 0 #define LIM 7 /* Sort numbers according to its increasing size */ void e(void);...
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
38
by: abasili | last post by:
Hi everyone, I'm trying to compile a C++ function and then call it from a C program. Since Google is my friend I've ended up to this link which seems very clear: ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.