49.7k views
3 votes
Write a fragment of code that reads in strings from standard input, until end-of-file and prints to standard output the largest value. You may assume there is at least one value. (cascading/streaming logic, basic string processing)

User Reginald
by
3.8k points

1 Answer

5 votes

Answer:

Step-by-step explanation:

Sample input file: numbers.txt

8 9 7 67 78 45 67 99 1001

Sample Output:

The largest value is:1001

Code to Copy:

// include stdafx, if using visual studio.

#include "stdafx.h"

// declare the necessary header files.

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

// Start the main function

int main()

{

// create the object of ifstream.

ifstream in_file;

// Open the file

in_file.open("numbers.txt");

// Declare the string variable

string str;

// Declare the integer variables.

int maximum = 0;

int num;

// Check whether the file open

if (in_file.is_open())

{

// Traverse the file till the end.

while (!in_file.eof())

{

// traversing the value.

in_file >> str;

// convert the string into integer.

num = stoi(str);

// Check whether value is max or not.

if (maximum < num)

{

// Update the value of maximum.

maximum = num;

}

}

}

// Display the statement on console.

cout << "The largest value is:" << maximum << endl;

// Close the file.

in_file.close();

system("pause");

return 0;

}

User Bharat Dodeja
by
3.3k points