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.
#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
#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
#include <iostream>
#include <fstream>
usingnamespace 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>
usingnamespace 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";
}
}
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.