Hi.. can anyone help me.. I having problems in displaying the output read from an input file. when I run the code, it only display the output of the first character in the input file. Anyone can help me figure out the problem? & I'm using borland.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>



int main ()
{
	int character;
   int count = 0;
	ifstream inputFile;
   char fileName[100];
   cout<<"Enter file name :";
   cin>>fileName;
   inputFile.open(fileName);

   if (!inputFile)
   	cout<<"Eror opening file. File name "<<fileName<<" does not exist."<<endl;
   else
   {
   	while (count <= 99)
      {
      	inputFile >> character;
         cout<<character<<endl;
         count++;
      }
   }

Edited 4 Years Ago by snowhite89: n/a

Attachments
5PN2qmWqBlQ9wQj99nsQzldVI5ZuGXbEWRK5RhRXdCdG5nG5azdNMK66MuCV6GXi5xr84P2R391UXaLHbavJvFZGfO47XWS2qVOw
5lxuBEBNue7b9PZS0hoI6pgabi9UOuSpM2Tlh01vOzwvSikHFswuzleht6xGbotuQQgmO6GkdAqwedXNRRadbLMZROaAfZDAWhxg
qRqcM1fdhRxRZYw3GFhPr6BInZITSBQOgwxhHxrhytFUloiivhOmebpblnTipx7wXZAqJZR5Y4M9k8AIyGE9CpuXkn1XvnFttynI
1MguokYDeWl5noesnB4mh19PM6Dph2Nh722AggBfUKJEdmm09Z1uFII2PNfTdMjJFB59lvpzORCPO4yy5dZ4yR2nIGRwH4cYCroO

cin is getting an error on the first character because it is not numeric digit. You declared variable character as int, so cin expects to read numeric digits. Change the data type of character to char and it should work as expected.

Suggestion: Change the while loop to read all the characters in the file

while( cin >> character )
{
   cout << character << endl;
   ++count;
}

Edited 4 Years Ago by Ancient Dragon: n/a

cin is getting an error on the first character because it is not numeric digit. You declared variable character as int, so cin expects to read numeric digits. Change the data type of character to char and it should work as expected.

Suggestion: Change the while loop to read all the characters in the file

while( cin >> character )
{
   cout << character << endl;
   ++count;
}

Thanks for your help~ I appreciate it!!~ ^_^

read from input file using C

Did you mean C, or do you mean C++? You're using C++ (a very old C++ that will not work on modern compilers; looks like pre-standard C++ to me, as if you were using a decade old compiler... using Dev-C++, by any chance?)

read from input file using C

Did you mean C, or do you mean C++? You're using C++ (a very old C++ that will not work on modern compilers; looks like pre-standard C++ to me, as if you were using a decade old compiler... using Dev-C++, by any chance?)

I'm using borland c++ not dev-c++ .. I'm new to this language.. so maybe because I refer to old books.. That's why it looks like some very old c++.. hope to learn more from u guys.. :)

If you're just learning now, I think you might do better to learn modern standard C++ rather than old C++; if you try to use old, pre-standard C++ with a modern compiler that adheres to the C++ standard, you'll have trouble.

There are many free modern IDEs and compilers available to you.

This question has already been answered. Start a new discussion instead.