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?

Recommended Answers

All 7 Replies

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);

Of course there is. Just use the stream's >> operator

int n;
ifstream in("filename.txt");
in >> n;
aCC In function 'int main()': error: expected `<' before '(' token error: expected type-specifier before '(' token 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?

b = static_cast(a);

should be:

b = static_cast<int>(a);
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)

or just use itoa/atoi functions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.