Any help counting lines in an if stream file

In advance, I would like to thank any contributor for their help. I have a question that asks me to write a C++ code introducing us to reading from files. We are asked to count the number of lines in a text file. For some reason, the error I am getting says

Checking for existence: /home/user/Desktop/partA
Executing: xterm -T '/home/user/Desktop/partA' -e /usr/bin/cb_console_runner "/home/user/Desktop/partA" (in /home/user/Desktop)
Process terminated with status 0 (0 minute(s), 2 second(s))

Here is the code:


// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int howManyLines(string userInput)
{
int count = 0;
string line;

//Creating input filestream

ifstream myFile ("StudentScores1.txt");

if (myFile.is_open())
{
while(getline(myFile,line))
count++;

cout << "Number of lines: " << setw(7) << count;
}
myFile.close();
}

int main(void)
{
return 0;
}

[/code]

main() does not appear to call the howManyLines function. May want to call the function in main and assign the return integer to a variable.

As a side ntoe, use code tags around your code to format it. Makes it easier to read.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void howManyLines(string userInput)
{
int count = 0;
string line;

//Creating input filestream

ifstream myFile ;
myFile.open(userInput.c_str());
if (myFile.is_open())
{
while(getline(myFile,line))
count++;

cout << "Number of lines: " << setw(7) << count<<endl;
}
myFile.close();
}

int main(void)
{
	string choice;
	cin>>choice;

	howManyLines(choice);
	
	
}

the function return type has to be void
then I put a call to the function in main
and it works fine
and when opening the file I used c_str() to change the name to c style string
it worked! thanks a lot, i was a little confused on how to call the function in main. this worked!
Topic archived. No new replies allowed.