470,850 Members | 1,312 Online

# c++n program 16
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.
Oct 5 '06 #1
16 3569 tyreld
144 100+
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.
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
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 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 also i don't understand what you mean by array how do i do that.
Oct 5 '06 #4
tyreld
144 100+
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;
2.
3. num = 5;
4. num = 10;
5. num = 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 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 100+
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 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 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 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 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 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 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 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 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 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 100+
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

 2 posts views Thread by Mike | last post: by 22 posts views Thread by edgrsprj | last post: by reply views Thread by Tom Lee | last post: by 11 posts views Thread by christopher diggins | last post: by 1 post views Thread by Eric Whittaker | last post: by 9 posts views Thread by Hemal | last post: by 7 posts views Thread by ibtc209 | last post: by 2 posts views Thread by Banfa | last post: by reply views Thread by amitpatel66 | last post: by reply views Thread by ryjfgjl | last post: by reply views Thread by prepawan | last post: by reply views Thread by sjain6 | last post: by reply views Thread by milkinvl | last post: by reply views Thread by jackhack | last post: by reply views Thread by shivajikobardan | last post: by reply views Thread by gglobus | last post: by reply views Thread by Ravipg | last post: by reply views Thread by zachatmarco | last post: by