Question

In: Computer Science

#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct Point{
int x, y;
bool operator==(const Point& p2) {
return this->x == p2.x and this->y == p2.y;
}
bool operator!=(const Point& p2) {
return this->x != p2.x or this->y != p2.y;
}
friend ostream &operator<<( ostream &out, const Point &P ) {
out << "(" << P.x << ", " << P.y << ")";
return out;
}

friend istream &operator>>( istream &in, Point &P ) {
char d1, d2, d3;
// input format: (1, 2)
in >> d1 >> P.x >> d2 >> P.y >> d3;
return in;
}
};

int main()
{
vector<Point> original_points;
cout << "Enter points :\n";
Point P;
// loop continues until any error occurs
while(cin>>P)
{
// if any input error occurs, exit the loop keeping the valid points in vector
if(cin.fail()){
cin.clear(); // back in 'normal' operation mode
cin.ignore(100,'\n'); // and remove the bad input

}
original_points.push_back(P);
}

ofstream out("mydata.txt");
cout << "You entered the points:\n";
for(Point p: original_points)
{
cout << p << '\n';
out << p << '\n';
}
out.close();


ifstream in("mydata.txt");
vector<Point> processed_points;

// loop to read the data from input file
while( in >> P )
{
// if input file read error occurs, exit the loop
if(in.fail())
{
in.clear(); // back in 'normal' operation mode
in.ignore(100,'\n'); // and remove the bad input
}
processed_points.push_back(P);
}

int n = original_points.size();
for(int i=0; i<n; i++)
{
if(original_points[i] == processed_points[i])
{
cout << "Points at index " << i << " are same\n"
<< original_points[i] << " "
<< processed_points[i] << '\n';
}
if(original_points[i] != processed_points[i])
{
cout << "Points at index " << i << " are not same\n"
<< original_points[i] << " "
<< processed_points[i] << '\n';
}
}

return 0;
}
Depend on this code, if the user enters the wrong input like (8.5,8.7) change in to (8,8) then let the user enter another input again the user enter like (er,yu) ignore this input and let the user enter another input the user wants to terminate use file termination. is there anybody can help

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

struct Point{
   int x, y;
   bool operator==(const Point& p2) {
       return this->x == p2.x and this->y == p2.y;
   }
   bool operator!=(const Point& p2) {
       return this->x != p2.x or this->y != p2.y;
   }
   friend ostream &operator<<( ostream &out, const Point &P ) {
       out << "(" << P.x << ", " << P.y << ")";
       return out;
   }

   friend istream &operator>>( istream &in, Point &P ) {
       char d1, d2, d3;
       // input format: (1, 2)
       float xf,yf;
       in >> d1 >> xf >> d2 >> yf >> d3;
       P.x = floor(xf);
       P.y = floor(yf);
       return in;
   }
};

int main()
{
   vector<Point> original_points;
   cout << "Enter points :\n";
   Point P;
   // loop continues until any error occurs
   while(!cin.eof())
   {
       cin>>P;
       // if any input error occurs, exit the loop keeping the valid points in vector
       if(cin.fail()){
           cin.clear(); // back in 'normal' operation mode
           cin.ignore(100,'\n'); // and remove the bad input
       }else{
           original_points.push_back(P);
       }
   }

   ofstream out("mydata.txt");
   cout << "You entered the points:\n";
   for(Point p: original_points)
   {
       cout << p << '\n';
       out << p << '\n';
   }
   out.close();


   ifstream in("mydata.txt");
   vector<Point> processed_points;

   // loop to read the data from input file
   while( in >> P )
   {
       // if input file read error occurs, exit the loop
       if(in.fail())
       {
           in.clear(); // back in 'normal' operation mode
           in.ignore(100,'\n'); // and remove the bad input
       }
       processed_points.push_back(P);
   }

   int n = original_points.size();
   for(int i=0; i<n; i++)
   {
       if(original_points[i] == processed_points[i])
       {
           cout << "Points at index " << i << " are same\n"
                   << original_points[i] << " "
                   << processed_points[i] << '\n';
       }
       if(original_points[i] != processed_points[i])
       {
           cout << "Points at index " << i << " are not same\n"
                   << original_points[i] << " "
                   << processed_points[i] << '\n';
       }
   }

   return 0;
}


Related Solutions

#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
9. #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt");...
9. #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt"); while (!inFile.eof()) { inFile >> bmi; if( bmi < 18.5) { cout << bmi << " is underweight " ; } else if( bmi >= 18.5 && bmi <= 24.9) { cout << bmi << " is in normal range " ; } else if( bmi >= 25.0 && bmi <= 29.9) { cout << bmi << " is overweight " ; } else...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int stateLength = 6; const int cityLength = 20; const int streetLength = 30; int custNomber = -1; // Structure struct Customers { long customerNumber; char name[nameLength]; char state[stateLength]; char city[cityLength]; char streetAddress1[streetLength]; char streetAddress2[streetLength]; char isDeleted = 'N'; char newLine = '\n'; int zipCode; }; int main() { ofstream file; file.open("Customers.dat", fstream::binary | fstream::out); char go; long entries = 0; struct Customers data; do...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT