Ask Question
3 December, 17:52

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)

+3
Answers (1)
  1. 3 December, 17:59
    0
    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

    #include

    #include

    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;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers