0

I had an assignment that asked me to read input from a .txt file then instead of displaying them line by line, I was supposed to use a loop to add a line to the one before it and display the sum. So in essence the file has: 1 2 3 4 5 My output was supposed to be: 1 3 5 7 9. There was also a string "stop" which would cause the loop to terminate when encountered. I could get the addition part to work. Any suggestions?

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream inFile;
    int number;
    int sum = 0;
    string word;
    stringstream sso;
    inFile.open("lines.txt");
    
    while (inFile >> word && word != "stop")
    {   
        sso << word;
        sso >> number;
        if (word != "stop")
        { 
            cout << (number + sum) << endl;
            sum = number;
            
        }
        else
        {
            cout << "File reading stopped";
            break;
        }
    }    
    
    
    inFile.close();
    return 0;
}
  • instead of reading word by word, read line by line, then parse the line into individual numbers. I think you could use std::getline with your inFile object. – selbie May 08 '21 at 17:37
  • _Can you sum consecutive lines in a loop in C++_ Yes. (At least, I believe I can.) _Any suggestions?_ Is it a code review? `if (word != "stop")` seems not necessary. There is already a check in `while (inFile >> word && word != "stop")`. However, do you have any problem with your code? – Scheff's Cat May 08 '21 at 17:38
  • You can use `std::stoi` instead of using a stringstream to extract an `int` (you aren't validating if `operator>>` is reading a valid `int` from the stream, also) – mcilloni May 08 '21 at 17:41
  • Also, why does every new learner of C++ on StackOverflow start their programs with a `using namespace std;`? Is it in some book or what? Because that's quite a bad practice, IMHO. – mcilloni May 08 '21 at 17:44
  • @mcilloni yes it is books. Consider that even Stroustrup has it in his examples. The important detail that beginners miss is that it is completely fine in small examples in books or on slides, but not in real code. – 463035818_is_not_an_ai May 08 '21 at 17:50
  • @mcilloni also consider that you cannot blame OP for the fact that "every new learner on Stackoverflow" is using it. – 463035818_is_not_an_ai May 08 '21 at 17:51
  • @mcilloni lastly, you can point them to this q&a [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) to make them understand, because just saying it would be "bad" does't really help – 463035818_is_not_an_ai May 08 '21 at 17:53
  • @Scheff the if statement was because I specifically needed the termination message in the else part but I do agree that it feels redundant. My output is not what I'm looking for. My thinking was that I would store the int value in number then pass it off into sum so i could use it in the addition on the next iteration. The result I'm getting is that it just reads 1 until it gets to "stop". If I remove the addition logic and just have it display the input as is, it works fine. Also the termination on "stop" works just fine. – Burlington1308 May 08 '21 at 17:58
  • @mcilloni It isn't always a bad practice. Implementation files after the includes might make sense: https://stackoverflow.com/a/26722134/7520531 – Jose May 08 '21 at 17:58
  • _the if statement was because I specifically needed the termination message in the else part_ If I didn't overlook something you should never see this message as the `else` branch should never be called. (Your `while` condition prevents this. Or you have to change your `while` condition...) – Scheff's Cat May 08 '21 at 18:01
  • @mcilloni I haven't encountered std::stoi before. As far as validation I am not sure that I would need to do that given that the values are being read as strings then parsed into an int variable. If the value were not an int, would the number variable not just reject it? – Burlington1308 May 08 '21 at 18:07
  • @largest_prime_is_463035818 I wasn't trying to attack OP or even correct they, actually. I was just curious why it was so vastly preponderant with beginners. The fact that's what examples use in books indeed makes sense. – mcilloni May 08 '21 at 19:27
  • @Burlington1308 _If the value were not an int, would the number variable not just reject it?_ - `operator<<` would fail and not read anything, but then your `number` variable would be in an invalid state - given that you didn't read anything, you shouldn't consider it and either warn the user somehow, quit, or skip the invalid value. – mcilloni May 08 '21 at 19:29
  • @Burlington1308 `std::stoi` simply attempts to convert a `std::string` to an int, avoiding you the need to create a `std::stringstream`. It will throw you an exception (which you can catch) if the string doesn't contain a valid integer. – mcilloni May 08 '21 at 19:31
  • 1
    @mcilloni no worries, you asked (in a comment directed to OP), I tried to answer. – 463035818_is_not_an_ai May 08 '21 at 19:35

1 Answers1

0

I finally figured this out. I was missing an << endl at the end of sso << word. That meant that the loop was repeating the first line into the stringstream object instead of going to the next line.