Beginner question regarding the use of system() commands

A

AG

Hello,
This is my first post to this group, and on top of that I am a
beginner. So please direct me to another group if this post seems out
of place....
I have recently written a program which calculates loan
amortization schedules, writes the data to a text file, and then upon
user prompt, the program will display the created file and print the
file. Until this morning, everything worked fine, and then I started
messing around with the scanf() function. Now none of my system
commands are working. I have rewritten the program to it's original
form (as much as I can remember), but none of my system commands work.
Everything else works. I have included the file here. If someone
could explain why the system commands won't work, I would appreciate
it. The compiler I am using isn lccwin32.

thanks,
AG

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



double
amount,down,payment,pay_m,taxes,tax_m,insurance,ins_m,extra,borrowed,num,den,years,months,T;
float interest,int_m;
double balance[1000], total_int[1000],inputs[6];
unsigned int n,count=1,k,flag;
char name[];
void prog_header(void);
void print_results(void);
void disp_results(void);
void print_file(void);
int selection(void);
FILE *fp;
double get_values(double arg[]);


void main(void)
{
system("CLS");
prog_header();
n=0;
k=0;

while(count==1)
{

printf("\n\n\nEnter cost of house: $");
scanf(" %lf",&amount);
printf("Enter downpayment: $");
scanf(" %lf",&down);
printf("Enter annual interest rate (as percent not decimal): ");
scanf(" %f",&interest);
printf("Enter life of loan (years): " );
scanf(" %lf",&years);
printf("Enter annual taxes (0 if not known): $" );
scanf(" %lf",&taxes);
printf("Enter annual insurance (0 if not known): $" );
scanf(" %lf",&insurance);


int_m=(interest)/1200;
months=years*12;
tax_m=taxes/12;
ins_m=insurance/12;
borrowed=amount-down;


payment=(int_m*borrowed)/(1-pow((1+int_m),-months));
pay_m=payment+tax_m+ins_m;

T=0;
for(n=1;n<=months;n++)
{
balance[0]=amount-down;
balance[n]=(1+int_m)*balance[n-1]-pay_m+tax_m+ins_m-extra;
total_int[n]=balance[n-1]*int_m;
T=T+total_int[n];
}
print_results();

printf("\n\n\n\n\n");

count=selection();


if(count==1)
{
system("CLS");
for(k=1;k<100000;k++)
{}
}
if(count==2)
{
disp_results();
printf("\n\n\t Enter 1 to continue...");
scanf("%i",&flag);
flag=system("CLS");
count=1;

}
if (count==3)
{
print_file();
}
}



}


int selection(void)
{
try_again:
printf(" \n What do you want to do?");
printf("\n\t [1] Enter another loan");
printf("\n\t [2] See file details and then enter another loan");
printf("\n\t [3] Print file and quit program\n");
printf("\n\t......");
scanf("%i",&count);

if(count>3 | count<1)
goto try_again;
return count;
}




void prog_header(void)
{

printf("\n\n * * * WELCOME TO THE LOAN CALCULATOR * * * \n\n");
printf("\nThis program will do the following....");
printf("\n\n [1] Determine how large your monthly loan payments will
be");
printf("\n [2] Calculate amount of interest paid during the life of
the loan.");
printf("\n [3] Create an amortization schedule for the life of the
loan.");
printf("\n\n The output file will be in \"loanfiles\" folder on
the c drive");
printf("\n\n The output file will be called
\"payment_schedule\"");
}


void print_results(void)
{
printf("\n\n\n RESULTS \n");
printf(" --------------------------------------- ");
printf("\n\t Cost of house: $%0.2f",amount);
printf("\n\t Down payment: $%0.2f",down);
printf("\n\t Amount borrowed: $%0.2f",borrowed);
printf("\n\t Interest rate: %1.2f%%",interest);
printf("\n\t Monthly payment: $%.2f",pay_m);
printf("\n\t Total interest paid: $%.2f",T);

}


void disp_results(void)
{

for(n=0;n<=months;n++)
{
if(n%12==0)
{ k++;
printf("\n Year %d Balance %.2f",k-1,balance[n]);

}
}
}


void print_file(void)
{



flag=system("mkdir c:\\loanfiles");
fp=fopen("c:\\loanfiles\\payment_schedule.txt","w");
fprintf(fp,"\n********************************************\n");
fprintf(fp,"\n Loan Amortization Schedule\n");
fprintf(fp,"\n********************************************\n\n");
fprintf(fp, "Cost of house: $%.2f\n",amount);
fprintf(fp, "Down payment: $%.2f\n",down);
fprintf(fp, "Loan Amount: $%.2f\n",borrowed);
fprintf(fp, "Loan Duration: %.0f years\n",years);
fprintf(fp, "Interest rate: %1.3f%%\n",interest);
fprintf(fp, "Monthly payment: $%.2f\n",pay_m);
fprintf(fp, "Total interest paid: $%.2f\n\n\n",T);
fprintf(fp, " Year Balance\n");
fprintf(fp,"-------------------------------\n");

k=0;
for(n=0;n<=months;n++)
{
if(n%12==0)
{ k++;
fprintf(fp," %5d %15.2f\n", k-1,balance[n]);
}
}
flag=fclose(fp);

flag=system("c:\\WINDOWS\\system32\\notepad.exe
c:\\loanfiles\\payment_schedule.txt");
flag=system("print c:\\loanfiles\\payment_schedule.txt");
flag=system("exit");

}
 
W

Walter Roberson

Until this morning, everything worked fine, and then I started
messing around with the scanf() function. Now none of my system
commands are working. I have rewritten the program to it's original
form (as much as I can remember), but none of my system commands work.
Everything else works. I have included the file here. If someone
could explain why the system commands won't work, I would appreciate
it. The compiler I am using isn lccwin32.

The C standards define that system() exist, but they don't define what
it -does-. Doing nothing, messing up your I/O, playing 'Anchors Away!'
on your line printer, trashing your hard disc: it's all the same as far
as the C standard is concerned. Anything more specific is up to your
operating system and compiler, and would need to be addressed in a
forum that is specific to them.

I would suggest that you comment out your system() commands and see if
the problem still persists. Isolate the problem. Your program should
still behave gracefully if you insert a constant error return value in
place of the system() calls -- for example, suppose the user does not
have permission to create that directory, then your program needs
to respond -cleanly- anyhow.
 
K

Kr00pS

AG said:
Hello,
This is my first post to this group, and on top of that I am a
beginner. So please direct me to another group if this post seems out
of place....
I have recently written a program which calculates loan
amortization schedules, writes the data to a text file, and then upon
user prompt, the program will display the created file and print the
file. Until this morning, everything worked fine, and then I started
messing around with the scanf() function. Now none of my system
commands are working. I have rewritten the program to it's original
form (as much as I can remember), but none of my system commands work.
Everything else works. I have included the file here. If someone
could explain why the system commands won't work, I would appreciate
it. The compiler I am using isn lccwin32.

thanks,
AG

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



double
amount,down,payment,pay_m,taxes,tax_m,insurance,ins_m,extra,borrowed,num,den,years,months,T;
float interest,int_m;
double balance[1000], total_int[1000],inputs[6];
unsigned int n,count=1,k,flag;
char name[];
void prog_header(void);
void print_results(void);
void disp_results(void);
void print_file(void);
int selection(void);
FILE *fp;
double get_values(double arg[]);


void main(void)
{
system("CLS");
prog_header();
n=0;
k=0;

while(count==1)
{

printf("\n\n\nEnter cost of house: $");
scanf(" %lf",&amount);
printf("Enter downpayment: $");
scanf(" %lf",&down);
printf("Enter annual interest rate (as percent not decimal): ");
scanf(" %f",&interest);
printf("Enter life of loan (years): " );
scanf(" %lf",&years);
printf("Enter annual taxes (0 if not known): $" );
scanf(" %lf",&taxes);
printf("Enter annual insurance (0 if not known): $" );
scanf(" %lf",&insurance);


int_m=(interest)/1200;
months=years*12;
tax_m=taxes/12;
ins_m=insurance/12;
borrowed=amount-down;


payment=(int_m*borrowed)/(1-pow((1+int_m),-months));
pay_m=payment+tax_m+ins_m;

T=0;
for(n=1;n<=months;n++)
{
balance[0]=amount-down;
balance[n]=(1+int_m)*balance[n-1]-pay_m+tax_m+ins_m-extra;
total_int[n]=balance[n-1]*int_m;
T=T+total_int[n];
}
print_results();

printf("\n\n\n\n\n");

count=selection();


if(count==1)
{
system("CLS");
for(k=1;k<100000;k++)
{}
}
if(count==2)
{
disp_results();
printf("\n\n\t Enter 1 to continue...");
scanf("%i",&flag);
flag=system("CLS");
count=1;

}
if (count==3)
{
print_file();
}
}



}


int selection(void)
{
try_again:
printf(" \n What do you want to do?");
printf("\n\t [1] Enter another loan");
printf("\n\t [2] See file details and then enter another loan");
printf("\n\t [3] Print file and quit program\n");
printf("\n\t......");
scanf("%i",&count);

if(count>3 | count<1)
goto try_again;
return count;
}




void prog_header(void)
{

printf("\n\n * * * WELCOME TO THE LOAN CALCULATOR * * * \n\n");
printf("\nThis program will do the following....");
printf("\n\n [1] Determine how large your monthly loan payments will
be");
printf("\n [2] Calculate amount of interest paid during the life of
the loan.");
printf("\n [3] Create an amortization schedule for the life of the
loan.");
printf("\n\n The output file will be in \"loanfiles\" folder on
the c drive");
printf("\n\n The output file will be called
\"payment_schedule\"");
}


void print_results(void)
{
printf("\n\n\n RESULTS \n");
printf(" --------------------------------------- ");
printf("\n\t Cost of house: $%0.2f",amount);
printf("\n\t Down payment: $%0.2f",down);
printf("\n\t Amount borrowed: $%0.2f",borrowed);
printf("\n\t Interest rate: %1.2f%%",interest);
printf("\n\t Monthly payment: $%.2f",pay_m);
printf("\n\t Total interest paid: $%.2f",T);

}


void disp_results(void)
{

for(n=0;n<=months;n++)
{
if(n%12==0)
{ k++;
printf("\n Year %d Balance %.2f",k-1,balance[n]);

}
}
}


void print_file(void)
{



flag=system("mkdir c:\\loanfiles");
fp=fopen("c:\\loanfiles\\payment_schedule.txt","w");
fprintf(fp,"\n********************************************\n");
fprintf(fp,"\n Loan Amortization Schedule\n");
fprintf(fp,"\n********************************************\n\n");
fprintf(fp, "Cost of house: $%.2f\n",amount);
fprintf(fp, "Down payment: $%.2f\n",down);
fprintf(fp, "Loan Amount: $%.2f\n",borrowed);
fprintf(fp, "Loan Duration: %.0f years\n",years);
fprintf(fp, "Interest rate: %1.3f%%\n",interest);
fprintf(fp, "Monthly payment: $%.2f\n",pay_m);
fprintf(fp, "Total interest paid: $%.2f\n\n\n",T);
fprintf(fp, " Year Balance\n");
fprintf(fp,"-------------------------------\n");

k=0;
for(n=0;n<=months;n++)
{
if(n%12==0)
{ k++;
fprintf(fp," %5d %15.2f\n", k-1,balance[n]);
}
}
flag=fclose(fp);

flag=system("c:\\WINDOWS\\system32\\notepad.exe
c:\\loanfiles\\payment_schedule.txt");
flag=system("print c:\\loanfiles\\payment_schedule.txt");
flag=system("exit");

}

Hello :)

It's my first post too..

The function main returns always int (it gives : int main (void)).

Use functions fgets with sscanf in stead of scanf.

And.. system("CLS"); is not portable (hmmm.. i don't know the exactely
translation of this).

Sorry for my bad level in English ;)

Good Luck !

Kr00pS
 
A

Andrew Poelstra

Kr00pS said:
Hello :)

It's my first post too..

Congratulations. Welcome to Usenet.
The function main returns always int (it gives : int main (void)).
Correct.
Use functions fgets with sscanf in stead of scanf.

To do that properly you must do the following:
char *raw_input = malloc(256) /* 255 bytes of non-null space */
fgets (raw_input, 255, stdin) /* This avoids buffer overflows with scanf */
sscanf (raw_input, format, args...);

The important things there are the calls to malloc, and the 255's as
opposed to 256's. Those numbers can be whatever you want, but the
malloc'd number must be greater than the fgets'd one.
And.. system("CLS"); is not portable (hmmm.. i don't know the exactely
translation of this).

True; you said that correctly. In Linux the command is "clear", and in a
lot of environments you don't even have a screen.

You should never need to clear the screen; unless you are targeting
computer-illiterates who must use a command line, you'll be erasing
important information from previous commands.
Sorry for my bad level in English ;)

Hardly. Your grammar is far, far better than most newcomers.
 
K

Keith Thompson

Andrew Poelstra said:
Congratulations. Welcome to Usenet.


To do that properly you must do the following:
char *raw_input = malloc(256) /* 255 bytes of non-null space */
fgets (raw_input, 255, stdin) /* This avoids buffer overflows with scanf */
sscanf (raw_input, format, args...);

No, you can do this:

/*
* Warning: Untested code.
*/
#define MAX_LENGTH 256
char *raw_input = malloc(MAX_LENGTH);
/* Add error checking here */
fgets(raw_input, MAx_LENGTH, stdin);
/* add error checking here */
count = sscanf(raw_input, format, args...);
/* add error checking here */

The 2nd argument to fgets() is the size of the array, not the length
of the string. As the standard says:

The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream
into the array pointed to by s. No additional characters are read
after a new-line character (which is retained) or after
end-of-file. A null character is written immediately after the
last character read into the array.

And you should *always* check for errors, even if you're just going to
abort the program on failure. malloc() returns a null pointer if it's
unable to allocate the requested memory, fgets() returns a null
pointer null pointer on error or end-of-file, and sscanf() returns the
number of items successfully scanned.

For that matter, there's no point in using malloc() if you're using a
fixed-size buffer. Just declare an array:

/*
* Warning: Untested code.
*/
#define MAX_LENGTH 256
char raw_input[MAX_LENGTH];
/* Add error checking here */
fgets(raw_input, MAx_LENGTH, stdin);
/* add error checking here */
count = sscanf(raw_input, format, args...);
/* add error checking here */
 
O

osmium

AG said:
This is my first post to this group, and on top of that I am a
beginner. So please direct me to another group if this post seems out
of place....
I have recently written a program which calculates loan
amortization schedules, writes the data to a text file, and then upon
user prompt, the program will display the created file and print the
file. Until this morning, everything worked fine, and then I started
messing around with the scanf() function. Now none of my system
commands are working. I have rewritten the program to it's original
form (as much as I can remember), but none of my system commands work.
Everything else works. I have included the file here. If someone
could explain why the system commands won't work, I would appreciate
it. The compiler I am using isn lccwin32.

<snip>

I just tried it on DevC and it works about as well as could be expected.
There were a lot of line seam problem because of Usenet, I fixed them and
changed the return type of main (just to get rid of whining messages) and
ran it. I didn't examine the numbers but it printed a bunch of reasonable
looking stuff, and bitched about errors on the file write but the file was
written successfully. I think you have a logic problem when the program is
run a seconds time, do a rmdir - or something - if the directory already
exists. The part that doesn't work is the printing of the notepad file.
But I tried the same command from the DOS prompt and got the same result: a
message saying the file is being printed, but it does not print. I didn't
investigate further , just figured it might well be the way XP home edition
works. {52 God dammed letter combinations and those morons force you to add
a suffix to identify WTF you are talking about! )

Pause... Got rid of the whining about the file. There was a mssing space in
this line
flag=system("c:\\WINDOWS\\system32\\notepad.exe
c:\\loanfiles\\payment_schedule.txt");
printf("hello\n");

It was "...\notepadexec:\"

FWIW some versions of Turbo C++ didn't have a working system command. If
you still have problems post a query to the lcc newsgroup, there is one. I
would say you are off to a good start!
 
O

osmium

:

Pause... Got rid of the whining about the file. There was a mssing space
in this line

It was "...\notepadexec:\"

Another line seam problem. I can see that now when I read the repost. :)
 
A

AG

Thank you very much. I used rmdir to clear out original file, and
everything works fine.

thanks again,
ag
 
A

Andrew Poelstra

AG said:
Thank you very much. I used rmdir to clear out original file, and
everything works fine.

thanks again,
ag
Please quote the above post. Click "More Options" and then "Add Reply".
 

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
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top