Hi, I've just started with c++ and I have no clue how to do this. Right now I have a code that just printing out the text lines in my file and how many lines there is just to check that i can read from my file...

But what I want to do and can't figure out is to pick out and store numbers from text lines.

Consider this as user inputs to a file:

Number 10 Downing Street
My birthday is 6 May 1980

And now I want to read from the .txt file and store the number 10 from the first text line and then store 6 and 1980 from the second line of text.

//..Some codes to put in lines of user specific text to a file. [done]

//And now the code that outputs the text rows and number of rows in
//the file but have nothing to do with what i want to achive :(
  
  ifstream inputfile(filename);
  char text;
  int n = 0; 
  
  while (inputfile.get(text))
    {
      cout.put(text);
      if (text == '\n')
	++n;
    }
  
  cout << endl << There is " << n << " rows of text";

You need a way to tell whether a string is a number or not. Define exactly what a "number" is (negatives, decimals, commas, just digits, etc.), then you can write a function to test a string. If you are only using integers, you can use the atoi function. If it returns something nonzero, it's an integer. If it returns 0, you'll need a further test: compare the string to "0" (errors return as 0 in atoi, so test whether it's actually the number 0).

stringstreams can be quite helpful here. You can read in a line, break it up using the >> operator, and you can also read into an integer variable and check the error flags. If you try to read "Fred" into an integer, the error bit will be set. Check that bit.

Also you can go through the string character by character and test the characters one at a time using isdigit from cctype.

Since u only want the numbers to be recovered , u must be aware of the ascii equivalent for the number

0 - 48
----------
----------
9 - 57


i suggest u make a char variable , fetch the input character vice and then check the ascii equivalent of the numbers , if the char "c" lies between 48 and 57 then print it , or else ignore
I hope i am giving u some idea from the code below

char c ;
while (is.good())     // loop while extraction from file is possible
  {
    c = is.get();       // get character from file
    if (c>=48 && c <= 57 )  // check the ascii range for 0-9
      cout << c;
  }

Thx guys, I'll look into both of your sugestions and maybe get back if it's still not straight for me.

thx again.

More sugestions are welcomed though :)

>if the char "c" lies between 48 and 57 then print it , or else ignore
Congratulations! You've introduced a completely unnecessary dependency in your program due to sheer ignorance and reduced the readability of your program at the same time. Double whammies like that are rare. Compare and contrast:

// Restricted to ASCII
// Magic numbers
if ( c >= 48 && c <= 57 )
  cout << c;
// 100% portable, even to systems without ASCII
// Obvious logic
if ( c >= '0' && c <= '9' )
  cout << c;

ohh god , i completely forgot about that..................
Well initially i had used the numbers without the quotes and i was wondering why didnt it work , then i switched over ASCII codes for that .

So , the code will be like this :

while (is.good())     // loop while extraction from file is possible
  {
    c = is.get();       // get character from file
    if (c>= '0'&& c <= '9' ) // No need of ASCII just single quotes 
      cout << c;
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.