Question

Write a C++ program that pick up a random number in text file name number.txt that...

Write a C++ program that pick up a random number in text file name number.txt that contains numbers from 6-9.

Cout the number the program picked

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**
* ============================================
*            File Name : read numbers.cpp
* ============================================
* Description: Generates a random index based on total numbers in the
* file . reads till the random index is 0 and prints the number at
* index random index generated.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include<cstdlib>
#include<ctime>
#define TOTAL_NUMBERS_IN_FILE 4
using namespace std;
int isValidInt(string strNum){
   stringstream ss(strNum);
   int num;
   ss >> num;
   return ss.fail() ? -1 : num;
  
}

void readRandomNumberFromFile(string filename){
   ifstream inp(filename.c_str());
   if(!inp)
   {
       cout<<"\nUnable to find file : "<< filename<<endl;
       return;
   }
   int randInd = rand() % TOTAL_NUMBERS_IN_FILE;
   int num;
   while(randInd >=0 && !inp.eof())
   {
       inp >> num;
       randInd--;
   }
   cout<<"\nRandom Number read from file : "<<num<<endl;
   inp.close();
  
}
int main()
{
   srand(time(0));
   readRandomNumberFromFile("number.txt");
   return 0;
}

number.txt file

6 7 8 9

C:\Windows\SYSTEM32\cmd.exe Random Number read from file : 7 Build <program exited with code: 0 Press any key to continue. ErC:\Windows\SYSTEM32\cmd.exe Random Number read from file : 8 Build <program exited with code: 0 Press any key to continue Ers

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that pick up a random number in text file name number.txt that...
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
  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 IN JAVA PLZ 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4...

  • Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and...

    Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt)...

    Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat).The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • Please use Python for this program Random Number File Writer Write a program that writes a...

    Please use Python for this program Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range 1 through 500. The program should let the user specify how many random numbers to put into the file. The name of the file to write to should be 'random.txt'. Submit the random.txt file generated by your program with the assignment. Sample program execution: Python 3.4.3 Shell Eile Edit...

  • Question 7 (a) Write a C program that performs the following tasks: 1. Create a text...

    Question 7 (a) Write a C program that performs the following tasks: 1. Create a text file to store a list of numbers. 2. Read a number entered by a user and store it into the text file until a negative number is entered. (b) Write a C program that performs the following tasks: 1. Open the text file that contains a list of numbers. 2. Print the list of numbers stored in the text file on the screen.

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • Write a program in C to generate random numbers. The program recieves 4 items on activation...

    Write a program in C to generate random numbers. The program recieves 4 items on activation through argv: 1. Name of the data file 2. Number of items for program to generate 3. Range of numbers 4. Seed of the random number generator Example run: ./rand datafile.txt 20 1000 3127 Program must convert all numbers to ints using atoi(), must save all parameters into variables, opens the data file to write the random numbers into it, program loops to generate...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

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