printing function output to a file

Hello all,

I am very new to C++ and I have a project due tomorrow in my C++ class and I'm rather stuck. I have to convert a binary value from an input file (the name of which is given by the user) and then convert the binary to decimal value and print that in an output file.

Right now it is compiling just fine with no error messages, but when I run the program, it doesn't end or print to the output file.

I know that this is last minute and late, but any ideas or help would be great and very much appreciated!!

Here is my code at the moment:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

//function prototypes
char convertBinary (char);


int main()
{

ifstream fcin;
ofstream fcout;
char inputFileName[50]; //store file name entered by user
char ch; //single character to store user input
char outputFileName[50]; //store output file name
char output;

cout <<"Enter the input file name" << endl;
cin >> inputFileName; //read in user file name
cout <<"Enter file name where output is to be stored: " << endl;
cin >> outputFileName;

fcin.open("inputFileName"); // open the input file
fcin.get(ch); //read a character ch from the input file

fcout.open("outputFileName"); //open output file

char sum = convertBinary (ch);

fcout.setf(ios::left, ios::adjustfield);
fcout << setw (20) <<"Binary" << setw(20) << "Decimal"
<< setw (20) << ch << setw (20) << sum << endl;

fcin.close();
fcout.close();

return 0;
}

//functions

char convertBinary (char ch){

char bin1;
char bin2;
int sum= 0;

while(ch)
{
cin.ignore(10, ' ');
cin.get(bin1);
cin.get(bin2);
while(bin2 != '\n'|| ' ')
{
if (bin1 == '0')
sum = sum + 0;
else if (bin1 == '1')
sum = sum + 1;
else cout << "Bad digit on input.";
sum = sum * 2;
cout << bin1;
bin1 = bin2;
cin.get(bin2);
}
if (bin1 == '0'){
sum = sum + 0;
}
else if (bin1 == '1')
sum = sum + 1;

else cout << "Bad digit on input.";
cout << bin1 << " " << sum << endl;
return sum;
}
}

but any ideas

Simply add things like

cout << "Test point 1" << endl;

to as much places as you like. You will see that some of your "test points" are printed and some are not. They you will see where you stuck.
fcin.open("inputFileName");It does not open the file specified by user, but instead file with name "inputFileName"
Get rid of quotes here

Also you should check if file is open with fcin.is_open()

Same problems with output
Topic archived. No new replies allowed.