-2

i need to find every 10th number in file and define if number is even or odd

i only remember how to read file and dont know what to do next

string linia;
fstream plik;

plik.open("przyklad.txt", ios::in);
if(plik.good() == true)
{
    while(!plik.eof())
    {
        getline(plik, linia);
        cout << linia << endl; 
    }
    plik.close();
}   
system("PAUSE");
return(0);
  • 4
    Start with a loop and a variable to count to 10. – drescherjm May 21 '19 at 13:06
  • 2
    `while(!plik.eof())` is a bug. https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – drescherjm May 21 '19 at 13:06
  • after you read the line, you can loop over space separated word and keep track of numbers. – CaptainDaVinci May 21 '19 at 13:08
  • `if (number % 2 == 0 ) { // number is even } else { //number is odd}` – drescherjm May 21 '19 at 13:08
  • 3
    The downvotes are probably due to the OP not attempting to solve the problem. This is a very simple problem. – drescherjm May 21 '19 at 13:28
  • 2
    @Lrrr let me play the devils advocate: the code OP posted does not even contain a hint of an attempt to solve the problem. Even an absolute beginner can do more. "I have not the slightest idea how to do it please help me" is not really a specific quesiton.... – 463035818_is_not_an_ai May 21 '19 at 14:02
  • 2
    @Lrrr btw it is not "downvotes for the user". I can only speak for myself, but I never vote for/against users, I vote for what is written in the answer/question – 463035818_is_not_an_ai May 21 '19 at 14:10
  • Start with a program that outputs every number from the file. Then make it output every tenth number. Then add the "oddness detector". – molbdnilo May 21 '19 at 14:27
  • Hint: every tenth number is when `line_count % 10 == 0`. – Thomas Matthews May 21 '19 at 14:41
  • @formerlyknownas_463035818 you are right. Thank you for your reasoning – Lrrr May 21 '19 at 16:23
  • 1
    I think this part of the how to handle homework questions is very relevant here ***Make a good faith attempt to solve the problem yourself first. If we can't see enough work on your part your question will likely be booed off the stage; it will be voted down and closed.***: https://meta.stackoverflow.com/a/334823/487892 – drescherjm May 21 '19 at 16:51

1 Answers1

1

Since everyone is so negative about the question, I'm going to go ahead and answer it. We cannot be sure that OP is learning from the correct sources and, as he stated, he doesn't remember what to do next, implying that he doesn't really have an option to look it up.

The working code with some explaining comments is as follows:

#include <iostream>
#include <fstream>

using namespace std;

// Returns wether the number is odd
bool isOdd(const int num){
    return num % 2 != 0;
}

int main(){
    // Open the file in input mode
    ifstream inputFile("data.txt");

    int count(0); // Represents the current line number we are checking
    string line;  // Represents the characters contained in the line we are checking

    // The loop goes over ever single line
    while(getline(inputFile, line)){
        //Increase the line count by 1
        ++count;

        //If the line we are on is either 0 (not possible due to instant increment), 10, 20, 30, ...
        if(count % 10 == 0){
            //Get the number (not safe since it's not guaranteed to be a number, however it's beyond the scope of the question)
            int number = stoi(line);

            //Ternary statement can be replaced with a simple if/else
            cout << "The number " << number << " is " << (isOdd(number) ? "odd" : "even") << '\n';
        }
    }

    return 0;
}
Rokas Višinskas
  • 533
  • 4
  • 12