Reading integers from a file

Sorry, I'm trying to read in from a file
which will look something like this

3 4 5
5 -3 222
2 1 45
42 -5 2345

etc...

I have three integers that I want to store the numbers into -->

int x, y, c.

(btw this is the simple diophantine linear equation)

But I'm running into problems such as "there are two int mains", "main must return an integer (which i did..?)"

can someone guide me into what might be the right steps into storing integers? or possibly what am I doing wrong?

my plan is to go one line at a time, simply store each line into the proper variables, and check if the d.l.e. is valid... then traverse the next line-> repeat process.

Please do not tell me the answer. Just guide me. I want to learn lol.

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
32
33
34
35
36
37
38
39
 #include <iostream>
#include <vector>
#include <fstream>

using namespace std;

struct _vector
{
    int x, y, c;
}

int main()
{
    string fileName; 
    vector<int>grid; 
    cout << "What is the name of your input file?" << endl;
    cin >> fileName; 
    
    ifstream file; 
    file.open(fileName.c_str());
    
    _vector data; 
    if (file.is_open())
    {
        while (!fileName.eof())
        {
            fileName >> data.x >> data.y >> data.c; 
            
            grid.push_back(data);
        }
    }
    
    for(unsigned int i = 0; i < 4; ++i)
    {
        cout << grid.at(i);
    }
    
    return 0;
}
What are the errors you are getting?
input.cpp:12: error: new types may not be defined in a return type
input.cpp:12: note: (perhaps a semicolon is missing after the definition of â)
input.cpp:12: error: two or more data types in declaration of â
input.cpp:12: error: â must return â
input.cpp: In function â:
input.cpp:25: error: â has no member named â
input.cpp:27: error: no match for â in â
input.cpp:29: error: no matching function for call to â
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]
You are missing a semicolon at the end of your struct on line 10.
Oh right. Thanks. But now I am getting these errors:


input.cpp: In function â:
input.cpp:26: error: â has no member named â
input.cpp:28: error: no match for â in â
input.cpp:30: error: no matching function for call to â
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]
line 15 declares a vector of type int, but you want the type struct _vector (perhaps a bad choice of variable name here) - hence the last error message. So you should have a typedef to make the typing easier - otherwise line 22 should have struct at the start.
Topic archived. No new replies allowed.