Using C++ language and a file attached full of words lines
np pointers
basic code please
how can i write a function that grabs randomly(number input by the user) lines from the file and print them using..... while (getline(name of the file, strng){
If you have any doubts, please give me comment...
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void printNLines(ifstream &in, int n);
int main(){
ifstream in;
int n;
in.open("input.txt");
if(in.fail()){
cout<<"Unable to open input.txt"<<endl;
return -1;
}
cout<<"Enter number of lines you need: ";
cin>>n;
printNLines(in, n);
in.close();
return 0;
}
void printNLines(ifstream &in, int n){
string line;
int line_no=0;
while(getline(in, line) && line_no<n){
cout<<"Line "<<line_no<<": "<<line<<endl;
line_no++;
}
}
Get Answers For Free
Most questions answered within 1 hours.