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<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)        cout << x << " ";    cout << endl; } void println(const vector<string>& v) {    for (const string& x : v)        cout << " "<< x << " ";    cout << endl; } int main() {    vector<int> v0;    cout << "An empty vector of integers: ";    println(v0);    vector<int> v1(3);    cout << "A...
#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> #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 <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
9. #include <fstream> #include <iostream> using namespace std; int main() {     float bmi;     ifstream...
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 &&...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT