0

How do I direct the output of this code into a .txt file?

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

using namespace std;

int main() {

    using std::cin;
    using std::cout;
    using std::endl;

    int input=1; 
    int sum=0; 
    int counter=1;

    while (input != 0) 
    {
        std::cout << "Please enter the hit data: "; 
        std::cin >> input; 

        if (input == 0) // after puting in data input zero 
        {
            break;
        }
        else if (input != 0)
        {
            sum += input; 
            counter++; 
        }
    }
    counter = counter - 1 ; 
    std::cout << "Sum of hits entered: " << sum << endl ; 
    std::cout << "Number of hits entered: " << counter << endl ;

    if ( counter < 100 ) 
    {
        std::cout << "The hits are less than 100" ;
    }
    else if ( counter > 100 ) 
    {
        std::cout << "The hits are greater than 100" ;
    }
    else if ( counter == 100 ) 
    {
        std::cout << "The hits are equal to 100" ;
    }
}

Also, instead of a user having to input data, how can I get the program to read data from another .txt file? I understand you can do this all easily in the terminal; however, I would like for the program to create the .txt file.

Also, how do I get the program to recognize certain numbers? I want it to output something like "there was twelve number -11s counted".

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
conDon
  • 9
  • 2
  • 1
    Hint: you already took the first step: [`#include `](http://en.cppreference.com/w/cpp/header/fstream). What tutorial and/or book are you using that does *not* cover basic file IO ? – WhozCraig May 30 '18 at 18:53

2 Answers2

1

Use std::ifstream to read input from a file, and std::ofstream to write output to a file. For example:

#include <iostream>
#include <fstream>

int main()
{
    int sum = 0; 
    int counter = 0;

    std::ifstream in("hits.txt");
    if (in.is_open())
    {
        while (in >> input)
        {
            sum += input; 
            ++counter;
        }
    }
    else
    {
        std::ofstream out("hits.txt");
        int input;

        do
        {
            std::cout << "Please enter the hit data: "; 

            // after putting in data, input zero 
            if (!(std::cin >> input) || (input == 0))
                break; 

            out << input << " ";

            sum += input; 
            ++counter;
        }
        while (true);
    }

    std::cout << "Sum of hits entered: " << sum << endl ; 
    std::cout << "Number of hits entered: " << counter << endl ;

    if (counter < 100)
    {
        std::cout << "The hits are less than 100" << std::endl;
    }
    else if (counter > 100) 
    {
        std::cout << "The hits are greater than 100" << std::endl;
    }
    else
    {
        std::cout << "The hits are equal to 100" << std::endl;
    }

    return 0;
}

Also, how do I get the program to recognize certain numbers? I want it to output something like "there was twelve number -11s counted".

You can use std:map for that, eg:

#include <iostream>
#include <fstream>
#include <map>

int main()
{
    int sum = 0; 
    int counter = 0;
    std::map<int, int> hits; // hit counter

    std::ifstream in("hits.txt");
    if (in.is_open())
    {
        while (in >> input)
        {
            hits[input]++;
            sum += input; 
            ++counter;
        }
    }
    else
    {
        std::ofstream out("hits.txt");
        int input;

        do
        {
            std::cout << "Please enter the hit data: "; 

            // after putting in data, input zero 
            if (!(std::cin >> input) || (input == 0))
                break; 

            out << input << " ";

            hits[input]++;
            sum += input; 
            ++counter;
        }
        while (true);
    }

    std::cout << "Sum of hits entered: " << sum << endl ; 
    std::cout << "Number of hits entered: " << counter << endl ;

    if (counter < 100)
    {
        std::cout << "The hits are less than 100" << std::endl;
    }
    else if (counter > 100) 
    {
        std::cout << "The hits are greater than 100" << std::endl;
    }
    else
    {
        std::cout << "The hits are equal to 100" << std::endl;
    }

    for (auto &p : hits)
    {
        if (p.second == 1)
            std::cout << "there was 1 number " << p.first << " counted" << std:::endl;
        else
            std::cout << "there were " << p.second << " number " << p.first << "'s counted" << std:::endl;
    }

    /* or, if you are not using C++11 or later:

    for (std::map<int, int>::iterator iter = hits.begin(); iter != hits.end(); ++iter)
    {
        std::map<int, int>::value_type &p = *iter;

        if (p.second == 1)
            std::cout << "there was 1 number " << p.first << " counted" << std:::endl;
        else
            std::cout << "there were " << p.second << " number " << p.first << "'s counted" << std:::endl;
    }
    */

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • okay thank you it worked great. now I have another question. how do i get the program to recognize certain numbers? I want it to output something like "there was twelve number -11s counted" – conDon May 30 '18 at 19:45
  • You can use a [`std::map`](http://en.cppreference.com/w/cpp/container/map) to keep track of them, eg: `std::map hits; ... hits[input]++;` Then you can loop through `hits` outputting the values as needed. – Remy Lebeau May 30 '18 at 19:53
  • @conDon And? Do you not understand what it is telling you? "*'auto' type specifier is a C++11 extension ... range-based for loop is a C++11 extension*" - you are clearly not compiling for C++11, or else you would not be getting the warnings. So, you will have to use a more traditional loop using iterators instead. I updated my answer to show that. – Remy Lebeau May 30 '18 at 20:23
0

Outputting data to a .txt file is easy indeed. You already included , now you need to create an object from the type std::ofstream and use it to write your text into a file. I would create a function like this (above main):

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

void outputTextToFile (std::string p_text) {
//is created under your project filepath:
  std::ofstream file("nameoffile.txt", std::ios::app); //"app" = appending, instead of overwriting text
  file << "Writing this to a file.\n";
  file.close();
}

Afterwards you can call your function in the while loop with the string text you want, like this for example:

outputTextToFile("test Text");

Reading text from a .txt file is very similar to this, I would suggest you look up this thread: Read file line by line

marCOmics
  • 1
  • 1