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();
}









share|improve this question




















  • 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 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






  • 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















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();
}









share|improve this question




















  • 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 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






  • 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













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();
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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






  • 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














  • 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 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






  • 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








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












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;
}





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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;
}





share|improve this answer





















  • 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















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;
}





share|improve this answer





















  • 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













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;
}





share|improve this answer












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;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 at 20:47









Bo R

612110




612110












  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin