Hello, everybody can y'all be so kind to help me with my program? I am trying to convert strings to integers. This is my task:
Open a file "numbers.txt"
read integer numbers as strings from the file (one number per line)
convert each string to a numeric representation (without using stoi() or similar functions from the string library)
store converted numbers in an array
close "numbers.txt"
So far I have read integer numbers as strings but cannot convert to integers without using the string library. Can someone help?
This is what I have so far:
This is the sort of thing a person just learning might have a hard time deducing for oneself. Since this is some sort of class assignment I'm not sure I'm doing right by posting finished operational code. But I have it and will post it for you, and I hope you'll put debug print statements in it or step through it with a debugger to see how its working.
//==============================================================================================
// Developed As An Addition To Matt Pietrek's LibCTiny.lib
// By Fred Harris, March 2016
//
// cl _atoi64.cpp /D "_CRT_SECURE_NO_WARNINGS" /c /W3 /DWIN32_LEAN_AND_MEAN
//==============================================================================================
#include "stdlib.h"
_int64 __cdecl _atoi64(constchar* pStr)
{
char c,cNeg=NULL; // c holds the char; cNeg holds the '-' sign.
_int64 lTotal=0; // The running total.
while(*pStr==32 || *pStr==8)
pStr++;
if(*pStr=='-')
{
cNeg='-';
pStr++;
}
while(*pStr)
{
if(*pStr>=48 && *pStr<=57)
{
c=*pStr++;
lTotal=10*lTotal+(c-48); // Add this digit to the total.
}
elsebreak;
}
if(cNeg=='-') // If we have a negative sign, convert the value.
lTotal=-lTotal;
return lTotal;
}
_int64 __cdecl _wtoi64(constwchar_t* pStr)
{
wchar_t c,cNeg=NULL; // c holds the char; cNeg holds the '-' sign.
_int64 lTotal=0; // The running total.
while(*pStr==32 || *pStr==8)
pStr++;
if(*pStr==L'-')
{
cNeg=L'-';
pStr++;
}
while(*pStr)
{
if(*pStr>=48 && *pStr<=57)
{
c=*pStr++;
lTotal=10*lTotal+(c-48); // Add this digit to the total.
}
elsebreak;
}
if(cNeg==L'-') // If we have a negative sign, convert the value.
lTotal=-lTotal;
return lTotal;
}
Its actually code I'm not using anymore. It was part of my custom C Runtime I developed for myself to minimize the size of executables created by C++. Using my own runtime instead of the C++ one I can create executables only a couple k in size. Not tested on other than Microsoft operating systems, I might add.