473,385 Members | 1,620 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,385 software developers and data experts.

Beginner question regarding the use of system() commands

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,in s_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");

}

Apr 22 '06 #1
8 2305
In article <11**********************@i39g2000cwa.googlegroups .com>,
AG <an************@gmail.com> wrote:
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.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Apr 22 '06 #2
"AG" <an************@gmail.com> a écrit dans le message de news:
11**********************@i39g2000cwa.googlegroups. com...
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,in s_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
Apr 22 '06 #3
Kr00pS wrote:
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.
Apr 23 '06 #4
Andrew Poelstra <ap*******@shaw.ca> writes:
Kr00pS wrote:
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...);


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 */

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 23 '06 #5
"AG" wrote:
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 lineflag=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!
Apr 23 '06 #6
"osmium" writes:

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


It was "...\notepadexec:\"


Another line seam problem. I can see that now when I read the repost. :-)
Apr 23 '06 #7
AG
Thank you very much. I used rmdir to clear out original file, and
everything works fine.

thanks again,
ag

Apr 23 '06 #8
AG wrote:
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".
Apr 23 '06 #9

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

Similar topics

1
by: Mosas | last post by:
Dear All, Php has exec function to run system commands and this function return last line of system command result. But in Python I can see only os.system() function to run system command but it...
10
by: Gerry | last post by:
Hi, Im starting to learn C++ and am looking to get myself a textbook to help me along. Can anyone on here recommend a book which will give me good foundations and take me up to OOP etc in...
12
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that...
5
by: mark | last post by:
I've been looking at working with Excel data. I understand the process of getting the data into a dataset and modifying it. It's one of simple beauty that is well documented. Now, I want to send...
3
by: William Foster | last post by:
Good evening all, Microsoft is really starting to annoy me as a new user. I am trying to convert my code from VBA (A very user friendly laguage with generally good help files) to Visual Studio...
6
by: jan | last post by:
My apologies for being a javascript beginner and asking such a basic question. This is probably so easy that nobody ever mentions it. Tutorials and places that tell of basic commands never seem to...
4
by: Hitchkas | last post by:
I have a very beginner and fundamental question regarding porting and running C# programs in Linux. As a background I am a total idiot when it comes to Linux but somehow managed to install Ubuntu...
1
by: wnaveenkumar | last post by:
package com.trewport.orderprocess.action; import java.io.*; import java.sql.*; import java.util.*; import java.util.Date; import java.lang.Object; import javax.servlet.*; import...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...

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.