472,958 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

c++n program

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 3770
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.
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
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
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[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
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
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
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
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
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
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
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
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
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

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

Similar topics

2
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
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
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
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
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
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
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
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
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
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.