473,654 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("inpu t.dat","r"))==N ULL)
{
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==CAP S) && ((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 1570
"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("inpu t.dat","r"))==N ULL)
{
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==CAP S) && ((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("inpu t.dat","r"))==N ULL)
{
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==CAP S) && ((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.l earn.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("inpu t.dat","r"))==N ULL)
{
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_FAILU RE);
}

--
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_P ATH,"r");
if(fp==NULL)
{
perror(FILE_PAT H);
exit(EXIT_FAILU RE);
}

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

fclose(fp);
return 0;
}

Aug 14 '06 #10

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

Similar topics

6
3015
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 on www.python.org, compiled with default options (./configure && make). I'm using the pyPgSQL plugin to connect to a PostGreSQL database, and have tried the Debian and self-compiled newest versions of that as well. I'm running BitTorrent, and...
52
9622
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
5847
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 available.\n" int main(int argc, char *argv) {
1
1342
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 have a feeling the errors I am making are probably stupid but I would appreciate any imput. //Specs to be added later //C Libraries
14
7463
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 worked on this for days and tried searching the net, but have found nothing.
42
2131
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 give any advise here? -------------------------------------------------- #include <stdlib.h> #include <stdio.h> int main(void) {
10
1258
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); void printar(void); void pline(void);
9
1921
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 like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
38
2644
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: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html Unfortunately it does not work. Here is what I'm doing: -----------------------------------------
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.