problem with vectors

Pages: 12
Oh yes.Finally I got it.Thank you a lottt Moschops.The worst thing is that my code wont be reusable, when I define the points.How can I make it to read from file instead?
Are you asking how to do file I/O?
Read the tutorial here: http://cplusplus.com/doc/tutorial/files/

Or did you want to know more specifically how to do it in your example?
I know about the streams.I dont know how exactly to implement it.
EDIT [don't know why I wrote about output streams...] Well each of your vertices is a pair of doubles, so you must read two doubles to the file stream. If you want, you could put the number of vertices at the top of the file, but with a vector this shouldn't be necessary. Just keep reading pairs and checking for EOF.
Last edited on
By making an ifstream (input filestream) object.

Here's some example code; it opens the file someTextFile.txt and reads the values from it until there are none left to read. This should be enough to demonstrate the mechanics of reading from a file; it's up to you to work out how best to store the numbers and make sure that you deal with them appropriately. There are other ways to read from a file; this should get you started.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>

int main()
{
  std::ifstream input("someTextFile.txt");

  int a = 0;

  while (input >> a)  
  {
    if( input.eof() ) break;
    std::cout << a << std::endl;
  }

  return 0;
}


Last edited on
I try it since yesterday, but It didnt work.So today I decided to make it with defined points.I Made it to read from the file, but I couldnt make a vector with the pairs..So if you can give me more specific advise on how to do it I will be very thankful.If you dont have time for me, Thank you for the help so far :)
Declare your vector as before: vector<pair<double, double> >.

The vector has a push_back method, which will accept a value of type pair<double, double>.

So just read in two doubles, let's say d1, d2 and then add them as to the vector like so:
vector_name_here.push_back(pair(d1, d2));

It uses the constructor of pair<double, double> to construct a pair of doubles and pushes that onto the vector.

This shows reading in values and putting them into elements of two vectors. The values are simply in the text file with a gap between each value. Nothing else.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <fstream>
#include <iostream>
#include <vector>


int main()
{
  std::ifstream input("someTextFile.txt");

  std::vector <double> a(5);
  std::vector <double> b(5);
  int i = 0;

  while (true)  
  {
    if( input.eof() ) break;
    if( i == 5) break;

    input >> a[i];
    input >> b[i];
    

   std::cout << "a[" <<i << "] contains value " << a[i] << std::endl;
   std::cout << "b[" <<i << "] contains value " << b[i] << std::endl;

   i++;
  }

  return 0;
}


Ok thanks for the advices, now I will try it and will post the result :)
Well, I made it, but it takes only two values.
The code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    vector< pair<double, double> > v;

ifstream infile; 
        cout << "Enter the name of the input file, please: ";
        char filename[1024];
          cin.getline(filename , 1024, '\n');
        
  vector <double> a(50);
  vector <double> b(50);
  int i = 0;

  while (true)  
  {
    if( infile.eof() ) break;
    if( i == 50) break;

    infile >> a[i];
    infile >> b[i];
    
v.push_back(make_pair(a[i], b[i]));
   std::cout << "a[" <<i << "] contains value " << a[i] << std::endl;
   std::cout << "b[" <<i << "] contains value " << b[i] << std::endl;

   i++;
  }

The output is :
a[0] contains value 0
b[0] contains value 0
The area is: -0
What I did wrong?
What did you put in the file?
it looks like this:
32 49
8 65
63 61
93 79
48 93
8 45
59 54
92 94
47 0
40 65
...
(50 points)
and its a .txt file
Last edited on
Actually, you don't appear to have opened the file in your code ;)
I dont open it??So how should I open it?
Well you can pass the filename as an argument to the constructor of the std::ifstream.

Or you could call std::ifstream::open and pass the filename to that.

Remember to check whether the file opened successfully, using std::ifstream::is_open or std::ifstream::fail.

It's all here: http://www.cplusplus.com/doc/tutorial/files/ :)
Ok thanks, tonight I will try to compile it, if I dont succeed, I will ask again tomorrow.Thanks guys :)
No problem.
I saw my mistake, I should have been very tired to miss "infile.open"...I have also some mistake in the calculations, but I am working at it now, and I think I will fix it.If I make it I will post the result here and will mark it as solved, for those after me.Thank you a lot for the help Xander and Moschops :)
Last edited on
Topic archived. No new replies allowed.
Pages: 12