SYntax error in this file i/o c++

Hi, I need help to make this program work.
As I've converted the text in MS.Excel to text file, there are commas in the input.
The text file input:
12345,,x,x,x,x,,x,x,x,x,x,x,
35674,,x,x,
13111,,x,x,x,x,x,
53535,,x,x,x,x,,x,x,
23455,,x,x,x,

Now, for every id, I need to read the number of ID and print out id that has less than 7 x's. However there is some syntax error with this and I don't know why.

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
40
41
42
43
44
45
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{

ifstream inData;
inData.open("rubbish.txt");
string line;
string ID;
int convertToInteger;

if(inData.is_open())
{
while(!inData.eof())
{
getline(inData, line);
int counter = 0;

for(int i = 0; i < line.length(); i++)
{
if(line[i] == 'x')
{
counter++;
}
}
if(counter < 7)
{
for(int i = 0; i < line.length(); i++)
{
if(isdigit(line[i]))
ID += line[i];
}
convertToInteger =atoi(line[i]); //syntax error occurs here
}
}
}

cout << "ID: " << convertToInteger << " has less than 7 x's" << endl;

system("pause");
return 0;
}

>>>>Any1 can help me fix it? or any other way to replace atoi?

Thx
Last edited on
I fix your code as
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<fstream>
#include<string>
#include <sstream>
using namespace std;
int main()
{

    ifstream inData;
    inData.open("rubbish.txt");
    string line;
    string ID;
    int convertToInteger;
	
    if(inData.is_open())
    {
        while(!inData.eof())
        {
            getline(inData, line);
            int counter = 0;

            for(int i = 0; i < line.length(); i++)
            {
                if(line[i] == 'x')
                {
                    counter++;
                }
            }
            if(counter < 7)
            {
                for(int i = 0; i < line.length(); i++)
                {
					if(isdigit(line[i]))
                        ID += line[i];
					
                }
                
                //convertToInteger = atoi(ID.c_str()); //syntax error occurs here
     			stringstream str;
     			str << ID;
     			str >> convertToInteger;
                cout << "ID: " << convertToInteger << " has less than 7 x's" << endl;
                ID = "";
                
            }
        }
    }

    

    system("pause");
    return 0;
}


Is this you want?

or you can write the 35 line code like this:
1
2
3
convertToInteger = atoi(ID.c_str()); //fixed
                cout << "ID: " << convertToInteger << " has less than 7 x's" << endl;
                ID = "";//new add 
Last edited on
Topic archived. No new replies allowed.