The file has: 65 78 65 89 25 98 -999 87 34 89 99 26 78 64 34 -999 -999 23 99 98 97 26 78 100 63 87 23 -999 62 35 78 99 12 93 19 -999 C++ will not output correctly #include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() { int sum; int num; int counter; ifstream infile; infile.open("Exp_5_24.txt"); counter = 0; infile >> num; while (infile) { sum = 0; sum = sum + num; infile >> num; while (num != -999) { sum = sum + num; infile >> num; } cout << "Line " << counter + 1 << ": Sum = " << sum << endl; counter++; infile >> num; } infile.close(); return 0; } Line 1: Sum = 420 Line 2: Sum = 511 Line 3: Sum = -305 Line 4: Sum = 398 This is what it is supposed to be: Line 1: Sum = 420 Line 2: Sum = 511 Line 3: Sum = 0 Line 4: Sum = 694 Line 5: Sum = 398
Added by James B.
Step 1
This means that the sum of the previous line is added to the sum of the current line, which is why the sum of line 3 is negative and the sum of line 4 is less than expected. Here is the corrected code: ```cpp #include <iostream> #include <fstream> #include Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 58 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Issue: My Results produce an Endless loop Question: A loop that reads positive integers from standard input and terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read (The two sums are separated by a space). Declare any variables that are needed. My Code: int x; int evenSum = 0; int oddSum = 0; cin >> x; while (x > 0) { if ((x % 2) == 0 && (x > 0)) { evenSum += x; } else { oddSum += x; } cout << evenSum << " " << oddSum; }
Henry R.
Integer numVals is read from input representing the number of integers to be read next. Use a loop to read the remaining integers from input. For each integer read in the loop, output "quantity >>" followed by the integer. End each output with a newline. Ex. If the input is: 3 70 95 80 Then the output is: quantity >> 70 quantity >> 95 quantity >> 80 #include <iostream> using namespace std; int main() { int numVals; int value; cin >> numVals; for (int i = 0; i < numVals; i++) { cin >> value; cout << "quantity >> " << value << endl; } return 0; }
Akash M.
Given a line of text as input, output the number of characters excluding spaces, periods, or commas. Ex: If the input is: Listen, Mr. Jones, calm down. the output is: 21 Note: Account for all characters that aren't spaces, periods, or commas (Ex: "r", "2", "!"). #include <iostream> #include <string> using namespace std; int main() { string userText; // Add more variables if needed getline(cin, userText); // Gets entire line, including spaces // Type your code here return 0; }
Deepak K.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD