5 Replies - 16989 Views - Last Post: 13 August 2012 - 07:23 PM Rate Topic: -----

#1 cniper   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

counting number of lines in given text file

Posted 13 August 2012 - 05:00 PM

i am suppose to write a program where it asks a user to input a text file location and then counts the number of lines in this file. output may look as:
enter a text file:c:/data/file.txt
c:/data/file.txt contains 478 lines

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
	string s,t;
	int count=0;
	cout<<"enter a text file :";
	cin>>s;
	ifstream inFile(s);
	while(!inFile.eof())
	{
		getline(inFile,t);
		count++;
	}
	cout<<s<<"contains "<<count<<"lines";
}




problem is when i enter a location nothing happens.

Is This A Good Question/Topic? 0
  • +

Replies To: counting number of lines in given text file

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: counting number of lines in given text file

Posted 13 August 2012 - 05:05 PM

Turn up your compiler warnings.

	ifstream inFile(s.c_str());


I'd use get line() though.
Was This Post Helpful? 2
  • +
  • -

#3 LaughingBelly   User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 109
  • Joined: 11-April 11

Re: counting number of lines in given text file

Posted 13 August 2012 - 05:05 PM

What do you mean by nothing happens?
- your input is not accepted?
- blank screen?

I think it would help to clarify what you mean by stating what you expect the output of your program to be and what the actual output of your program is.

Also, how are you running your program? Are you sure your program did not run as opposed to running really quickly and closing out on your from your faviourite IDE?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5521
  • View blog
  • Posts: 17,113
  • Joined: 25-December 09

Re: counting number of lines in given text file

Posted 13 August 2012 - 05:09 PM

View PostCTphpnwb, on 13 August 2012 - 07:05 PM, said:

	ifstream inFile(s.c_str());


Actually if your compiler supports C++11 using a std::string in the ifstream constructor or the ifstream.open() command is now supported.

Jim

This post has been edited by jimblumberg: 13 August 2012 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 cniper   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: counting number of lines in given text file

Posted 13 August 2012 - 05:19 PM

@CTphpnwb thanks for the help. it worked like charm. now could anyone suggest how am i supposed to increase my warning level in visual c++. any help would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6292
  • View blog
  • Posts: 21,622
  • Joined: 05-May 12

Re: counting number of lines in given text file

Posted 13 August 2012 - 07:23 PM

What version of "Visual C++" do you have?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1