Question

Using the variables shown below and a EOF-controlled loop write a set of C++ statements that...

Using the variables shown below and a EOF-controlled loop write a set of C++ statements that accumulate the values read from the file in total as long as total does not get greater than max.

For example, if the content of the input file is: 10 25 17 6 24 23 12 5

For max = 100, the value in total should be 82 (10+25+17+6+24)

For max = 75, the value in total should be 58 (10+25+17+6)

Assume that max will never make your loop to read beyond the end of the file and the values in the file are all valid. Implement the loop using a while statement.

Initialize variables according to your needs. Display the value of total.

ifstream myInfile;

myInfile.open("input.txt");

int value;           // value read from file

int total;

int max = 75;

Must be in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <fstream>
using namespace std;

/*Make sure to provide input.txt file at appropriate location
 * where your compiler can read text file
 * also make sure that name of input file must be input and
 * it must be a text file is having txt extension*/

int main(){
    ifstream myInfile;
    myInfile.open("input.txt");
    int value;
    int total;
    int max = 75;   /*try to change this value but make sure that it does not let the
 * program to reach the end of file as it is assured in the question that max will be
 * choosen such that it will never reach to end of file*/

    /*Below condition will test whether opening of text file was successful or not
     * you can delete this if code */
    if(!myInfile){
        cout << "Unable to open input file!\n";
    }

    total = 0;
    while(total < max){
        myInfile >> value;  //reading int by int from input.txt file

        if(total + value < max){
            total += value;
        }
        else{
            break;
        }
    }

    cout << "Sum total : "<<total << "\n";

    return 0;
}

Run CPROJECT c: \Users\VIREN CLionProjects\CPROJECT\Cmake-build-debug\CPROJECT.exe Sum total : 58 Process finished with exit

Add a comment
Know the answer?
Add Answer to:
Using the variables shown below and a EOF-controlled loop write a set of C++ statements 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
  • Assignment • No variables declared after you start writing code, must be declared at the top....

    Assignment • No variables declared after you start writing code, must be declared at the top. • Reuse all the variables from the first half in the second half, do not make up new variable names. • You code must be properly formatted. • You must use the code provided and variables names provided and not add any more variables to your code. • No for(int I = 0; … no declaring variables except at top of code. • You...

  • 17. (2 points) C++ Using a while loop, write the code statements to sum 10 integers...

    17. (2 points) C++ Using a while loop, write the code statements to sum 10 integers entered by a user. Display the sum. Hint: Be sure to declare, initialize, and use appropriate variables to count the repetitions and accumulate the sum.

  • Given that two int variables, total and amount, have been declared write a loop that reads...

    Given that two int variables, total and amount, have been declared write a loop that reads non-negative values into amount and adds them into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0. To read a value into amount use a method, getNum() that we provide for you a class named TC:    amount = TC.getNum(); Instructor Notes: amount = TC.getNum(); TC is the name of a class program....

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • Question 21 The loop condition can be a complex condition involving several operators. True OR False...

    Question 21 The loop condition can be a complex condition involving several operators. True OR False Question 22 final int MAX = 25, LIMIT = 100; int num1 = 12, num2 = 25, num3 = 87; if(num3-5 >= 2*LIMIT) { System.out.println ("apple"); } else { System.out.println ("orange"); } System.out.println("grape"); What prints? A. Apple B. Orange C. Grape D. apple grape E. orange grape F. Nothing. Question 23 When we use a while loop, we always know in advance how many...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace...

    How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace std; void read(int arr[]) {    string line;    ifstream myfile("input.txt");    int i = 0;    if (myfile.is_open())    {        while (getline(myfile, line))        {            arr[i];            i++;        }        myfile.close();    } } void output(int arr[]) {    ofstream myfile("output.txt");    if (myfile.is_open())    {        int i = 0;...

  • CODE MUST BE IN C 1. Given an int variable n that has been initialized to...

    CODE MUST BE IN C 1. Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have alreadybeen declared, use a while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and...

  • [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a...

    [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a starting month name, an ending month name, and then the monthly rainfall for each month during that period. As it does this, it should sum the rainfall amounts and then report the total rainfall and average rainfall for the period. For example, the output might look like this: During the months of March–June the total rainfall was 7.32 inches and the average monthly rainfall...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

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