Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Nested Loops – Lab3C.cpp Loops often have loops inside them. These are called “n

ID: 3748494 • Letter: N

Question

Nested Loops – Lab3C.cpp

Loops often have loops inside them. These are called “nested” loops. In this exercise you will finish a nested loop that reads lines of numbers from a file and computes and prints out the sum of each line.

//C++ Lab3C.cpp

//1. [include required header files]//

using namespace std;

int main()

{

//[2. Declare required variables]

   try

   {

        //3> put your file name & Open file as an input mode

        ifstream Openfile(" “);

       

        //4> If file doesn't exist then throw error number

        if(Openfile.good())

        {

               while( getline(Openfile, sLine)) //Outer While Loop Condition

               {

                  cout << "The Contents of line sLine" << sLine << " ";

              

                  stringstream Str(sLine);

                    while (Str >> temp ) //Inner While Loop Condition

                  {

                    cout << "String ~to double" << temp;

                  }// Inner while Loop

              

           } //Outer While Loop

           Openfile.close();

   //3> Catch the error number and display message

        }

        else {

               throw 10;

        }

   } //if

   catch(int e)

   {

        cout << "File Not found " << e << endl;

   }

   //system("pause"); //Pause

   return 0;

}//main

Once you have completed the program, run it on the input file below:

Lab4C.in

10 20 30 40 50

60 70 80 90.0

11 13.0

20 40 70 19.0

Output should be as follows:

Read Line 0 10.0 Sum 10.0

Read Line 0 20.0 Sum 30.0

Read Line 0 30.0 Sum 60.0

Read Line 0 40.0 Sum 100.0

Read Line 0 50.0 Sum 150.0

…..

Read Line 3 20.0 Sum 20.0

Read Line 3 40.0 Sum 60.0

Read Line 3 70.0 Sum 130.0

Read Line 3 19.0 Sum 149.0

Explanation / Answer

// C++ implementation of given text file which

// contains any type of characters. We have to

// find the sum of integer value.

#include <bits/stdc++.h>

using namespace std;

// a function which return sum of all integers

// find in input text file

int findSumOfIntegers()

{

ifstream f; // to open the text file in read mode

f.open("text.txt");

int sum = 0, num = 0;

// One by one read strings from file. Note that

// f >> text works same as cin >> text.

string text;

while (f >> text) {

// Move in row and find all integers

for (int i = 0; text[i] != ''; i++) {

// Find value of current integer

if (isdigit(text[i]))

num = 10 * num + (text[i] - '0');

// If other character, add it to the

// result

else {

sum += num;

num = 0; // and now replace

// previous number with 0

}

}

}

sum += num;

return sum;

}

// Driver program to test above functions

int main()

{

cout << findSumOfIntegers();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote