using *.get from a filestream to read integers

i've heard that it's possible to use isdigit to read integers from a file. however, i'm a bit lost as to what this means...

myfile.txt
 
Jason 1010101


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <ctype.h> // 'isdigit', but i have yet to use it
using namespace std;

int main ()
{
  char myName[5];
  int myNumber;

  ifstream myFile;
  myFile.open("file.txt");
  myFile.get(myName,5);

  //here is the problem
  myFile.get(myNumber);

  cout << "My name is " << myName << " : " << myNumber << ".\n";
  return 0;
}
Last edited on
Get a char, check if it is a digit. If yes, subtract '0' from it, store it somewhere and repeat. That way you'll get all digits of a number. It's simple to build a number from its digits (you don't even need the temporary array really).
Though there is no reason why you would want to do that. Operator >> does that for you.
i'm confused...

so change line 15 to:
myFile.get(myNumber);
(isdigit(myNumber) ..?
bump for a noob
1
2
int my_number;
cin >> my_number;
This does exactly what you want for you.
Topic archived. No new replies allowed.