finding sum of integers in a file and out putting it to another file? still wrong output?
up vote
-2
down vote
favorite
i am trying to find sum in a file and output it to another , however when i open the output file the sum is still 0?
include <fstream>
using namespace std;
void main() {
ifstream fin("inFile.txt"); // create input stream & connects to file
ofstream fout("outFile.txt"); // create output stream & connects to file
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
for (int i = 0; i < n; i++) {
fin >> num;
sum = sum + num;
}
fout << "sum is " << sum << endl;
fin.close();
fout.close();
}
c++ file loops for-loop
|
show 2 more comments
up vote
-2
down vote
favorite
i am trying to find sum in a file and output it to another , however when i open the output file the sum is still 0?
include <fstream>
using namespace std;
void main() {
ifstream fin("inFile.txt"); // create input stream & connects to file
ofstream fout("outFile.txt"); // create output stream & connects to file
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
for (int i = 0; i < n; i++) {
fin >> num;
sum = sum + num;
}
fout << "sum is " << sum << endl;
fin.close();
fout.close();
}
c++ file loops for-loop
2
How does the input file look like? Also are you sure it was opened correctly (you never checked for error states)?
– πάντα ῥεῖ
Nov 18 at 19:55
1
So what is the value ofsum
? You never mentioned whatsum
is -- you only mentioned what your output file contains.
– PaulMcKenzie
Nov 18 at 19:56
This Code is Ok, can you show me what does the input file contain, where is it?
– Muhammad Magdi
Nov 18 at 20:00
2
You really should simply figure out where the break down begins. Is it the input file reading of the numbers? Is it the computing ofsum
? Is it the output file itself, something silly like not viewing the correct file or the file could not be created?
– PaulMcKenzie
Nov 18 at 20:04
i am confused do i have to add the files to the compiler first or no need?
– Sarah_Xx
Nov 18 at 20:14
|
show 2 more comments
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
i am trying to find sum in a file and output it to another , however when i open the output file the sum is still 0?
include <fstream>
using namespace std;
void main() {
ifstream fin("inFile.txt"); // create input stream & connects to file
ofstream fout("outFile.txt"); // create output stream & connects to file
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
for (int i = 0; i < n; i++) {
fin >> num;
sum = sum + num;
}
fout << "sum is " << sum << endl;
fin.close();
fout.close();
}
c++ file loops for-loop
i am trying to find sum in a file and output it to another , however when i open the output file the sum is still 0?
include <fstream>
using namespace std;
void main() {
ifstream fin("inFile.txt"); // create input stream & connects to file
ofstream fout("outFile.txt"); // create output stream & connects to file
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
for (int i = 0; i < n; i++) {
fin >> num;
sum = sum + num;
}
fout << "sum is " << sum << endl;
fin.close();
fout.close();
}
c++ file loops for-loop
c++ file loops for-loop
edited Nov 18 at 20:09
πάντα ῥεῖ
71.3k970133
71.3k970133
asked Nov 18 at 19:52
Sarah_Xx
616
616
2
How does the input file look like? Also are you sure it was opened correctly (you never checked for error states)?
– πάντα ῥεῖ
Nov 18 at 19:55
1
So what is the value ofsum
? You never mentioned whatsum
is -- you only mentioned what your output file contains.
– PaulMcKenzie
Nov 18 at 19:56
This Code is Ok, can you show me what does the input file contain, where is it?
– Muhammad Magdi
Nov 18 at 20:00
2
You really should simply figure out where the break down begins. Is it the input file reading of the numbers? Is it the computing ofsum
? Is it the output file itself, something silly like not viewing the correct file or the file could not be created?
– PaulMcKenzie
Nov 18 at 20:04
i am confused do i have to add the files to the compiler first or no need?
– Sarah_Xx
Nov 18 at 20:14
|
show 2 more comments
2
How does the input file look like? Also are you sure it was opened correctly (you never checked for error states)?
– πάντα ῥεῖ
Nov 18 at 19:55
1
So what is the value ofsum
? You never mentioned whatsum
is -- you only mentioned what your output file contains.
– PaulMcKenzie
Nov 18 at 19:56
This Code is Ok, can you show me what does the input file contain, where is it?
– Muhammad Magdi
Nov 18 at 20:00
2
You really should simply figure out where the break down begins. Is it the input file reading of the numbers? Is it the computing ofsum
? Is it the output file itself, something silly like not viewing the correct file or the file could not be created?
– PaulMcKenzie
Nov 18 at 20:04
i am confused do i have to add the files to the compiler first or no need?
– Sarah_Xx
Nov 18 at 20:14
2
2
How does the input file look like? Also are you sure it was opened correctly (you never checked for error states)?
– πάντα ῥεῖ
Nov 18 at 19:55
How does the input file look like? Also are you sure it was opened correctly (you never checked for error states)?
– πάντα ῥεῖ
Nov 18 at 19:55
1
1
So what is the value of
sum
? You never mentioned what sum
is -- you only mentioned what your output file contains.– PaulMcKenzie
Nov 18 at 19:56
So what is the value of
sum
? You never mentioned what sum
is -- you only mentioned what your output file contains.– PaulMcKenzie
Nov 18 at 19:56
This Code is Ok, can you show me what does the input file contain, where is it?
– Muhammad Magdi
Nov 18 at 20:00
This Code is Ok, can you show me what does the input file contain, where is it?
– Muhammad Magdi
Nov 18 at 20:00
2
2
You really should simply figure out where the break down begins. Is it the input file reading of the numbers? Is it the computing of
sum
? Is it the output file itself, something silly like not viewing the correct file or the file could not be created?– PaulMcKenzie
Nov 18 at 20:04
You really should simply figure out where the break down begins. Is it the input file reading of the numbers? Is it the computing of
sum
? Is it the output file itself, something silly like not viewing the correct file or the file could not be created?– PaulMcKenzie
Nov 18 at 20:04
i am confused do i have to add the files to the compiler first or no need?
– Sarah_Xx
Nov 18 at 20:14
i am confused do i have to add the files to the compiler first or no need?
– Sarah_Xx
Nov 18 at 20:14
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Not much wrong with the basic structure of the file but it also depends heavily on the input file format. And consider reading input can easily go wrong you should add multiples checks for failure on the input stream.
So either run the program under a debugger or add appropriate print statements.
#include <cstdlib>
#include <fstream>
int main() {
std::ifstream fin("inFile.txt"); // create input stream & connects to file
if (!fin) return EXIT_FAILURE;
std::ofstream fout("outFile.txt"); // create output stream & connects to file
if (!fout) return EXIT_FAILURE;
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
if (!fin) return EXIT_FAILURE;
for (int i = 0; i < n; i++) {
if (!fin >> num) return EXIT_FAILURE;
sum = sum + num;
}
fout << "sum is " << sum << std::endl;
return EXIT_SUCCESS;
}
And of course there's a missing check just beforereturn EXIT_SUCCESS
if final write tofout
succeeded.
– Bo R
Nov 19 at 11:40
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Not much wrong with the basic structure of the file but it also depends heavily on the input file format. And consider reading input can easily go wrong you should add multiples checks for failure on the input stream.
So either run the program under a debugger or add appropriate print statements.
#include <cstdlib>
#include <fstream>
int main() {
std::ifstream fin("inFile.txt"); // create input stream & connects to file
if (!fin) return EXIT_FAILURE;
std::ofstream fout("outFile.txt"); // create output stream & connects to file
if (!fout) return EXIT_FAILURE;
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
if (!fin) return EXIT_FAILURE;
for (int i = 0; i < n; i++) {
if (!fin >> num) return EXIT_FAILURE;
sum = sum + num;
}
fout << "sum is " << sum << std::endl;
return EXIT_SUCCESS;
}
And of course there's a missing check just beforereturn EXIT_SUCCESS
if final write tofout
succeeded.
– Bo R
Nov 19 at 11:40
add a comment |
up vote
3
down vote
accepted
Not much wrong with the basic structure of the file but it also depends heavily on the input file format. And consider reading input can easily go wrong you should add multiples checks for failure on the input stream.
So either run the program under a debugger or add appropriate print statements.
#include <cstdlib>
#include <fstream>
int main() {
std::ifstream fin("inFile.txt"); // create input stream & connects to file
if (!fin) return EXIT_FAILURE;
std::ofstream fout("outFile.txt"); // create output stream & connects to file
if (!fout) return EXIT_FAILURE;
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
if (!fin) return EXIT_FAILURE;
for (int i = 0; i < n; i++) {
if (!fin >> num) return EXIT_FAILURE;
sum = sum + num;
}
fout << "sum is " << sum << std::endl;
return EXIT_SUCCESS;
}
And of course there's a missing check just beforereturn EXIT_SUCCESS
if final write tofout
succeeded.
– Bo R
Nov 19 at 11:40
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Not much wrong with the basic structure of the file but it also depends heavily on the input file format. And consider reading input can easily go wrong you should add multiples checks for failure on the input stream.
So either run the program under a debugger or add appropriate print statements.
#include <cstdlib>
#include <fstream>
int main() {
std::ifstream fin("inFile.txt"); // create input stream & connects to file
if (!fin) return EXIT_FAILURE;
std::ofstream fout("outFile.txt"); // create output stream & connects to file
if (!fout) return EXIT_FAILURE;
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
if (!fin) return EXIT_FAILURE;
for (int i = 0; i < n; i++) {
if (!fin >> num) return EXIT_FAILURE;
sum = sum + num;
}
fout << "sum is " << sum << std::endl;
return EXIT_SUCCESS;
}
Not much wrong with the basic structure of the file but it also depends heavily on the input file format. And consider reading input can easily go wrong you should add multiples checks for failure on the input stream.
So either run the program under a debugger or add appropriate print statements.
#include <cstdlib>
#include <fstream>
int main() {
std::ifstream fin("inFile.txt"); // create input stream & connects to file
if (!fin) return EXIT_FAILURE;
std::ofstream fout("outFile.txt"); // create output stream & connects to file
if (!fout) return EXIT_FAILURE;
int n = 0, sum = 0, num = 0;
fin >> n; // read the number of integers from inFile.txt
if (!fin) return EXIT_FAILURE;
for (int i = 0; i < n; i++) {
if (!fin >> num) return EXIT_FAILURE;
sum = sum + num;
}
fout << "sum is " << sum << std::endl;
return EXIT_SUCCESS;
}
answered Nov 18 at 20:47
Bo R
612110
612110
And of course there's a missing check just beforereturn EXIT_SUCCESS
if final write tofout
succeeded.
– Bo R
Nov 19 at 11:40
add a comment |
And of course there's a missing check just beforereturn EXIT_SUCCESS
if final write tofout
succeeded.
– Bo R
Nov 19 at 11:40
And of course there's a missing check just before
return EXIT_SUCCESS
if final write to fout
succeeded.– Bo R
Nov 19 at 11:40
And of course there's a missing check just before
return EXIT_SUCCESS
if final write to fout
succeeded.– Bo R
Nov 19 at 11:40
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53364845%2ffinding-sum-of-integers-in-a-file-and-out-putting-it-to-another-file-still-wron%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
How does the input file look like? Also are you sure it was opened correctly (you never checked for error states)?
– πάντα ῥεῖ
Nov 18 at 19:55
1
So what is the value of
sum
? You never mentioned whatsum
is -- you only mentioned what your output file contains.– PaulMcKenzie
Nov 18 at 19:56
This Code is Ok, can you show me what does the input file contain, where is it?
– Muhammad Magdi
Nov 18 at 20:00
2
You really should simply figure out where the break down begins. Is it the input file reading of the numbers? Is it the computing of
sum
? Is it the output file itself, something silly like not viewing the correct file or the file could not be created?– PaulMcKenzie
Nov 18 at 20:04
i am confused do i have to add the files to the compiler first or no need?
– Sarah_Xx
Nov 18 at 20:14