Hi, I have the following problem:
Text file "strlines.dat" contains series of the following information of straight lines:
1) coordinate of one line's point
2) angle slope
It looks like (x,y) 45 deg:

2,6 60
4,7 45
2,1 30

After adding this information to associative container verify it and find lines
1) with same coordinates
2) with same angles
Showing results separately for each 1) and 2).
How to add info from file to associative container and find it? It should be like this?:

#include <string.h>
#include <iostream>
#include <map>
#include <utility>

typedef std::pair<std::(int, int), std::int>  strlines;

int main(){
   // straight line is defined by: 
   //   1) coordinate of one line's point
   //   2) angle slope

   map<int, string>  Line; // associative array
   map<int,string>::iterator it; // iterator for access to Lines

  Line.insert(strlines.dat);

 for(it= Line.begin(); it!= Line.end(); ++it){
           cout <<"Point's coordinate: : "<< (*it).first << " Angle slope of line: " << (*it).second << endl;

// Verify if coordinates of some points are identical
  if((it=Line.find()) == Line.end()){
    std::cout<<"Point's coordinate: "<< (*it).first<<" Angle slope of line: " << (*it).second << endl;
 } else{

    cout << "No lines with similar coordinates"; 
 }

return 0;
}
Member Avatar for MonsieurPointer

You are close, but you are performing a string comparsion. Also, I don't think that this line

if((it=Line.find()) == Line.end()){

will compile.

If you want to have two checks (one for the coordinates, one for the slopes), then it would be easier if you create a class for the data, then write a method which will determine if the coordinates are the same, and another one which checks if the slopes are the same.

I should perfom this problem using methods above. I've compiled this starting-up code:

            #include <string.h>
            #include <iostream>
            #include <map>
            #include <utility>

            typedef std::pair<std::int, std::int>  strlines;

            int main(){
               // straight line is defined by: 
               //   1) coordinate of one line's point
               //   2) angle slope

               map<int, int>  Line; // associative array
               map<int, int>::iterator it; // iterator for access to Lines

              Line.insert(strlines.dat);

             for(it= Line.begin(); it!= Line.end(); ++it){
                       cout <<"Point's coordinate: : "<< (*it).first << " Angle slope of line: " << (*it).second << endl;

            // Verify if coordinates of some points are same
              if((it=Line.find()) == Line.end()){
                std::cout<<"Point's coordinate is not in the map"<< endl;
             } else{

                cout << "Lines with identical coordinates are exist"; 
             }
             return 0;
            }

And Builder show the following errors:

Build
[C++ Error] 02.CPP(6): E2272 Identifier expected
[C++ Error] 02.CPP(6): E2272 Identifier expected
[C++ Error] 02.CPP(13): E2451 Undefined symbol 'map'
[C++ Error] 02.CPP(13): E2188 Expression syntax
[C++ Error] 02.CPP(14): E2188 Expression syntax
[C++ Error] 02.CPP(16): E2451 Undefined symbol 'Line'
[C++ Error] 02.CPP(16): E2108 Improper use of typedef 'strlines'
[C++ Error] 02.CPP(18): E2451 Undefined symbol 'it'
[C++ Error] 02.CPP(19): E2451 Undefined symbol 'cout'
[C++ Error] 02.CPP(19): E2451 Undefined symbol 'endl'
[C++ Error] 02.CPP(29): E2134 Compound statement missing }``

This is edited code but it don't add info to the text file:

#include <string.h>
#include <iostream>
#include <map>
#include <utility>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>

void outputLine (int, int, int)

typedef std::pair<std::int, std::int>;

int main(){
   // straight line is defined by:
   //   1) coordinate of one line's point
   //   2) angle slope

   ifstream inDataFile("strlines.dat", ios::in);
     if (!inDataFile)
       {
         cout << "File cannot be opened\n";
         exit(1);
         }

   int x;
   int y;
   int angle;

   while (inDataFile >> x >> y >> angle)
       outputLine (x, y, angle);

   map<int, int, int>  Line; // associative array
   map<int, int, int>::iterator it; // iterator for access to Lines


  // Line.insert(strlines.dat); How to add info to associative container.

 for (it= Line.begin(); it != Line.end(); ++it){
           cout <<"Point's coordinate: : "<< (*it).first << " Angle slope of line: " << (*it).second << endl;
               }

// Verify if coordinates of some points are same
  if((it=Line.find()) == Line.end()){
    std::cout<<"Point's coordinate is not in the map"<< endl;
 } else{

    cout << "Lines with similar coordinates are exist";
 }
 return 0;
}

void outputLine (int x1, int y1, int angle1)
{
  cout << setiosflags(ios::left) << setw(10) << x1 << y1 << angle1 << endl;
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.