|
-
January 21st, 2017, 09:18 PM
#1
How can I convert the strings to integers?
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 the string array to integers without using the string library. Can someone help? My code won't compile but I am trying to do this: 154: Sum = 4*1 + 5*10 + 1*100 = 154 so it can convert to an integer.
This is what I have so far:
Code:
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int StringToInteger( string 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;
myArray[i].size();
cout << i << endl;
StringToInteger(i);
++i;
}
}
int StringToInteger( string arraySize)
{
int values = 0;
for (int index = 0; index < arraySize.size; index++) {
values *= 10;
values += (arraySize[index] - '0');
}
return values;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|