446,159 Members | 888 Online Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

# c++n program

 P: 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. please help !!! Oct 5 '06 #1
16 Replies

 100+ P: 144 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 read file into ARRAY   NEGATIVE = 0 POSITIVE = 0 POS_SUM = 0 NEG_SUM = 0   for each i in ARRAY do   if ARRAY[i] < 0 {   NEGATIVE++   NEG_SUM += ARRAY[i] }   else {   POSITIVE++   POS_SUM += ARRAY[i] }   next   write output file   Oct 5 '06 #2

 P: 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 #include #include 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

 P: 16 also i don't understand what you mean by array how do i do that. Oct 5 '06 #4

 100+ P: 144 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 int nums;   num = 5; num = 10; num = 15;   for (int i = 0; i < 3; i++)   cout << "Value #" << i << " = " << num[i] << endl;     Oct 5 '06 #5

 P: 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

 100+ P: 144 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

 P: 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

 P: 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

 P: 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 #include #include 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

 P: 24 Try this #include #include #include 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<=0) { positivecount++; sumpos+=num; } else if(num<0) { negativecount++; sumneg+=num; } } outfile<

 P: 16 Try this #include #include #include 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<=0) { positivecount++; sumpos+=num; } else if(num<0) { negativecount++; sumneg+=num; } } outfile<

 P: 43 i still got error messages saying that << were token its working good.. use this code #include #include #include 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<=0) { positivecount++; sumpos+=num; } else if(num<0) { negativecount++; sumneg+=num; } } outfile<

 P: 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

 P: 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

 P: 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

 100+ P: 144 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 