How Do i cast from a char to int?

Jan 25, 2009 at 11:18pm
Hi im really new to c++ and im taking a college course on c++ and my assignment requires i cast from a char to int how do i do that my current code is this its very incomplete the input text file contains the letter a on the first line and two double digit numbers on the second 10 and 20. assignment is to have this write an output file.


#include <iostream>

#include <fstream>

using namespace std;

int main()
{

ifstream fileIn;
ofstream fileOut;

fileIn.open("Input Text.txt");
fileOut.open("Output Text.txt");

fileOut << "The ASCII value of your character: " __ " is " ____"
//you will need to cast from a char to an int

// Close the files
fileIn.close();
fileOut.close();

return 0;
}
Jan 25, 2009 at 11:37pm
1
2
char var='A';
std::cout <<(unsigned)var<<std::endl;
Jan 26, 2009 at 12:10am
http://www.cplusplus.com/forum/articles/6046/
That link contains a better approach to casting from a string to an int.
Jan 26, 2009 at 8:44pm
Please use proper puncuation. It is very difficult to sort through a paragraph without any punctuation.

I'd recommend static_cast over the c style cast if you must perform a cast at all.

1
2
int iValue;
iValue = static_cast<int>(cValue);  


I'm not sure how that helps with the assignment but there you go.
Last edited on Jan 26, 2009 at 8:44pm
Topic archived. No new replies allowed.