-1

I have a text file which that contains only numbers inside , and i have successfully pulled the numbers from the file and stored it inside an array:

my problem is that the array is "string" and i cant do mathematical operations on the array like Addition and Subtraction I have tried to use atoi(array[i][j].c_str()) to convert it to intger but it gives me only the first digit of a number!

my program looks like this for now , its a mess I know :(

#include <iostream>
#include <fstream> 
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{
ifstream iFile("input.txt");      
string line;
string array[7][7];
for (int i=0;i<7;i++){
       for (int j=0;j<6;j++){
       getline(iFile,line);
        if (!line.empty()){
            array[i][j]=line;
       }
       else  {
            break;
       }
   }
}
cout<<"number of processes is:  "<<array[0][0]<<endl;
cout<<"resource types:  "<<array[1][0]<<endl<<endl;
cout<<"Allocation Matrix:"<<endl;
cout<<"   A B C D"<<endl;
cout<<"0: "<<array[2][0]<<endl;
cout<<"1: "<<array[2][1]<<endl;
cout<<"2: "<<array[2][2]<<endl;
cout<<"3: "<<array[2][3]<<endl;
cout<<"4: "<<array[2][4]<<endl;
cout<<"Max Matrix:"<<endl;
cout<<"   A B C D"<<endl;
cout<<"0: "<<array[3][0]<<endl;
cout<<"1: "<<array[3][1]<<endl;
cout<<"2: "<<array[3][2]<<endl;
cout<<"3: "<<array[3][3]<<endl;
cout<<"4: "<<array[3][4]<<endl;
cout<<"Need Matrix:"<<endl;
cout<<"   A B C D"<<endl;
//cout<<"0: "<<array[3][1]+array[2][1]<<endl;
//int c= atoi(array[3][1].c_str());
//int c2= atoi(array[3][1].c_str());
//cout<<c+c2<<endl;



  return 0;
}

my input.txt file looks like this:

5

4

0 0 1 2
1 0 0 0
1 3 5 4
0 6 3 2
0 0 1 4

0 0 1 2
1 7 5 0
2 3 5 6
0 6 5 2
0 6 5 6

1 5 2 0

1:0 4 2 0

edit:

note:if there is an empty line >> stop!

the program is based on banker algorithm which takes the first number from the input.txt as the numbers of of processes

then takes the second number as the numbers of of resource types then takes the next numbers that there is no empty line between them as Allocation Matrix

then takes the next numbers that there is no empty line between them as max Matrix

and here is my problem when i want do Subtraction between Allocation Matrix and max Matrix because both are strings !

as for 1:0 4 2 0 it means do some operations with process number 1

Community
  • 1
  • 1
rakk92
  • 57
  • 1
  • 9
  • `1:0` is that a typo? Is that `:` supposed to be a space? If not, what does it mean? – Baum mit Augen Dec 09 '15 at 23:03
  • It appears that there is a space between each digit in your input.txt file, meaning that all of the numbers are in fact only one digit each. This would explain why atoi() only picks up the first digit. – sifferman Dec 09 '15 at 23:03
  • Possible duplicate of [How to parse a string to an int in C++?](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – Fantastic Mr Fox Dec 09 '15 at 23:03
  • To be honest, I don't quite understand the question. Please [edit] your post to include the actual and the desired behavior as well as a clear problem statement. – Baum mit Augen Dec 09 '15 at 23:05
  • Why do you read the numbers as strings? Try reading as numbers. – Thomas Matthews Dec 09 '15 at 23:48
  • I have edited my post , hope my problem is clear :( – rakk92 Dec 10 '15 at 01:15

1 Answers1

2

You can use atoi but in c++ you have better options.

in c++ you can easily use stringstream to convert these types.

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

int convert_str_to_int(const string& str) {
    int val;
    stringstream ss;
    ss << str;
    ss >> val;
    return val;
}

int main () {
    string str = "1024";
    int val = convert_str_to_int(str);
    cout << "Val is: " << val << ", val/2 is " << val/2 << endl;
}
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
  • Mostly right. Need to check that `ss >> val;` actually read something. Example: contents of `val` will be undefined with input of "fubar". Recommend `stringstream ss(str);` in place of default constructor and then insertion of string. – user4581301 Dec 09 '15 at 23:59