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 !!!
16 3792
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. -
read file into ARRAY[10]
-
-
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
-
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?
also i don't understand what you mean by array how do i do that.
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. -
int nums[3];
-
-
num[0] = 5;
-
num[1] = 10;
-
num[2] = 15;
-
-
for (int i = 0; i < 3; i++)
-
cout << "Value #" << i << " = " << num[i] << endl;
-
-
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
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.
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.
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.
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
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;
}
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
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..
:)
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.
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
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
...
|
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...
|
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 >>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
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: 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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |