0

I have a list of numbers in a text file and I am trying to make a console application in C++ which converts those ASCII decimals into their symbol and then outputs them into a new file. For example, the ASCII decimal 88 into 'X'.

The text file looks as follows:

84
104
101
32
99
111
100
101
32
105
115
58
32
53
52
57
57
52
53

So far this is what I've got:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()

{
    ifstream infile;
    infile.open("Output.txt");

    if (infile.fail()){
        cerr << "Error Opening File" << endl;
        //Terminate
    }


    int number;
    int count = 0;


    while(!infile.eof()){
        infile >> number;
        count++;

    }

    cout << count << " Number(s) Found!" << endl;


    cout << "\nConverting To Ascii!" << endl;

    //Convert To ASCII and Output to new file   



    return 0;
}
roadrunner66
  • 6,808
  • 3
  • 27
  • 37
  • What have you attempted? – sebenalern May 28 '16 at 02:48
  • I've looked online about itoa and sprintf but I don't understand the functions well enough to figure out how to implement it in this way. – SystemX32 May 28 '16 at 02:51
  • 2
    Unrelated: Stop doing this: `while(!infile.eof())`. [Read this to find out why](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – WhozCraig May 28 '16 at 02:52
  • Did you just try `std::cout << static_cast(number) << '\n';` ? I.e. stop looking for a needle when all you need is the haystack. – WhozCraig May 28 '16 at 02:53
  • Do you know enough C++ to be able to figure out what "char c=88; std::cout < – Sam Varshavchik May 28 '16 at 02:53
  • I have not tried this yet. I'm going to read up on this and see if I can get it to work. – SystemX32 May 28 '16 at 02:54
  • "Did you just try std::cout << static_cast(number) << '\n'; ? I.e. stop looking for a needle when all you need is the haystack." Thanks! This works. I'm going to make sure I understand it well. – SystemX32 May 28 '16 at 03:01

0 Answers0