473,749 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to C: Getting Application error when running program

I am new to the C programming language but have been programming in
Cobol for over 10 years. When I compile the following code, it
compiles clean but I get an application error both under Windows XP and
Win2K.

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

char title[] = "Year End Report";

char work_buffer[51];

main()
{
strcpy(work_buf fer,title);
strncenter(work _buffer,50);
printf("%s\n",w ork_buffer);
strljust(work_b uffer);
printf("%s\n",w ork_buffer);
strrjust(work_b uffer);
printf("%s\n",w ork_buffer);
}

/* Left justify a string */
strljust(str)
char *str;
{
int len;

len = strlen(str);
while (isspace(str) != 0) {
memmove (str,str + 1,len);
str[len] = ' ';
}
}

/* Reverse a string */
strrev(str)
char *str;
{
char ch;
char *end;

end = str + strlen(str) - 1;

while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}

/* Right justify a string */
strrjust(str)
char *str;
{
strrev(str);
strljust(str);
strrev(str);
}

/* Truncate to last non-white character */
strtrunc(str)
char *str;
{
char *end;

end = str + strlen(str) - 1;

while ((*str != 0) && (isspace (*end) != 0)) {
*end-- = 0;
}
}

/* Pad a string for len with spaces */
struntrunc(str, len)
char *str;
int len;
{
while (strlen (str) < len) {
strcat (str,' ');
}
}

/* Center a string */
strcenter(str)
char *str;
{
strncenter (str, strlen(str));
}

/* Center a string within width */
strncenter(str, width)
char *str;
int width;
{
int non_blank_len, padding;

strtrunc (str);
strrev (str);
strtrunc (str);
non_blank_len = strlen (str);

padding = (width - non_blank_len) / 2;

struntrunc (str,padding);
strrev (str);
struntrunc (str,width);
}

It seems to be blowing up in the struntrunc function, but I can't
understand why. I know that this program is trying to take a string,
center it, left justify it and right justify it.

Any ideas on how I can get this program to run?

I am using Open Watcom C/C++ 1.4 but I had the same problem when I
tried it with OW 1.3.

Thanks

Jan 17 '06 #1
22 2329
Be sure, there will be no buffer overrun. Try a buffer with 52 or some
more chars.

Yust a thought :-)

Lothar

Jan 17 '06 #2
On 17 Jan 2006 14:47:28 -0800, in comp.lang.c , "Mike Polinske"
<mp******@execp c.com> wrote:
compiles clean but I get an application error both under Windows XP and
Win2K.
Turn warninglevels up to max on your compiler. There are several
coding errors that ought to have been alerted to you.
#include <stdio.h>
#include <ctype.h>

char title[] = "Year End Report";
char work_buffer[51];
no need for these to be globals, they can be inside main.
main()
{
strcpy(work_buf fer,title);
you should get a warning from your compiler here - you need a
declaration of strcpy() in scope, usually provided by #include
<string.h>
strncenter(work _buffer,50);
you have the same problem here. Put a prototype for strncenter()
before the start of main.

Also, its a bad idea to name functions str followed by a lower case
letter. Such names are reserved for the compiler. strCenter would be
ok tho, or str_center.
printf("%s\n",w ork_buffer);
strljust(work_b uffer);
printf("%s\n",w ork_buffer);
strrjust(work_b uffer);
printf("%s\n",w ork_buffer);
}

/* Left justify a string */
strljust(str )
char *str;
this style of function definition is 20 years out of date. The normal
modern style is
void strladjust(char *str)

There's two differences here - firstly your original function was
implicitly returning an int, which is permissible but deprecated ;
secondly, you should include the parameter type in the parens.

Also, a function must return something, unless its declared void.
Yours is declared as returning an int, but doesn't return anything.
Oops!
{
int len;

len = strlen(str);
while (isspace(str) != 0) {
isspace() expects a single character - str is a string.
Did you mean *str. ?
memmove (str,str + 1,len);
str[len] = ' ';
}
}

/* Reverse a string */
strrev(str)
char *str;
this name may conflict with a builtin library function. Did this
actually compile?
struntrunc(str ,len)
char *str;
int len;
{
while (strlen (str) < len) {
strcat (str,' ');


strcat requires the 2nd parameter to be a string, not a char.
strcat(str, " ");

by the way, one can do this entire operation much more simply with
sprintf()

Mark McIntyre
--
Jan 17 '06 #3
Mike Polinske wrote:
I am new to the C programming language but have been programming in
Cobol for over 10 years. When I compile the following code, it
compiles clean but I get an application error both under Windows XP and
Win2K.

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

char title[] = "Year End Report";

char work_buffer[51];

main()
Main returns an int, and as you are not using the optional parameters it
is better to say so explicitly.

int main(void)
{
strcpy(work_buf fer,title);
strncenter(work _buffer,50);
printf("%s\n",w ork_buffer);
strljust(work_b uffer);
printf("%s\n",w ork_buffer);
strrjust(work_b uffer);
printf("%s\n",w ork_buffer);
Main returns an int, so return an int!
return 0;
}

/* Left justify a string */
strljust(str)
char *str;
Replace whatever resource you are learning from. That style went out in
1989. Use the prototype style instead

strljust(char *str)

Also, add a prototype definition before main so that the compiler is
required to validate the parameters.

Also, function names starting str followed by a lower case letter are
reserved, so you should pick a different name. Possibly str_ljust.
{
int len;

len = strlen(str);
while (isspace(str) != 0) {
Why not simply
while (isspace(str))
?
memmove (str,str + 1,len);
str[len] = ' ';
C arrays are indexed from 0 not from 1, so this is your problem. You are
overwriting the null termination of the string instead of the last
non-null character.
}
Also, why not work out how far you need to move it then move it all the
way in one go? I know we say don't optimise until you know you have a
problem, but there is no need to go overboard and make it obviously
inefficient!
}

/* Reverse a string */
strrev(str)
char *str;
Same comments as before.
{
char ch;
char *end;

end = str + strlen(str) - 1;

while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}

/* Right justify a string */
strrjust(str)
char *str;
Same comments.
{
strrev(str);
strljust(str);
strrev(str);
}

/* Truncate to last non-white character */
strtrunc(str)
char *str;
Same comments again.

<snip unchecked additional code>
It seems to be blowing up in the struntrunc function, but I can't
understand why. I know that this program is trying to take a string,
center it, left justify it and right justify it.

Any ideas on how I can get this program to run?
First fix everything I (and anyone else who responds) have pointed out,
then go through the code looking for any other similar errors. I've
you've made a given class of mistakes once yoy may well have made it
more than once.
I am using Open Watcom C/C++ 1.4 but I had the same problem when I
tried it with OW 1.3.


On comp.lang.c the compiler is irrelevant since we only deal with
standard C. However, your problem looks to be with standard C rather
than the compiler, so it is perfectly on topic in comp.lang.c
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 18 '06 #4
<mp******@execp c.com> wrote:
I am new to the C programming language but have been programming in
Cobol for over 10 years. When I compile the following code, it
compiles clean but I get an application error both under Windows XP and
Win2K.

In addition to what others pointed out re: "old style" declarations,
these are the few errors I can see:

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

char title[] = "Year End Report";

char work_buffer[51];

main()
{
strcpy(work_buf fer,title);
strncenter(work _buffer,50);
printf("%s\n",w ork_buffer);
strljust(work_b uffer);
printf("%s\n",w ork_buffer);
strrjust(work_b uffer);
printf("%s\n",w ork_buffer);
}

/* Left justify a string */
strljust(str )
char *str;
{
int len;

len = strlen(str);
while (isspace(str) != 0) { %%%%%% should be isspace(*str) - isspace expects a char, not a pointer
to one memmove (str,str + 1,len);
str[len] = ' ';
}
}

/* Reverse a string */
strrev(str) %%%%%% strrev is already defined. choose another namechar *str;
{
char ch;
char *end;

end = str + strlen(str) - 1;

while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}

/* Right justify a string */
strrjust(str )
char *str;
{
strrev(str);
strljust(str);
strrev(str);
}

/* Truncate to last non-white character */
strtrunc(str )
char *str;
{
char *end;

end = str + strlen(str) - 1;

while ((*str != 0) && (isspace (*end) != 0)) {
*end-- = 0;
}
}

/* Pad a string for len with spaces */
struntrunc(str ,len)
char *str;
int len;
{
while (strlen (str) < len) {
strcat (str,' '); %%%%%% should be strcat(str, " "); - strcat() expects two strings }
}

/* Center a string */
strcenter(st r)
char *str;
{
strncenter (str, strlen(str));
}

/* Center a string within width */
strncenter(str ,width)
char *str;
int width;
{
int non_blank_len, padding;

strtrunc (str);
strrev (str);
strtrunc (str);
non_blank_len = strlen (str);

padding = (width - non_blank_len) / 2;

struntrunc (str,padding);
strrev (str);
struntrunc (str,width);
}

It seems to be blowing up in the struntrunc function, but I can't
understand why. I know that this program is trying to take a string,
center it, left justify it and right justify it.

Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
Jan 18 '06 #5
Mike Polinske wrote:

I am new to the C programming language but have been programming in
Cobol for over 10 years. When I compile the following code, it
compiles clean but I get an application error both under Windows XP and
Win2K.

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

char title[] = "Year End Report";

char work_buffer[51];

main()
{
strcpy(work_buf fer,title);
strncenter(work _buffer,50);
printf("%s\n",w ork_buffer);
strljust(work_b uffer);
printf("%s\n",w ork_buffer);
strrjust(work_b uffer);
printf("%s\n",w ork_buffer);
}

/* Left justify a string */
strljust(str)
char *str;
{
int len;

len = strlen(str);
while (isspace(str) != 0) {
memmove (str,str + 1,len);
str[len] = ' ';
}
}

/* Reverse a string */
strrev(str)
char *str;
{
char ch;
char *end;

end = str + strlen(str) - 1;

while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}

/* Right justify a string */
strrjust(str)
char *str;
{
strrev(str);
strljust(str);
strrev(str);
}

/* Truncate to last non-white character */
strtrunc(str)
char *str;
{
char *end;

end = str + strlen(str) - 1;

while ((*str != 0) && (isspace (*end) != 0)) {
*end-- = 0;
}
}

/* Pad a string for len with spaces */
struntrunc(str, len)
char *str;
int len;
{
while (strlen (str) < len) {
strcat (str,' ');
}
}

/* Center a string */
strcenter(str)
char *str;
{
strncenter (str, strlen(str));
}

/* Center a string within width */
strncenter(str, width)
char *str;
int width;
{
int non_blank_len, padding;

strtrunc (str);
strrev (str);
strtrunc (str);
non_blank_len = strlen (str);

padding = (width - non_blank_len) / 2;

struntrunc (str,padding);
strrev (str);
struntrunc (str,width);
}

It seems to be blowing up in the struntrunc function, but I can't
understand why. I know that this program is trying to take a string,
center it, left justify it and right justify it.

Any ideas on how I can get this program to run?


/* BEGIN new.c */

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

void str_ljust(char *str);
void str_rev(char *str);
void str_rjust(char *str, size_t len);
void str_trunc(char *str);
void str_untrunc(cha r *str, size_t len);
void str_center(char *str);
void str_ncenter(cha r *str, size_t len);

int main(void)
{
char title[] = " Year End Report ";
char work_buffer[51];

strcpy(work_buf fer, title);

str_ncenter(wor k_buffer, 50);
printf("%s\n",w ork_buffer);

str_ljust(work_ buffer);
printf("%s\n", work_buffer);

str_rjust(work_ buffer, 50);
printf("%s\n", work_buffer);

return 0;
}

void str_ljust(char *str)
{
char *ptr = str;

while (isspace(*ptr)) {
++ptr;
}
memmove(str, ptr, strlen(ptr) + 1);
}

void str_rev(char *str)
{
char ch;
char *end;

if (str[0] != '\0') {
end = str + strlen(str + 1);
while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}
}

void str_rjust(char *str, size_t len)
{
size_t pad;

str_rev(str);
str_trunc(str);
str_rev(str);
str_trunc(str);
pad = len - strlen(str);
memmove(str + pad, str, strlen(str) + 1);
memset(str, ' ', pad);
}

void str_trunc(char *str)
{
char *end = str + strlen(str);

while (end != str && isspace(*--end)) {
*end = '\0';
}
}

void str_untrunc(cha r *str, size_t len)
{
memset(str + strlen(str), ' ', len - strlen(str));
str[len] = '\0';
}

void str_center(char *str)
{
str_ncenter (str, strlen(str));
}

void str_ncenter(cha r *str, size_t len)
{
size_t pad;

str_rev(str);
str_trunc(str);
str_rev(str);
str_trunc(str);
pad = (len - strlen(str)) / 2;
memmove(str + pad, str, strlen(str) + 1);
memset(str, ' ', pad);
}

/* END new.c */
--
pete
Jan 18 '06 #6
Thank you everyone!!

I am using a book called "Moving from Cobol to C" by Mo Budlong which
is from 1993.

I thought it would be good to start with this book rather than K&R's C
Programming, but I guess I was wrong :-)

I didn't realize C had changed that much and I had ignored the compiler
warnings about no prototypes. I also thought that since C++ is derived
from C I would start out with C programming.

Coming from the nice "wordiness" of Cobol, C and C++ feel like a bunch
of shorthand or hieroglyphics :-)

I'm going to keep on trying to learn C and C++ since I'd like to expand
my programming language knowledge.

Jan 19 '06 #7
Mike Polinske wrote:
Thank you everyone!!

I am using a book called "Moving from Cobol to C" by Mo Budlong which
is from 1993.
It looks like that book was out of date even when it was written. The C
standard which introduced prototypes came out in 1989!
I thought it would be good to start with this book rather than K&R's C
Programming, but I guess I was wrong :-)
You were. You would be far better off starting with K&R.
I didn't realize C had changed that much and I had ignored the compiler
warnings about no prototypes.
Never ignore warnings because they almost always indicate that you
really do have a problem.
I also thought that since C++ is derived
from C I would start out with C programming.
Yes, learning C before C++ is reasonable.
Coming from the nice "wordiness" of Cobol, C and C++ feel like a bunch
of shorthand or hieroglyphics :-)

I'm going to keep on trying to learn C and C++ since I'd like to expand
my programming language knowledge.


Just remember that although C++ was derived from C in the early days the
languages have diverged significantly so things which are good style in
C are errors in C++ and vice versa.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 19 '06 #8
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Mike Polinske wrote:
I didn't realize C had changed that much and I had ignored the compiler
warnings about no prototypes.


Never ignore warnings because they almost always indicate that you
really do have a problem.


But also do note that warnings do not always point out exactly the
problem that the text of the warning seems to indicate. One infamous
warning is the one about "converting integer to pointer without a cast",
which most usually does _not_ indicate that you're missing a cast, but
that you've forgot to #include the required header which would tell the
compiler that there is no integer to convert.

Richard
Jan 19 '06 #9
Flash Gordon wrote:
Mike Polinske wrote:

I also thought that since C++ is derived
from C I would start out with C programming.


Yes, learning C before C++ is reasonable.


The C++ people tend to disagree with this. I would generally agree that
if you only want to learn C++, then start with it. I don't agree with
them that C is a handicap to learning C++, not if one chooses a proper
introductory book for that language.

If you'd like to become at least familiar with both languages, then C
is probably the better starting point, as it's a smaller language.
I'm going to keep on trying to learn C and C++ since I'd like to
expand my programming language knowledge.


Just remember that although C++ was derived from C in the early days
the languages have diverged significantly so things which are good
style in C are errors in C++ and vice versa.


And some things that are good style in C are permissible in C++, but
not the optimal way to do things.
Brian

Jan 19 '06 #10

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

Similar topics

0
1340
by: Oleg Medyanik | last post by:
Hello, All I have a question regarding running .NET application on W2k SP4 server in Terminal Service client, After we installed SP4 we face the exception when closing .NET app in Terminal Service Client After reading this http://support.microsoft.com/default.aspx?kbid=823485 we converted our apps to Framework 1.1 but it has no effect. And also i have a administrative
1
1324
by: Yoshitha | last post by:
hi i've vb application and i migrated it to vb.net application and it is working fine when i run this in windows 2000 professional where am having dotnetframwork 1.1 and visual studio 2003. but when i run the same application in XP home edition which is having only framwork 1.1 i am getting the following error.
8
10014
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/WebApplication1'. 'HTTP/1.1 500 Internal Server Error'."
2
2115
by: David Hearn | last post by:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. SQLExpress database file auto-creation error: The connection string specifies a local SQL Server Express instance using a...
12
5871
by: JohnR | last post by:
I have narrowed a problem down to a simple example. A form with two buttons. One EXIT and one FBD. The exit button does an "END" to end the application. The FBD button does a FolderBrowserDialog and nothing else. When I start the application and hit EXIT everything works fine. However if it display the FBD box (even if I don't pick a folder and immediately hit cancel) and then try to exit one of two things happens. Either (1) the...
2
4220
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed .NET. I could not debug using breakpoints, breakpoints are not getting hit, but the application is working fine with out any issue.
6
3620
by: David Lozzi | last post by:
Hello there, I'm getting the following error System.NullReferenceException: Object reference not set to an instance of an object. at shopping_bag.GetBagTotals()
0
2921
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form action="/login"> <html:text property="userName" value="Bunty"/> <html:submit/>
4
5729
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working, commercially sold application with only partial build instructions. The previous maintainer of the code (a mixture of C and C++) is no longer with the company, but when he built the code he used MSVC++, and though I am not certain of the version he was ...
0
8997
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
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8257
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...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
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
4709
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...
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2218
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.