473,513 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++n program

16 New Member
write a program that reads data from a file whose name is given by the user and then outputs to another file called myoutput.txt. the input file contains 10 numbers-the output file will contain data on each of the 10 numbers broken up into the following categories:
A of how many numbers are positive
A listing of all the numbers
A count of how many numbers are negative
A sum of all the positive numbers
A sum of all the negative numbers

i kinda have an idea of how to do this but i can't figure out how to compute the sum once i determine if the number is negative or positive and how to save that to the output file.
please help !!!
Oct 5 '06 #1
16 3793
tyreld
144 New Member
write a program that reads data from a file whose name is given by the user and then outputs to another file called myoutput.txt. the input file contains 10 numbers-the output file will contain data on each of the 10 numbers broken up into the following categories:
A of how many numbers are positive
A listing of all the numbers
A count of how many numbers are negative
A sum of all the positive numbers
A sum of all the negative numbers

i kinda have an idea of how to do this but i can't figure out how to compute the sum once i determine if the number is negative or positive and how to save that to the output file.
please help !!!
Basically, you want to read the 10 numbers into an array. Then loop through all the numbers in the array checking whether positive or negative, and perform the proper actions. Here is some psuedo code describing the basic algorithm.

Expand|Select|Wrap|Line Numbers
  1. read file into ARRAY[10]
  2.  
  3. NEGATIVE = 0
  4. POSITIVE = 0
  5. POS_SUM = 0
  6. NEG_SUM = 0
  7.  
  8. for each i in ARRAY do
  9.  
  10. if ARRAY[i] < 0 {
  11.   NEGATIVE++
  12.   NEG_SUM += ARRAY[i]
  13. }
  14.  
  15. else {
  16.   POSITIVE++
  17.   POS_SUM += ARRAY[i]
  18. }
  19.  
  20. next
  21.  
  22. write output file
  23.  
Oct 5 '06 #2
queenma7
16 New Member
thanks for the reply. am writing the program now but when i try to read the numbers into my input the compiler gives me an error message. this is what i have so far.
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int one, two, three, four, five, six, seven, eight, nine, ten;
int positivecount, negativecount, sumneg, sumpos;

ofstream outdata;
ifstream filename;
string filename;

cout << "enter the file name: ";
cin >> filename;
infile.open(filename.c_str());

infile >> one >> two >> three...
does this look correct so far?
Oct 5 '06 #3
queenma7
16 New Member
also i don't understand what you mean by array how do i do that.
Oct 5 '06 #4
tyreld
144 New Member
What is the compiler error you are getting?

Arrays are a contiguous block of storage that allow you to store multiple values of a the same data type. Arrays are indexed starting with 0 as opposed to 1. So, an array of size 5 would have elements at indices 0,1,2,3,4.

Expand|Select|Wrap|Line Numbers
  1. int nums[3];
  2.  
  3. num[0] = 5;
  4. num[1] = 10;
  5. num[2] = 15;
  6.  
  7. for (int i = 0; i < 3; i++)
  8.   cout << "Value #" << i << " = " << num[i] << endl;
  9.  
  10.  
Oct 5 '06 #5
smartway
24 New Member
You just have to read the numbers one by one from input file and write it into output file. Simultaneously you have to check each number for positive and negative and calculate their respective count. There is no need to declare 10 variables to hold the number only one is enough for that. The same variable can be used to store number from input file using loop
Oct 5 '06 #6
tyreld
144 New Member
You just have to read the numbers one by one from input file and write it into output file. Simultaneously you have to check each number for positive and negative and calculate their respective count. There is no need to declare 10 variables to hold the number only one is enough for that. The same variable can be used to store number from input file using loop
Your solution only works if you are allowed to write the 10 numbers to the output file first. If any of the statistical info has to be written first then all 10 numbers must be read in and stats calculated before any data can be written to the output file.
Oct 5 '06 #7
smartway
24 New Member
Yes you are right. Your solution is the best way to write the prog.
But if we are restricted to use array then this is the only solution. Since queenma7 is not aware of array concept it seems that it has not been taught yet and If this program is an assignment given then this is the only solution.
Oct 5 '06 #8
queenma7
16 New Member
i still trying to figure this out. i dont know how to to count the positive or negative and compute the sum at the same time. i feel stuck. we haven't been taught how to do any if this in class but we were given this assignment for homework. i have been working on this for hours so if someone could just please show me a sample of how this would work. i dont understand the array concept. this assisgment is due in the morning. please help me out.
Oct 5 '06 #9
queenma7
16 New Member
thanks for the reply. am writing the program now but when i try to read the numbers into my input the compiler gives me an error message. this is what i have so far.
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int one, two, three, four, five, six, seven, eight, nine, ten;
int positivecount, negativecount, sumneg, sumpos;

ofstream outdata;
ifstream filename;
string filename;

cout << "enter the file name: ";
cin >> filename;
infile.open(filename.c_str());

infile >> one >> two >> three...
does this look correct so far?
am stuck here because i don't know how to begin the positive, negative and sum counts. i keep getting errors messages
Oct 5 '06 #10
smartway
24 New Member
Try this

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int num;
int positivecount, negativecount, sumneg, sumpos;

ofstream outfile;
ifstream infile;
string filename;

positivecount=0;
negativecount=0;
sumneg=0;
sumpos=0;

cout << "enter the input file name: ";
cin >> filename;
infile.open(filename.c_str());

cout << "enter the output file name: ";
cin >> filename;
outfile.open("myoutput.txt");


for (int i=0;i<10;i++)
{
infile>>num;
outfile<<num<<" ";
if(num>=0)
{
positivecount++;
sumpos+=num;
}
else if(num<0)
{
negativecount++;
sumneg+=num;
}
}

outfile<<endl;
outfile<<positivecount<<" numbers are positive"<<endl;
outfile<<negativecount<<" numbers are negative"<<endl;
outfile<<"sum of positive numbers = "<<sumpos<<endl;
outfile<<"sum of negative numbers = "<<sumneg<<endl;
}
Oct 5 '06 #11
queenma7
16 New Member
Try this

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int num;
int positivecount, negativecount, sumneg, sumpos;

ofstream outfile;
ifstream infile;
string filename;

positivecount=0;
negativecount=0;
sumneg=0;
sumpos=0;

cout << "enter the input file name: ";
cin >> filename;
infile.open(filename.c_str());

cout << "enter the output file name: ";
cin >> filename;
outfile.open("myoutput.txt");


for (int i=0;i<10;i++)
{
infile>>num;
outfile<<num<<" ";
if(num>=0)
{
positivecount++;
sumpos+=num;
}
else if(num<0)
{
negativecount++;
sumneg+=num;
}
}

outfile<<endl;
outfile<<positivecount<<" numbers are positive"<<endl;
outfile<<negativecount<<" numbers are negative"<<endl;
outfile<<"sum of positive numbers = "<<sumpos<<endl;
outfile<<"sum of negative numbers = "<<sumneg<<endl;
}
i still got error messages saying that << were token
Oct 5 '06 #12
anushhprabu
43 New Member
i still got error messages saying that << were token

its working good.. use this code
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
int num;
int positivecount, negativecount, sumneg, sumpos;

ofstream outfile;
ifstream infile;
string filename;

positivecount=0;
negativecount=0;
sumneg=0;
sumpos=0;

cout << "enter the input file name: ";
cin >> filename;
infile.open(filename.c_str());

outfile.open("myoutput.txt");


for (int i=0;i<10;i++)
{
infile>>num;
outfile<<num<<" ";
if(num>=0)
{
positivecount++;
sumpos+=num;
}
else if(num<0)
{
negativecount++;
sumneg+=num;
}
}

outfile<<endl;
outfile<<positivecount<<" numbers are positive"<<endl;
outfile<<negativecount<<" numbers are negative"<<endl;
outfile<<"sum of positive numbers = "<<sumpos<<endl;
outfile<<"sum of negative numbers = "<<sumneg<<endl;
}

also before executing this file create one text file (.txt) in the folder from where you are running this code.. suppose you are running from d:\queen\cprg, then create one text file there with 10 integer values as...
10
-4
2
.
.
.
then save it and then execute this code..
the code will create one file named myoutput.txt..
you can get result from there..
:)
Oct 5 '06 #13
smartway
24 New Member
Yes you are right....the file should already be created with 10 numbers in it
no output will be shown on console but after running the code when you open myoutput.txt if you find the desired output then it means it is working fine.
Oct 5 '06 #14
queenma7
16 New Member
I cant use loops because it hasnt been taught in class. so i have to write the program using only if statements. i will post what i have so far in a few minutes
Oct 5 '06 #15
m013690
23 New Member
You're learning file I/O and haven't even learned loops yet? That seems a little odd, but it's been a while for me. Anyone else think that's weird?
Oct 5 '06 #16
tyreld
144 New Member
You're learning file I/O and haven't even learned loops yet? That seems a little odd, but it's been a while for me. Anyone else think that's weird?
I find it very weird.
Oct 5 '06 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
14147
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
22
3574
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
0
6078
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
11
2581
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
1
3251
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
9
4519
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
7
13249
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
19321
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
13290
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
7267
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
7391
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
7553
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...
1
7120
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...
0
7542
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...
1
5100
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...
0
3247
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...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.