output non ascii

Hi I am writing a program that the inputs characters from another file and is supposed to output them to the screen. I am trying to make it so that the output of the first 4 numbers are the actual characters and not the ASCII code for those characters. Please help.

The file i am inputting from is a text file that contains:

1234

The program that is reading this text file is the following:

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
string file;
int SSN[4];
string firstname;
string lastname;
string major;
char charachter;

cout<<"type the name of the file containing your students records:"<<endl;
cin>>file;

ifstream fin;
fin.open(file.c_str());

if(fin.fail())
{
cout<<"There was an error trying to read the file you requested!"<<endl;
exit(1);
}
else

for(int i=0;i<4;i++)
{
fin>>charachter;
SSN[i]=charachter;
}
cout<<SSN[0]<<SSN[1]<<SSN[2]<<SSN[3];

return 0;
}


your help is greatly appreciated.

Why don't you just read the file into SSN from the beginning? If you want to make it an int, do so. I don't see the need for char character or a type conversion.
Last edited on
Alright so i changed my program to the code below but I am still getting ASCII code outputs for the numbers in the file. Any suggestions?

#include<iostream>
#include<fstream>
#include<cctype>

using namespace std;

int main()
{
string file;
int SSN[4];
string firstname;
string lastname;
string major;


cout<<"type the name of the file containing your students records:"<<endl;
cin>>file;

ifstream fin;
fin.open(file.c_str());

if(fin.fail())
{
cout<<"There was an error trying to read the file you requested!"<<endl;
exit(1);
}
else


for(int i=0;i<4;i++)
{
SSN[i]=fin.get();
}

fin.close();
cout<<SSN[0]<<SSN[1]<<SSN[2]<<SSN[3]<<endl;

return 0;

}
1
2
3
4
for(int i=0;i<4;i++)
{ 
SSN[i]=fin.get();
}


get() Extracts a character from the stream and returns its value (casted to an integer).

Try this:
1
2
3
4
for(int i=0;i<4;i++)
{ 
	fin >> SSN[i];
}


Perfect. Thank you for helping me figure this out.
Topic archived. No new replies allowed.