-2

Here is my Code

#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
int main()
{
    char n, d;
string s;
// ofstream out("sample.txt");
ifstream in("sample.txt");
while (!in.eof())
{
    if (in.eof())
        break;
    in >> s;
}
cout << s << endl;
n = (s[6]*10);
cout << n << endl;
return 0;
}

Basically , in file there is a table of 5 line by line. I extracted last string of file which is

5*10=50

but what I Need is last number 50 in an int to furthur process my program. How can I get that last number of string and file into an Integer Variabe?

  • I think you mean 'last number' not 'last digit' last digit is '0'. – pm100 May 24 '22 at 18:43
  • Look at the last character - is it a digit?, if so look at the previous chcracter, keep going till you find a non numeric character. Now take the string from there to the end and pas it to `std::stoi` – pm100 May 24 '22 at 18:45
  • 2
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude May 24 '22 at 19:04
  • 2
    As for your problem, even if `s[6]` is a digit, `s[6]*10` doesn't make sense. There is no character encoding where e.g. `'2' == 2`. – Some programmer dude May 24 '22 at 19:05
  • Parse your string with `string::find` and `string::substr` or parse it with `regex` – limserhane May 25 '22 at 09:05

0 Answers0