#include <fstream>
#include <iostream>
#include <cctype>
#include <cstdlib>
int main ()
{
int totalChar = 0;
int fileSize = 0;
int temp;
// shouldn't this be 2, not 3?
// char cTemp[3];
char cTemp[2];
char input [100];
// shouldn't this be an input stream?
// std::ofstream pali;
std::ifstream pali;
pali.open("pali.txt");
// not a good way to loop through a file
// while (!pali.eof())
// {
// pali << input[fileSize];
// fileSize++;
// }
// The problem is that EOF is not signalled until AFTER
// you attempt the read.
while(pali >> input[fileSize])
{
// only gets here is read was a success
fileSize++;
}
for (int i = 0; i < fileSize; i++)
{
// cTemp[0] is th efirst element, not 1
// cTemp[1] = tolower(input[i]);
cTemp[0] = tolower(input[i]);
cTemp[1] = '\0'; // set string terminator
// This passes a single char, need to pass the whole string
// temp = atoi(cTemp[1]);
temp = atoi(cTemp);
std::cout << temp;
}
return 0;
}
Mainly arrays are indexed from 0, not 1. Strings need to be terminated by the value '\0'. So you needed to put a '\0' as your second char in cTemp.
1 2
cTemp[0]; // contains the char you read in
cTemp[1]; // contains '\0' (end of string marker)