how do i get int in fstream?

i am in need to change my char to int.

i am not allowed to use anything above and including fuctions.

this includes sstream.

is there any way for the program to read "3" in the program
as an interger?

8 Years
Discussion Span
Last Post by pecet

i am in need to change my char to int.

You are in luck.. char to int conversions are wicked easy..

(and so is any conversion among c++ standard primitive data types)

Most people like to use the old c-style cast because it's quick and easy although technically it's not c++:

char a = 'A';
int b;

// b will contain the numerical ascii value of capital letter 'A'
b = (int)a;

Now here is the technically correct c++ method:

char a = 'A';
int b;

b = static_cast<int>(a);

Edited by Clinton Portis: n/a

aCC ola5.cc
ola5.cc: In function 'int main()':
ola5.cc:29: error: expected `<' before '(' token
ola5.cc:29: error: expected type-specifier before '(' token
ola5.cc:29: error: expected `>' before '(' token

i get an error message like this.
this is what i did.

cout << "Please enter the file name that contains the student records: ";

           char a;
cin >> fileName;

cout << "there are " << a << " scores per student\n";
cout << endl;
cout << "Student name ";

int b;
b = static_cast(a);
count = 1;
while (count <= a)
    cout << "test" << count << "  ";

what is wrong?

Edited by mike_2000_17: Fixed formatting

b = static_cast(a);

should be:

b = static_cast<int>(a);

even though i changed it to int, i get 51 when i need 3.

my char is 3, and i want to make it into int 3.

am i too confusing?

sorry for bothering you, but i NEED to get this done.


51 is the ascii value of the character '3'....

Knowing this, we can now we can put some of that advanced college education to work:

ascii value - x = int

51 - x = 3

x = 48

After you make your char-to-int cast, just subtract 48. (This offset will apply to all ascii numeric characters)

Edited by Clinton Portis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.