making char's integers

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
char next;

ifstream in_stream;

in_stream.open("data.txt");

if(in_stream.fail())
{
cout << "file failed to open"<<endl;
exit(1);
}

in_stream.get(next);

do
{
cout << next<<endl;
in_stream.get(next);

}while(next != '\n');

int c = static_cast<int>(next);
cout << c;
in_stream.close();

return 0;
}
Last edited on
Hello taf0110.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

This is a rework of what you started with and a little adjustment and rearrangement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>

int main()
{
	char next;

	std::ifstream in_stream;

	in_stream.open("data.txt");

	if (in_stream.fail())
	{
		std::cout << "file failed to open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(5));
		exit(1);
	}

	in_stream.get(next);

	do
	{
		std::cout << next << ", ";
		std::cout << static_cast<int>(next) << std::endl;  // <--- Moves and revised.
		in_stream.get(next);

	} while (next != '\n');

	//std::cout << "\n After do " << c;  // Not needed.
	in_stream.close();

        std::this_thread::sleep_for(std::chrono::seconds(5));
}


This code std::this_thread::sleep_for(std::chrono::seconds(5)); requires the header files "chrono" and "thread".

The program works just not well the way you have it.

int c = static_cast<int>(next); This line will only print the last character read from the file which is "\n".

Hope that helps,

Andy
I appreciate it thank you so much!!
Topic archived. No new replies allowed.