Oh, so there's more than 1 binary in the file, and the file is written like that?
You will definitely need a while-loop. You open the file correctly, but you only read the first line. And cin.get() does not read from the file. It just waits for the user to input something. In general, anything to do with cin means it's between the program and the user.
and make sure that the user inputs the .txt along with the file name. Like "filename.txt".
Try something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
ifstream in;
ifstream out;
string binary;//Holds the binary input from file.
in.open(inputname);
//Everytime the while loop runs, it reads one line.
//So in your response above, this while loop will run 3 times.
while(!in.eof()){
in >> binary;//So this takes the binary on the first line and stores it
//into the variable "binary". Conveniently, if you put my code in a function and
// change it a bit, and pass it this "binary" variable, it will convert it to decimal.
....//do some other stuff
//and then you can output it to a file with something like this:
out << binary;
}
| |
I THINK you can't have a ifstream and an ofstrem open at the same time. Not sure about that. If not, you can just store the binaries into an array of strings, and your answer into an array of decimals. And then output them after you calculate everything.
And to store the input into an array you could try something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
//declare an array of string:
string binaries[10];// 10 is just random number.
//The amount of binaries goes there.
//If there is 15 binaries in the file, it would be string binaries[15];
int count = 0;//This is the position of the array we will be writing to.
//.... and then right after the part where you write:
in >> binary;
//write this:
binaries[count] = binary;
count++;//to move to the next spot of the array.
| |
It would be much easier using vectors instead of arrays, but arrays works.