0

I'm new to coding and this is my first slack post. I've been working on this assignment for a few days and I just can't figure it out. I cant get it to compile. The fist error i get is about my string fileName. I'm confused on how to get the program to add the integers in the file, i.e how does to recognize the numbers. Any feedback is appreciated. Here is the guidelines. My code is below in the link.

Write a program that prints "Please enter your filename.", reads in the name of the file, and then tries to open it. If the input file is there and can be opened, the program should read the list of integers in the file, which will have one integer per line as in the following example:

  • 453
  • 6
  • -45
  • 34
  • 2

The program will then add together all the integers in the file, create an output file called sum.txt, write the sum to that file (just that number - no additional text), and then print (to the console) "result written to sum.txt". Remember to close both the input and output files. If the input file is not there (or is there but couldn't be opened for some reason), the program should just print out "could not access file".

Here is my code that I've written so far.

A program that adds the integers in one file and copies the sum to another.

#include <iostream>
#include <string>
#include <fstream>

int main()
{
    string fileName;

    std::cout << "Please enter your filename." << std::endl;
    std::getline.cin >> (fileName);

    std::ifstream fileName;
    fileName.open(fileName);

    if (fileName.fail())  {
        std::cout << "Error in opening file" << std::endl;
    }

    int sum = 0, x;
    while (fileName >> x) {
        sum = sum + x;
    }

    fileName.close();

    std::ofstream outFile;
    outFile.open("sum.txt");

    outFile << sum;
    outFile.close();

    std::cout << "Results written to sum.txt" << std::endl;

    return 0;
}
Bowdzone
  • 3,827
  • 11
  • 39
  • 52
lauren
  • 65
  • 1
  • 1
  • 11
  • post your code instead of sending it in a picture – Ivam Jul 08 '18 at 17:42
  • 2
    Post your code please. Don’t put links to other sites as we like to keep a record of our questions. Links can die, the stackoverflow questions will live on. – Sailanarmo Jul 08 '18 at 17:44
  • 2
    In addition to the other comments you should also explain what the problem is (and what you've tried to do to fix it). *"I just can't figure it out"* is a useless description and sounds like you want us to do your homework for you – UnholySheep Jul 08 '18 at 17:46
  • Much better. Could use a `std::` in front of `string fileName;`, though. – user4581301 Jul 08 '18 at 17:55
  • `fileName` is used to declare two unrelated variables in the same scope. You should select different names for them. – user7860670 Jul 08 '18 at 17:57
  • `std::ifstream fileName; fileName.open(fileName);` is going to be a problem. by the time you try to use the `string` version of `fileName` it's been replaced by the `ifstream` version. I recommend changing the name of the `ifstream`. – user4581301 Jul 08 '18 at 17:57
  • I don't get `while (fileName).x)`. Transcription error from `while (fileName >>x)`? – user4581301 Jul 08 '18 at 17:58
  • @user4581301 Nothing is replaced, in C++ variable names in the same scope can not be reused so this code is ill-formed. – user7860670 Jul 08 '18 at 17:59
  • yes i meant fileName>>x – lauren Jul 08 '18 at 18:00
  • @VTT good point. – user4581301 Jul 08 '18 at 18:00
  • The code still runs even if the file fails to open – Victor B. Jul 08 '18 at 18:00
  • `std::getline.cin >> (fileName);` is also a bit off. Give `std::getline(std::cin, fileName);` a shot. – user4581301 Jul 08 '18 at 18:01
  • so to change the name of the ifstream should i just use something like input? Then the next like would read input.open(fileName)? – lauren Jul 08 '18 at 18:01
  • @notorius should I put the input.open in if statement? – lauren Jul 08 '18 at 18:03
  • 1
    Recommend that you stop coding for a while and do a refresh read of the first few chapters of whatever text you are learning from. If you have no text, here's a semi-curated list of recommended learning materials: [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Jul 08 '18 at 18:03
  • 1
    "so to change the name of the ifstream should i just use something like input?" Yes. Avoid reusing variable names. Do not reuse in the same function without a very good reason, and as VTT points out, you cannot reuse within the same scope. – user4581301 Jul 08 '18 at 18:05
  • Yes, you should use an if-else statement or add a return statement. And come on, std::getline.cin makes no sense – Victor B. Jul 08 '18 at 18:16
  • Also why do you have two different things named fileName?? – Victor B. Jul 08 '18 at 18:19
  • @notorious can you be more specific? – lauren Jul 08 '18 at 18:33
  • how do I get the program to recognize the integers in the file and sum them? – lauren Jul 08 '18 at 18:34
  • @lauren you have a `std::string fileName` that is a string called fileName. Next, you have a `std::ifstring fileName` which now you have naming conflicts there. You should do your best to avoid that kind of stuff. That is what notorious was getting at. – Sailanarmo Jul 08 '18 at 18:45
  • ok thank you i will try all of these recommendations – lauren Jul 08 '18 at 19:17
  • Go read [C++ Primer](https://www.amazon.com/dp/0321714113). Here: https://pastebin.com/raw/cfSknXJ7 – Victor B. Jul 08 '18 at 19:29

0 Answers0