Error checking values from txt

hey everyone i am trying to create a program that error checks values from a text file
Examples of valid number input: 6 0 15 9 12
Examples of invalid number input: 16 -4 8.54 8v +6 127 b

i have so far been able to get the range but, i can't figure out a way to check for letters when i have a file with v as the third value eg. 4 3 v 5 the numbers after the v aren't being outputted

i have tried using isdigit but it won't work


thanks in advance



int ch;
int v;
ifstream infile(argv[1], ios::in | ios::binary);
if (!infile)
{
cout << "F" << endl;
return 0;
}

while (infile >> ch)
{




if (ch > 15 || ch < 0)
{
cout << "invalid value in file" << endl;
}

else

{
v = ch;
cout << v << endl;


}


}
infile.close();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
   stringstream in( "16 -4 8.54 8v +6 127 b" );
   for ( string word; in >> word; )
   {
      double x;
      stringstream ss( word );
      ss >> x;
      char c;
      if ( ss && !(ss >> c) ) cout << "Valid input   " << x << '\n';
      else                    cout << "Invalid input " << word << '\n';
   }
}


Valid input   16
Valid input   -4
Valid input   8.54
Invalid input 8v
Valid input   6
Valid input   127
Invalid input b
Hello gsid8365,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


It is best to post the whole program. This way everyone can see what you are doing and have the ability to test the program.

This line ifstream infile(argv[1], ios::in | ios::binary);. The "ifstream" already says it is for input, so the "ios::in" is not needed. I have no idea why you think it should open in "binary" mode. It is just a plain text file of numbers. I have tried it both ways and saw no difference.

Putting this "16 -4 8.54 8v +6 127 b " in the input file it read the "16", "-4" and "8.54" storing this as an "8". After that the while loop ended an I am not sure why. Unless it has to do with the decimal point and I am not seeing it yet.

"v = ch;" You have what you need in "ch". There is no point to put it into "v" just to print it out.

Hope that helps,

Andy
Topic archived. No new replies allowed.