Question

I need help finding the several errors and several loop errors for I get the final...

I need help finding the several errors and several loop errors for I get the final output. C++ cLion

#include <iostream>
using namespace std;

int main() {
cout << "Welcome to Loop World" << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section I" << endl;
cout << "******************" << endl;

int sum; // Accumulates the total
int i; // Used as loop control variable
int numIter; // The number of times to iterate
cout << "Enter a number greater than 1 to sum up to: ";
cin >> numIter;
for (i = 1; i < numIter; ++i) {
sum += i;
}
cout << "The sum of the numbers from 1 to " << numIter << " (inclusive) is: " << sum << endl;


cout << endl;
cout << "******************" << endl;
cout << "Section II" << endl;
cout << "******************" << endl;

double total; // Accumulates total
double price; // Gets next price from user
int numItems; // Number of items
int counter = 1; // Loop control counter

cout << "How many items do you have? ";
cin >> numItems;
cout << endl;

while (counter <= numItems) {
total = 0;
cout << "Enter the price of item " << counter << ": ";
cin >> price;
cout << endl;
total += price;
counter++;
}
cout << "The total price is: " << total << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section III" << endl;
cout << "******************" << endl;

int sum2 = 0;
int counter2 = 1;
int numIter2;

cout << "What number do you wish me to sum to?" << endl;
cin >> numIter2;

do {
sum2 += counter2;
cout << "Sum so far: " << sum2 << endl;
} while (counter2 <= sum2);

cout << endl << "Section III Recap" << endl;

cout << "I calculated the sum of numbers from 1 to " << numIter2 << " (inclusive) as " << sum2 << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section IV" << endl;
cout << "******************" << endl;

cout << "I will now calculate ";
cout << "the sum of squares from 1 to ? (inclusive)" << endl;
  
int numIter3;
cin >> numIter3;

int sum3 = 0;
for (int i3 = numIter3; i3 > 0; i3++) {
sum3 += i3*i3;
}

cout << "The sum of squares from 1 to " << numIter3 << " is: " << sum3 << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section V" << endl;
cout << "******************" << endl;

cout << "I will now calculate ";
cout << "the sum of cubes from 1 to ? (inclusive)" << endl;

int numIter4;
cin >> numIter4;

int sum4 = 0;
int counter4 = 1;

while (counter4 < 10) {
sum4 += (counter4 * counter4 * counter4);
}

counter4++;

cout << "The sum of cubes from 1 to " << numIter4 << " is: " << sum4 << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section Done" << endl;
cout << "******************" << endl;

cout << endl << "Congrats! You fixed them all (hopefully correctly!)" << endl << endl << "Goodbye" << endl << endl;

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Wherever I have changed the code, I have commented like "// G - Changed i<num......... ", means comments starting with G have been made by me. I've also included screenshots of the results of the code.

#include <iostream>

using namespace std;

int main()
{
cout << "Welcome to Loop World" << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section I" << endl;
cout << "******************" << endl;

int sum; // Accumulates the total
int i; // Used as loop control variable
int numIter; // The number of times to iterate
cout << "Enter a number greater than 1 to sum up to: ";
cin >> numIter;
for (i = 1; i <= numIter; ++i) { // G - Changed i<numIter to i<=numIter to include the final number in sum
sum += i;
}
cout << "The sum of the numbers from 1 to " << numIter << " (inclusive) is: " << sum << endl;


cout << endl;
cout << "******************" << endl;
cout << "Section II" << endl;
cout << "******************" << endl;

double total; // Accumulates total
double price; // Gets next price from user
int numItems; // Number of items
int counter = 1; // Loop control counter

cout << "How many items do you have? ";
cin >> numItems;
cout << endl;

total = 0; // G - Initialisation of total was inside the while loop previously which would get reset on every iteration, so took it out
while (counter <= numItems) {
cout << "Enter the price of item " << counter << ": ";
cin >> price;
cout << endl;
total += price;
counter++;
}
cout << "The total price is: " << total << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section III" << endl;
cout << "******************" << endl;

int sum2 = 0;
int counter2 = 1;
int numIter2;

cout << "What number do you wish me to sum to?" << endl;
cin >> numIter2;

do {
sum2 += counter2;
counter2++; // G - Added counter increment
cout << "Sum so far: " << sum2 << endl;
} while (counter2 <= numIter2); // G - Changed sum2 to numIter2

cout << endl << "Section III Recap" << endl;

cout << "I calculated the sum of numbers from 1 to " << numIter2 << " (inclusive) as " << sum2 << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section IV" << endl;
cout << "******************" << endl;

cout << "I will now calculate ";
cout << "the sum of squares from 1 to ? (inclusive)" << endl;
  
int numIter3;
cin >> numIter3;

int sum3 = 0;
for (int i3 = numIter3; i3 > 0; i3--) { // G - Changed i3++ to i3-- because the loop is adding square of highest value first, then coming down in a descending manner
sum3 += i3*i3;
}

cout << "The sum of squares from 1 to " << numIter3 << " is: " << sum3 << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section V" << endl;
cout << "******************" << endl;

cout << "I will now calculate ";
cout << "the sum of cubes from 1 to ? (inclusive)" << endl;

int numIter4;
cin >> numIter4;

int sum4 = 0;
int counter4 = 1;

while (counter4 <= numIter4) { // G - Changed counter4<10 to counter4<numIter4
sum4 += (counter4 * counter4 * counter4);
counter4++; // G - Inserted counter4 increment which was previously immediately outside the while loop
}

cout << "The sum of cubes from 1 to " << numIter4 << " is: " << sum4 << endl;

cout << endl;
cout << "******************" << endl;
cout << "Section Done" << endl;
cout << "******************" << endl;

cout << endl << "Congrats! You fixed them all (hopefully correctly!)" << endl << endl << "Goodbye" << endl << endl;

return 0;
}

Add a comment
Know the answer?
Add Answer to:
I need help finding the several errors and several loop errors for I get the final...
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
  • Find and fix the errors in this C++ code: * This program illustrates a variety of...

    Find and fix the errors in this C++ code: * This program illustrates a variety of common loop errors. * Fix the errors in each section. */ #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; // SECTION I: update comment below on how you fixed this section's code, and tests run // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout <<...

  • Class: C++ Final Exam Dates Multiple Choice Identify the choice thar best completes the statement or...

    Class: C++ Final Exam Dates Multiple Choice Identify the choice thar best completes the statement or answer the question N 1. In s tructures, the computer repeats particular statements a certain number of times depending on some condition(s) a. looping C. selection b. branching d. sequence 2. What is the output of the following CH code? count = 1; num - 25; while (count < 25) numnum - 1: count++; cout << count << " " << num << endl;...

  • Finish the following program which adds up all integers from 0 to the user's given number inclusively using a While Loop

    // Finish the following program which adds up all integers from 0 to// the user's given number inclusively using a While Loop. The total should be// assigned to the variable 'total'.#includeusing namespace std;int main() {int number;int total = 0;int counter = 0; //initialize the variable// user enters a numbercout << "Enter a positive integer to find the summation of ";cout << "all numbers from 0 to the given number up to 100." << endl;cin >> number;// check for invalid user...

  • I need help in my C++ code regarding outputting the enums in string chars. I created...

    I need help in my C++ code regarding outputting the enums in string chars. I created a switch statement for the enums in order to output words instead of their respective enumerated values, but an error came up regarding a "cout" operator on my "Type of Item" line in my ranged-based for loop near the bottom of the code. I tried casting "i.type" to both "i.GroceryItem::Section::type" and "i.Section::type", but neither worked. Simply put, if a user inputs 1 as their...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • Hello, I am trying to get this Array to show the summary of NETPAY but have...

    Hello, I am trying to get this Array to show the summary of NETPAY but have been failing. What is wrong? #include <iostream> #include <iomanip> using namespace std; main(){ char empid[ 100 ][ 12 ];    char fname[ 100 ][ 14 ], lastname[ 100 ][ 15 ];    int sum; int hw[ 100 ]; double gp[ 100 ], np[ 100 ], hr[ 100 ], taxrate[100], taxamt[ 100 ]; int counter = 0; int i; cout<<"ENTER EMP ID, FNAME, LNAME, HRS...

  • im writing a c++ code and getting stuck on two parts. The first part, when I...

    im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the...

  • I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using...

    I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {            ...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

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