External string to int

Hello, need help with some code. I can't seem to figure out how to convert my external file's string values to int. They are string i believe atleast, I get them using getline. I need them to be int, because i need to sum them up later.

External file is skaitli.txt and contains -

3 7 10 15 37 75 133 337 350 400 500 997 1000



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	fstream txt("skaitli.txt");
	string line;
	
    if (txt.is_open()) {
        
        while (getline (txt, line)) {
            cout << line << '\n';
        }
        txt.close();
        
    }else cout << "Failu nevarēja atrast."; 
        
}


You don't need to convert. Just read them as ints.
1
2
3
4
5
int num = 0;
while (txt >> num)
{
  // do sth. with num
}
Using a stringstream to read a single line of numbers stored in a string:
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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>  // for std::stringstream

int main()
{
   std::ifstream txt("skaitli.txt");

   // file not open, report it and return to the OS
   if (!txt.is_open())
   {
      return std::cerr << "Unable to open file!  Exiting!\n", 1;
   }

   // read the single line of data in the file, and close it.
   std::string data;
   std::getline(txt, data);
   txt.close();

   int sum { };
   int num { };

   // create a stringstream from the data string
   std::istringstream ss(data);

   // read each number in the string as a number
   // no more numbers, the while loop will end
   while (ss >> num)
   {
      sum += num;
      std::cout << "Read number is: " << num << '\n';
   }

   std::cout << "\nThe sum of the numbers is: " << sum << '\n';
}
Read number is: 3
Read number is: 7
Read number is: 10
Read number is: 15
Read number is: 37
Read number is: 75
Read number is: 133
Read number is: 337
Read number is: 350
Read number is: 400
Read number is: 500
Read number is: 997
Read number is: 1000

The sum of the numbers is: 3864

http://www.cplusplus.com/reference/sstream/
Dealing with multiple lines of numbers in a file:
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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>  // for std::stringstream

int main()
{
   std::ifstream txt("skaitli.txt");

   // file not open, report it and return to the OS
   if (!txt.is_open())
   {
      return std::cerr << "Unable to open file!  Exiting!\n", 1;
   }

   std::string data;
   int sum { };

   while (std::getline(txt, data))
   {
      std::cout << "Line of numbers: " << data << '\n';
      
      int num { };

      // create a stringstream from the data string
      std::istringstream ss(data);

      // read each number in the string as a number
      // no more numbers, the while loop will end
      while (ss >> num)
      {
         sum += num;
         std::cout << "Read number is: " << num << '\n';
      }
      std::cout << '\n';
   }
   std::cout << "The sum of the numbers is: " << sum << '\n';
   
   txt.close();
}
Line of numbers: 3 7 10 15 37 75 133 337 350 400 500 997 1000
Read number is: 3
Read number is: 7
Read number is: 10
Read number is: 15
Read number is: 37
Read number is: 75
Read number is: 133
Read number is: 337
Read number is: 350
Read number is: 400
Read number is: 500
Read number is: 997
Read number is: 1000

Line of numbers: 14 9
Read number is: 14
Read number is: 9

Line of numbers: 8 145 99
Read number is: 8
Read number is: 145
Read number is: 99

The sum of the numbers is: 4139
@thmm But how do I sum them up after? They are just all one big int - num, not each one.
Easiest way to sum a bunch of numeric values is do it as you read each one. Both of my code examples do that.
1
2
3
4
5
int num = 0, sum = 0;
while (txt >> num)
{
  sum += num;
}
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   ifstream txt( "skaitli.txt" );
   int sum = 0;
   for ( int i; txt >> i; ) sum += i;
   cout << "Sum is " << sum << '\n';
}


Sum is 3864





If you want to check that your file is accessible, then
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   ifstream txt( "skaitli.txt" );
   if ( txt )
   {
      int sum = 0;
      for ( int i; txt >> i; ) sum += i;
      cout << "Sum is " << sum << '\n';
   }
   else
   {
      cerr << "Can't open file\n";
   }
}
Last edited on
When reading a file that just contains numbers, there is no need/reason to read a line using getline() and then use stringstream to extract number(s). This is just adding an unnecessary complication. Just extract the number direct from the file stream using >>. See thmm and lastchance code above.
Topic archived. No new replies allowed.