Question

In: Computer Science

(write a program that get the numbers from user and search the file numbers.text for that...

(write a program that get the numbers from user and search the file numbers.text for that value. in C++)
numbers.txt:
10
23
43
5
12
23
9
8
10
1
16
9

you must to have the exact output:

Enter a number: 10
10 last appears in the file at position 9
Enter a number: 29
29 does not appear in the file
Enter a number: 9
9 last appears in the file at position 12
Enter a number:

Solutions

Expert Solution

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

fstream inputFile;

int count, number,target,found=0;

count = 0;

//opening the file

inputFile.open("numbers.txt");

if (inputFile.fail()){

cout << "File open error....";

return 0;

}

//reading the number

cout<<"Enter number : ";

cin>>target;

while (!inputFile.eof()){

count++;

inputFile >> number;

inputFile.ignore();

//checking if this is the number

if(number==target){

found=count;

}

}

//printing the details

if(found!=-1)

cout<<target<<" last appears in the file at position "<<found<<endl;

else

cout<<target<<" does not appear in the file";

inputFile.close();

return 0;

}




Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user on one line (digits are separated by white space) and adds them to a list. The first digit and the last digit should not be a zero. If the user provides an invalid entry, the program should prompt for a new value. Converts every entry in the list to an integer and prints the list. The digits in the list represent a non-negative integer....
using python3 Write a Python program that lets a user search through the 'data.txt' file for...
using python3 Write a Python program that lets a user search through the 'data.txt' file for a given first name, last name, or email address, and print all the matches/results. Prompt the user for which field to search, (f) for first name, (l) for last name, or (e) for email data.txt entries are all formatted as: first_name, last_name, email, separated by TABS Your program should not read the entire file into memory, it should read line by line, and only...
Write a program that will print the whole numbers from a user-specified minimum to a user-specified...
Write a program that will print the whole numbers from a user-specified minimum to a user-specified maximum. Display the total amount of numbers printed.
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input (other than the sentinel). Do the following: 1. Output the sum of all even numbers 2. Output the sum of all odd numbers 3. Output the count of all even numbers 4. Output the count of all odd numbers You must use alternation ('if' statements), loops and simple calculations to do...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
C programming Get a number from the user and write a program that checks to see...
C programming Get a number from the user and write a program that checks to see if the inputted number is equal to x*x+x and x must be greater than 0. For example, if the user inputs 12. The program should check to see if 12 = x*x+x. In this case it is true because 3*3+3 = 12.
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Write a program that prompts the user to enter a file name, then opens the file...
Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT