Convert Strings to Integer

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:

#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

void StringToInteger(string arr[], int arraySize);

int main()
{
ifstream infile;
infile.open("numbers.txt");
const int size = 1000;
string myArray[size];
int i = 0;
while (infile >> myArray[i])
{

cout << "The string is \"" << myArray[i] << "\"" << endl;
++i;
StringToInteger(myArray, i);
}
}

void StringToInteger(string arr[], int arraySize)
{

}

cannot convert to integers without using the string library

create a std::istringstream object instead with the std::string and read that istringstream object into an int object
Last edited on
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.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//==============================================================================================
//               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(const char* 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.
    }
    else
       break;
 }
 if(cNeg=='-')               // If we have a negative sign, convert the value.
    lTotal=-lTotal; 
 
 return lTotal; 
}


_int64 __cdecl _wtoi64(const wchar_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.
    }
    else
       break;
 }
 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.
Last edited on
Topic archived. No new replies allowed.