show me my mistake

Nov 21, 2013 at 6:37am
hi,i wanna give a file from notepad that i dont know the number of row and column like
15.0 12.2 13.8
12 54 7 6 4
12 8 74 90 76
i write this program,plz show me my mistake
#include <iostream>
#include <string>
#include<fstream>
using namespace std;

int main()
{
string s,ss;
double x,y,z;
int * f;
int a,b,c,i,j,row,col;
ifstream A;
ofstream B;
ifstream C;
A.open("C:/Users/milad/Desktop/miladam.txt");

i=0;

while (!A.eof())
{
getline(A,s);
A>>s;
i++;
}
row=i;
A.close();

f=new int [row];
A.open("C:/Users/milad/Desktop/miladam.txt");
for(j=0;j<row;j++)
{
getline(A,ss);
A>>ss;
B.open("C:/Users/milad/Desktop/miladam10000.txt");
B<<ss;
B.close();
C.open("C:/Users/milad/Desktop/miladam10000.txt");
col=0;
while(!C.eof())
{
C>>z;
col++;
}
C.close();
cout<<col;
}
cin>>x;
return 0;
}

its output is 114
Nov 21, 2013 at 7:05am
Your code is not readable. You need to edit your post and put your code in code brackets to make it readable.

Your code should look like this:

 
int main() { }


Not this:

int main() { }
Nov 21, 2013 at 7:28am
[#include <iostream>
#include <string>
#include<fstream>
using namespace std;

int main()
{
string s,ss;
double x,y,z;
int * f;
int a,b,c,i,j,row,col;
ifstream A;
ofstream B;
ifstream C;
A.open("C:/Users/milad/Desktop/miladam.txt");

i=0;

while (!A.eof())
{
getline(A,s);
A>>s;
i++;
}
row=i;
A.close();

f=new int [row];
A.open("C:/Users/milad/Desktop/miladam.txt");
for(j=0;j<row;j++)
{
getline(A,ss);
A>>ss;
B.open("C:/Users/milad/Desktop/miladam10000.txt");
B<<ss;
B.close();
C.open("C:/Users/milad/Desktop/miladam10000.txt");
col=0;
while(!C.eof())
{
C>>z;
col++;
}
C.close();
cout<<col;
}
cin>>x;
return 0;
}

][/code]
Nov 21, 2013 at 7:29am
[#include <iostream>
#include <string>
#include<fstream>
using namespace std;

int main()
{
string s,ss;
double x,y,z;
int * f;
int a,b,c,i,j,row,col;
ifstream A;
ofstream B;
ifstream C;
A.open("C:/Users/milad/Desktop/miladam.txt");

i=0;

while (!A.eof())
{
getline(A,s);
A>>s;
i++;
}
row=i;
A.close();

f=new int [row];
A.open("C:/Users/milad/Desktop/miladam.txt");
for(j=0;j<row;j++)
{
getline(A,ss);
A>>ss;
B.open("C:/Users/milad/Desktop/miladam10000.txt");
B<<ss;
B.close();
C.open("C:/Users/milad/Desktop/miladam10000.txt");
col=0;
while(!C.eof())
{
C>>z;
col++;
}
C.close();
cout<<col;
}
cin>>x;
return 0;
}

]
Nov 21, 2013 at 7:30am
ok?
u can copy it
Nov 21, 2013 at 12:59pm
This is his 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include<fstream>
using namespace std;

int main()
{
    string s, ss;
    double x, y, z;
    int *f;
    int a, b, c, i, j, row, col;
    ifstream A;
    ofstream B;
    ifstream C;
    A.open("C:/Users/milad/Desktop/miladam.txt");

    i = 0;

    while (!A.eof()) {
	getline(A, s);
	A >> s;
	i++;
    }
    row = i;
    A.close();

    f = new int[row];
    A.open("C:/Users/milad/Desktop/miladam.txt");
    for (j = 0; j < row; j++) {
	getline(A, ss);
	A >> ss;
	B.open("C:/Users/milad/Desktop/miladam10000.txt");
	B << ss;
	B.close();
	C.open("C:/Users/milad/Desktop/miladam10000.txt");
	col = 0;
	while (!C.eof()) {
	    C >> z;
	    col++;
	}
	C.close();
	cout << col;
    }
    cin >> x;
    return 0;
}
Nov 21, 2013 at 2:16pm
@ miladam: use std::stringstream after you used std::getline().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string s;
    std::ifstream input_file("input.txt");

    while (std::getline(input_file, s))
    {
        std::stringstream ss(s);
        float f;

        std::cout << "Read a new line:";

        while (ss >> f)
            std::cout << ' ' << f;

        std::cout << '\n';
    }
}
Read a new line: 15 12.2 13.8
Read a new line: 12 54 7 6 4
Read a new line: 12 8 74 90 76
Topic archived. No new replies allowed.