-3
#include<iostream.h>
 #include<fstream.h>
int main()
{
    ifstream infile("text.txt");
    char ch[50];
    int count=0,i;
    for(i=0;infile.eof()==0;i++)
    {
        infile.getline(ch,50);
        if(ch[i]=='\n')
            if(ch[i-1]=='.')
                count++;
    }
    cout<<"Total number of lines are:"<<count;
}

I tried this code but it doesnt seem to work.I used the logic to take all the file contents in ch and then subsequently check for newline character and '.'
How do I make it work.
Please help?

EDIT new code

#include<iostream.h>
#include<fstream.h>
int main()
{
    ifstream infile("text.txt");
    char ch[50];
    int count=0,i;
    while(!infile.eof())
    {

        infile.getline(ch,50);
        for(i=1;ch[i]!='\n';i++);
        if(ch[i-1]=='.')
                count++;    

    }
    cout<<"Total number of lines are:"<<count;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user3500780
  • 11
  • 1
  • 8
  • add some debugging to your loop to look at what you read in, your index variable etc... – Nim Jun 14 '16 at 09:01
  • @Nim i tried that, I guess the loop is not running properly – user3500780 Jun 14 '16 at 09:02
  • don't guess, try to understand what is happening with that variable, and what `getline()` is doing... – Nim Jun 14 '16 at 09:03
  • ...and how often it is doing it... – John Burger Jun 14 '16 at 09:04
  • You're checking wrong. variable `i` in this case counts the number of lines you've read, yet you use it to access the index inside an array of chars. – DaMachk Jun 14 '16 at 09:08
  • Please read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude Jun 14 '16 at 09:10
  • 4
    As for your problem, learn about [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) and [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline). – Some programmer dude Jun 14 '16 at 09:12
  • also, `eof()` returns a boolean, don't compare it to a number, specially where a boolean is expected! – Garf365 Jun 14 '16 at 09:18
  • With new code, you don't care about some of comments : **don't use** `eof` in condition of `while` and don't compare boolean variable with an integer (just write `!infile.eof()` or `infile.eof() == false`) – Garf365 Jun 14 '16 at 09:30
  • 2
    Read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Jun 14 '16 at 09:38
  • @user3500780 some advice still missing, see JoachimPileborg and molbdnilo comments about http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Garf365 Jun 14 '16 at 09:45
  • @molbdnilo I don't understand what is conveyed in the link :/ – user3500780 Jun 14 '16 at 09:47
  • 1
    Tagging the question as [tag:turboc++], since you are using a primordial 1980s dialect of the language. – Lightness Races in Orbit Jun 14 '16 at 09:55
  • @LightnessRacesinOrbit tell me a thing tho, aren't almost all language same in structure and logic but different in keywords?? – user3500780 Jun 14 '16 at 14:13
  • @LightnessRacesinOrbit are they very different? – user3500780 Jun 14 '16 at 14:52
  • @user3500780: Yes, extremely. Compare FORTRAN with Haskell with Java with Prolog with Brainfuck with etc etc Perhaps you're only considering imperative and OO-like languages, many of which are superficially similar at least. – Lightness Races in Orbit Jun 14 '16 at 15:42

3 Answers3

0
  1. you have problem with long lines
  2. you have problem with '\r' on windows
  3. you are not looking properly at the end of line.
  4. in the last line you may not have '\n'

check out this solution:

#include <stdlib.h>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    ifstream infile("text.txt");
    string str;
    int count=0;
    while(std::getline(infile,str))
    {
        string::reverse_iterator it=str.rbegin();
        while(it != str.rend() && iswspace(*it)) it++;
        if(*it =='.') count++;
    }
    cout<<"Total number of lines are:"<<count;
    return 0;
}
SHR
  • 7,940
  • 9
  • 38
  • 57
0

Your program is undefined if any line is longer than fifty characters (the original version is undefined if the file is longer), your end-of-file test is wrong, and the last line doesn't necessarily end with a newline.

You might write it something like this.

int main()
{
    ifstream infile("text.txt");
    std::string line;
    int count = 0;
    while (infile.getline(line))
    {
        if (line.back() == '.')
        {
            count += 1;
        }
    }
    cout << "Total number of lines are:" << count;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

Since you have mentioned 50 as maximum line size. I tried to write a code based on that assumption

int main()
{
    ifstream infile("text.txt");
    char str[50]; int strSize = 50;
    int count = 0;

    while (infile.getline(str, strSize, '.')) {
        count++;
    }

    cout << "Total number of lines are:" << count << endl;

    infile.close();
    return 0;
}

This code basically counts the number of lines based on number of '.' present in a line, whether a sentence is in a new line or not.

oya163
  • 1,371
  • 2
  • 16
  • 20