Question

Create a program named Lab14B. You will create a nested loop in this program by putting...

  1. Create a program named Lab14B. You will create a nested loop in this program by putting Lab14A’s for loop inside a while loop. In the new while loop, you will read integers from the file, Lab14B.txt, and determine if the number is prime (using the copied loop from Lab14A). Stop the while loop when you reach the end of file.
  • Copy your code from Lab14A into your new program
  • Add a while not eof loop around that code, and also change the code to read the input number from a text file instead of from the user. (Lab 12 has an eof controlled while loop.)
  • Don’t forget to reset your count variable to 0 every time you read a new number.

For each number, print both the number itself and a statement saying if it is prime or not.

My code is below. I'm mainly confused with what to leave from my original code and what to take out for the eof while loop

int main()
{
    int num, i, count = 0;
    ifstream infile;
    infile.open("Lab14B.txt");
    while (!infile.eof())
    cout << "Please enter a number : ";
    cin >> num;
    if (num == 0)
    {
        cout << " is not prime ";
    }
    else
    {
        for (i = 2; i < num; i++)
            if (num % i == 0)
                count++;
    }
    if (count > 1)
        cout << " Number is not prime ";
    else
        cout << " Number is prime ";
    return 0;
}

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

Lab14B.cpp:

#include <fstream>
#include<iostream>
using namespace std;
int main()
{
int num, i, count = 0;
ifstream infile;
infile.open("Lab14B.txt"); //opening given text file
   //here we wont use !infile.eof() condition in while loop
   // because it doesnt read text file properly .If we use that condition then last line will be read twice
while (infile >> num) //reading until lines exist
   {
       count=0;
       if (num == 0 || num == 1) //if number is 0 or 1
       {
       cout <<num<< " is not prime\n"; //they are not prime
           continue; //skipping the next statments of loop
       }
       else
       {
       for (i = 2; i < num; i++) //iterating upto that number
       if (num % i == 0){ //if it is divisible by any number
       count++; //increment count variable
                   break; //coming out of the for loop
               }
       }
       if (count == 1) //if count is 1 then it is not prime
       cout <<num<< " Number is not prime\n";
       else
       cout <<num<< " Number is prime\n";
   }
return 0;
}

OUTPUT:
my file Lab14B.txt contents:

1
2
3
4
5
6
7
8
9
10

Please rate my answer if you like it.

Add a comment
Know the answer?
Add Answer to:
Create a program named Lab14B. You will create a nested loop in this program by putting...
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
  • The following program is in c++ ; I am trying to read in games from a...

    The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...

  • //This program is your final practice exam. //Please fill in the functions at the bottom of...

    //This program is your final practice exam. //Please fill in the functions at the bottom of the file. (sum and removeItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream>...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • If necessary, create a new project named Intermediate24 Project and save it in the Cpp8\Chap14 folder....

    If necessary, create a new project named Intermediate24 Project and save it in the Cpp8\Chap14 folder. Also create a new source file named Intermediate24.cpp. If you are using Microsoft Visual C++, copy the Intermediate24.txt file from the Cpp8\Chap14 folder to the Cpp8\Chap14\Intermediate24 Project folder. Use a text editor to open the Intermediate24.txt file, which contains payroll codes and salaries. Close the Intermediate24.txt file. Create a program that allows the user to enter a payroll code. The program should search for...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • How do I complete my code using an external file? External file: data.txt //the number 6...

    How do I complete my code using an external file? External file: data.txt //the number 6 is in the external file Incomplete code: #include <iostream> #include <fstream> using namespace std; class example {    int data[1]; public:    example();    void read(ifstream &); }; example::example() {    ifstream infile;    infile.open("data.txt");    } void example::read(ifstream &infile) {    infile >> data[1];    cout << data[1] << endl;    } int main() {    example example, read; //system("pause");    return 0;...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • I am getting an error that the variable num was not declared in the scope on...

    I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5). Do you know how to fixe this error? #include <iostream> #include <iomanip> #include <string> #include<fstream> #include <cstring> using namespace std; //Declare the structure struct Games { string visit_team; int home_score; int visit_score; }; // Function prototypes int readData(Games *&stats); int menu(); void pickGame(Games *&stats, int size); void inputValid (Games *&stats, int size); void homeTotal(Games *&stats,...

  • c++ program that prints joke and its punchline

    nothing happens when I replace the skeleton code. the program runs but it does not give me the desired results. All the question marks must be removed and filled in with the appropriate code.// Function prototypesvoid displayAllLines(ifstream &infile);void displayLastLine(ifstream &infile);int main(){// Arrays for file nameschar fileName1[SIZE];char fileName2[SIZE];// File stream objectsifstream jokeFile;ifstream punchlineFile;// Explain the program to the user.cout << "This program will print a joke "<< "and its punch line.\n\n";// Get the joke file name.cout << "Enter the name of...

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