-4

Write a simple C++ program called sum.ccp, the program should:
1. Read a file that contains several integers.
2. Print these integers on screen.
3. Add these integers together to sum.
4. Print the sum on the screen.

This is what I have so far:

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

int main() {
    using namespace std;
    ifstream inf("sum.ccp");
    while (inf) {
        std:: string strInput;
        inf >> strInput;
        cout << strInput << endl;
}

}

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 5
    You need to create a variable called sum and add all of the integers into it. – NathanOliver Aug 28 '15 at 22:27
  • 2
    And don't use `std::string` to read an integer. – LogicStuff Aug 28 '15 at 22:33
  • Should I use std:: int – James Johnson Aug 28 '15 at 22:35
  • Also would this be correct for creating a variable called sum and adding integers to it. – James Johnson Aug 28 '15 at 22:36
  • 4
    Please follow a C++ tutorial. You can't just guess a programming language. – juanchopanza Aug 28 '15 at 22:37
  • Would this be correct:sum = strInput – James Johnson Aug 28 '15 at 22:37
  • 2
    @James Johnson There's no `std::int`, it's `int` and it's [built-in type](http://en.cppreference.com/w/cpp/language/types). `sum = strInput` doesn't mean nothing to me or anyone without type information (except you have (again) failed to guess the operator for accumulation). Do as juanchopanza says. – LogicStuff Aug 28 '15 at 22:40
  • Btw your loop is wrong, for the reason explained [here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Baum mit Augen Aug 28 '15 at 22:58
  • You are reading the content of a file called "sum.cpp". That file should be your program, not the one with the numbers. If you have called your file with the numbers like that, please change its name. A .cpp file must contain code, not data. For data you can use a simple .txt file. And since it contains numbers (and not a sum) I'd call it numbers.txt. Oh, and anyway it's .cpp, not .ccp. It's C Plus Plus, not C C Plus. – Fabio says Reinstate Monica Aug 28 '15 at 23:15

1 Answers1

0

Firstly looking at your code, you're already doing some incorrect things.

    int main() {
    using namespace std;
    ifstream inf("sum.ccp");
    while (inf) {
       std:: string strInput;
       inf >> strInput;
       cout << strInput << endl;

}

You need to have using namespace std outside of your main method in order to use the majority of basic C++ commands without tacking on std:: to the beginning. So place it underneath all of the "#include" directives.

Secondly, you should make the variable you're using to take in all of the values an integer, and then simply print the number to the screen with a simple cout command.

The hardest part about your assignment here is just taking the numbers from a text file and using them as integers.

Even then, that's pretty easy. Something like this, using ifstream since you're reading from a file.

    ifstream inputFile;
    inputFile.open(*your file name here, which should be a .txt file. 
    I think it works w/ .doc files as well, 
    but just use a .txt file to be safe.)

After that the process is pretty simple. You do use a while loop, but in this case you use the string insertion operator (the double arrows >>), like so:

    while(inputFile >> //your number variable)

This lets the text file keep looping your stuff, while also inserting the value into the variable from the start. So from there you can just print it to the screen, tally up the total, and then after the loop you can print the total the screen.

Hope that helps!

Josh
  • 1
  • 1