Question

In this exercise you will be designing a program in c++ that does the following: Prompts...

In this exercise you will be designing a program in c++ that does the following:

Prompts the user for a file name and then shows the user with the number of word in the file. The program should then allow the user to repeatedly specify a number and the program should show that word in the file.

For example, if the file contained the text: Hello. This is the text that is contained in this file.
A sample run of the program might look like this:

Enter file name: in.txt
File contains 10 words. Which one would you like to see? (Enter 0 to exit): 5
The 5th word is "text".
Which word would you like to see next: 0
Good bye!

Note: You may not use an array or any other aggregate structure to store all the numbers as a whole

  • For this first part of the exercise, you will use functional decomposition to develop the logic/algorithm and functions that you will use. You are not writing code at this point. You are simply determining the logical flow of your program and what functions the program should use.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*C++ program that prompts user to enter the name of the file and then ask to enter the word
to see and print the word to console. The program continues until user enters 0 to exit*/

//main.cpp

//include header files iostream
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
string getnthterm(string filename, int nvalue);
int main()
{
   string filename;
   string word;
   cout<<"Enter file name: ";
   getline(cin,filename);

   int numWords=0;
   int n;

   ifstream fin;
   //open file for reading
   fin.open(filename);
   //check if file not exists
   if(!fin)
   {
       cout<<"File not found"<<endl;
       system("pause");
       return -1;
   }
   //Count words
   while(fin>>word)
   {
       numWords++;
   }

   fin.close();

   cout<<"File contains "<<numWords<<" words.";
   cout<<"Which one would you like to see?(Enter 0 to exit): ";
   cin>>n;
   //read till n is not 0
   while(n!=0)
   {
       cout<<"The "<<n<<"th word is "<<getnthterm(filename,n)<<endl;
       cout<<"Which one would you like to see?(Enter 0 to exit): ";
       cin>>n;
   }

   cout<<"Good bye!"<<endl;
   system("pause");
   return 0;
}
string getnthterm(string filename, int nvalue)
{
   ifstream fin;
   fin.open(filename);
   string word;
   int count=0;
   //Count words
   while(fin>>word && count++<nvalue-1);
   //close the file
   fin.close();
   //return word
   return word;

}

in.txt

Hello. This is the text that is contained in this file.

Sample Output:

Enter file name: in. txt File contains 11 words. Which one would you like to see? (Enter 0 to exit): 1 The 1th word is Hello.

Add a comment
Know the answer?
Add Answer to:
In this exercise you will be designing a program in c++ that does the following: Prompts...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, you will write a C program to encrypt a file. The program prompts...

    In this lab, you will write a C program to encrypt a file. The program prompts the user to enter a key (maximum of 5 bytes), then the program uses the key to encrypt the file. Note that the user might enter more than 5 characters, but only the first 5 are used. If a new line before the five characters, the key is padded with 'a'; Note that the new line is not a part of the key, but...

  • Write a program in C++ that prompts the user to input the name of a text...

    Write a program in C++ that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a C program that does the following: • Displays a menu (similar to what you...

    Write a C program that does the following: • Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square, Diamond (with selected height and selected symbol), or Quits if user entered Q. Apart from these 2 other shapes, add a new shape of your choice for any related character. • Program then prompts the user to...

  • Write a C program that prompts the user to enter a series of integers that represent...

    Write a C program that prompts the user to enter a series of integers that represent a coded message. I have given you the decoder program so that you can see how it is written using array notation. Your assignment is to convert the code to a program that uses only pointers and pointer math. Your program must use pointer notation and pointer math (i.e. no array index notation other than in the declaration section, no arrays of pointers and...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • write a python program that prompts the user for a name of a text file, opens...

    write a python program that prompts the user for a name of a text file, opens that file for reading , and tracks the unique words in a file and counts how many times they occur in the file. Your program should output the unique words and how often they occur in alphabetical order. Negative testing for a file that does not exist and an empty file should be implemented.

  • C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

    C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT