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++;
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace 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