Why canot this code read and write txt file?

Oct 12, 2014 at 3:02pm
Dear Friends,
I want this code get "t" 9 times from input.txt at desktop and save the acc(t) results in array [3][3] forms in output.text file in desktop. But while running it shows different than I expect. I made a text file in desktop and wrote:
2
3
4
5
6
7
8
9
10

But this code output is: 10 20 30 40 50 60 70 80 90 100
I want to see the results in
20 30 40
50 60 70
80 90 100
form.


what should I do?


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
    #include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int acc(int x)
{
    return (10*x);
}

int main()
{
    const int a = 9;  //reading 9 items from file, assume the file has upto 9 items
    int array[3][3];
    
    ifstream inputFile("C:/users/spring/Desktop/input.txt");  //ive not checked if the file exists
    ofstream outputFile("C:/users/spring/Desktop/output.txt");
    
    //read from file
    int a;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            inputFile>>a;
            array[i][j] = acc(a);
        }
    }
    
    //print to file
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if(i%3 == 0)
                cout<<endl;
            outputFile<<array[i][j]<<setw(4);
        }
    }
    return 0;
}
Last edited on Oct 12, 2014 at 3:37pm
Oct 12, 2014 at 3:36pm
I've corrected your code. It seems that you were over-complicating things a little bit; you didn't really need a 2-dimensional array so I changed it into a single dimensional one.

I tried to keep the code as simple as possible, but if there is something you wish me to explain such as what something does or why I did it then I will be happy to.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int acc(string x)
{
	return (10*stoi(x)); // "stoi" converts a string into an integer
}

int main()
{
	const int a = 9;  //reading 9 items from file, assume the file has upto 9 items
	string array[a];

	ifstream inputFile("C:/users/spring/Desktop/input.txt");  //ive not checked if the file exists
	ofstream outputFile("C:/users/spring/Desktop/output.txt");

	//read from file
	int pos = 0;
	while (!inputFile.eof() && pos <= 9) {
		getline(inputFile, array[pos]);
		pos++;
	}
	inputFile.close();

	//print to file
	for(int i = 0; i < a; i++)
	{
		outputFile << acc(array[i]) << endl;
	}
	outputFile.close();
	return 0;
}
Oct 12, 2014 at 3:45pm
Thanks a lot. But while running it gives error in lines 9 and 10.

Last edited on Oct 12, 2014 at 3:47pm
Oct 12, 2014 at 3:48pm
Did you put "#include <string>" at the top like I did? If you did then the error will be because one of the lines in the file is not an integer. Other than that, the code works flawlessly for me.
Oct 12, 2014 at 3:49pm
Yes I put.
Oct 12, 2014 at 3:50pm
Is it an error during compilation or is it while the program is actually running?
Oct 12, 2014 at 3:52pm
The error is this: it says stoi is not declared

6:44:17 **** Incremental Build of configuration Debug for project test 12 ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main12.o "..\\main12.cpp"
..\main12.cpp: In function 'int acc(std::string)':
..\main12.cpp:16:19: error: 'stoi' was not declared in this scope
return (10*stoi(x)); // "stoi" converts a string into an integer
^
..\main12.cpp:17:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Oct 12, 2014 at 3:57pm
If you are using g++ then you have to set the compiler options to support C++11 because "stoi" is a new feature. But other than that there should be no other issues. As long as you include the "string" header and use the "std" namespace as well then you should be fine.
Last edited on Oct 12, 2014 at 4:01pm
Oct 12, 2014 at 3:58pm
The stox() series of of functions required a recent C++11 compiler.
Oct 12, 2014 at 3:59pm
Thanks a lot.
Also Do you have any idea how can I change the
10 20 30 40 50 60 70 80 90 100

output to

20 30 40
50 60 70
80 90 100

in my first code?
Oct 12, 2014 at 4:04pm
In my code all you have to do is change the "int i = 0" on line 29 into "int i = 1". That would just output all of the lines except the first one.
Oct 12, 2014 at 4:10pm
The problem is that my compiler does not know stoi
:(
Oct 12, 2014 at 4:14pm
What compiler are you using? You can use an alternative compiler such as Eclipse which does support C++11 functions and is also free. If you don't want to use Eclipse then there are plenty of other C++11 compilers hanging around on the internet.
Last edited on Oct 12, 2014 at 4:14pm
Oct 12, 2014 at 4:16pm
I use eclipse
Oct 12, 2014 at 4:20pm
Have you enabled C++11 support in the project properties?

Try this link:
http://stackoverflow.com/questions/17457069/enabling-c11-in-eclipse-juno-kepler-luna-cdt
Topic archived. No new replies allowed.