Question

Using C++ language and a file attached full of words lines np pointers basic code please...

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){

Homework Answers

Answer #1

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++;

    }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Attached is a text file full of names. Write a program that prompts the user to...
Attached is a text file full of names. Write a program that prompts the user to type in a name. If the name appears in the list of names, the program prints a message indicating the name was found. If the name entered by the user is not in the database, the program prints a different message indicating the name was not found. The program will continue prompting the user and searching for names until the user enters "quit". The...
WRITE USING PYTHON PROGRAMMING THE CODE GIVEN BELOW HAS SOME ERRORS WHICH NEED TO BE SOLVED....
WRITE USING PYTHON PROGRAMMING THE CODE GIVEN BELOW HAS SOME ERRORS WHICH NEED TO BE SOLVED. ALSO THE 2 POINTS MENTIONED BELOW SHOULD BE PRESENT IN THE CODE Write a script that calculates the 3 longest words of a text stored in a file and print them from the longest to the smaller of the 3. Please note: 1. The name of the file is word_list.csv and it doesn’t need to be asked to the user (meaning the name will...
please write a LONG basic piece of code in Python language that focuses on using Variables,...
please write a LONG basic piece of code in Python language that focuses on using Variables, please do not make the code short! thank you!
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
Problem: Our Armstrong number Please write code for C language So far we have worked on...
Problem: Our Armstrong number Please write code for C language So far we have worked on obtaining individual digits from 4 digits or 5 digit numbers. Then added them to find the sum of digits in various examples and assignments. However, the process of extracting individual digits is actually can be solved using a loop as you were doing a repetitive task by using mod operation and division operation. Now, we know how loops work and we can remove the...
Please provide commenting of code so I can understand how to solve this problem. Please provide...
Please provide commenting of code so I can understand how to solve this problem. Please provide text files. Using C++ Write a program that reads a given file, and then outputs the contents of it to another file. It should also print out the number of lines and the number of times each alphabetic character appears (regardless of case) in the input file at the end of the output file. It should prompt the user for the input and output...
(PLEASE USE C++) (PLEASE USE THE EXAMPLE GIVEN) Question 1: [10 pointes] the input file inFile.txt...
(PLEASE USE C++) (PLEASE USE THE EXAMPLE GIVEN) Question 1: [10 pointes] the input file inFile.txt stores a number of words with possible replication. Each word is stored on a separate line. The number of words within inFile.txt is stored as the first line in that file. Write and test a program that reads the words from inFile.txt and prints them in outFile.txt  without replication as well as the number of replication for each word. Hint: to implement the above program,...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
By using Python code answer the following parts: A) Write a program to ask the user...
By using Python code answer the following parts: A) Write a program to ask the user to input their name and then output the type and length of the input. B) Write a program to output the last 11 letters of 'Introduction to Indiana'. C) Write a program to output the 3rd to the 11th letters of 'Introduction to Indiana'. D) Write a program to ask the user to input a float number and output the rounded value of it...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary Keys Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT