C++ programming problem


C++ help with Simple Program?

I'm a beginner and I am having difficulty with this problem:

Write a program that reads a data file that should contain only integer values(only digits, plus/minus signs, whitespace). The program should print any invalid characters located in the file, and print a count of the invalid characters located.

My code, which complies is this:

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

ifstream infile;
infile.open("test.txt");

//Check for Error
if (infile.fail()){
cerr << "Error opening file" << endl;
}

char item;
int count = 0;

// Read a file until you have reached the end
while (!infile.eof()) {
infile >> item;
if (item !=' '){
count++;
}
if (item !='+'){
count++;
}
if (item !='-'){
count++;
}
if ((item > '0') || (item < '9')){
count++;
}}

cout << "Invalid Characters Found are " << count << endl;

infile.close();

system("pause");
}

//My program comes back with 50, but there is only 2 items that should be invalid.

My test.txt file contains
a
b

Hi,

first, i'm not sure, but it might be usefuk to assume the first element of your file to be a '3'.

Then the first if - statement will increase "counter" by one
but the second and the third as well, which is not intended.
"Counter" has only to be increased, if all if - statements fail.

Like that maybe:

1
2
3
4
5
6
7
8
9
10
11
12
13
fl = false;

while( ... whatever ...){
if ( a == b){
fl =true;
else if (a==c){
fl = true;
}
else if ( ....){
 etc...

}
} // end while 

and afterwards check, if fl ==true


or the other way around ... you'll make it ...

Greets,
Last edited on
never assume anything. The populare axiom of programming is "if it CAN happen, it WILL happen". If you want to be good, then assume your first char might not be a 3. If you want to be average, then assume it is....

the best way to do it would be to use a bool. When you find a number, set the bool to true. when the bool is true, get the chars and add to the array/sttring. if you hit a non-number, then set the bool to false.

this would be fool proof.

hints:

- Use a while loop
- You could write a function to get 1 number from the stream (if you wanted)
Last edited on
@IWishIKnew:

So, what i intended to say is : IF first_el = '3' THEN program_behaves_not_as_intended END
That's a classic counterexample ... from which follows "PROGRAM DOESN'T WORK IN THIS CASE" which means "PROGRAM DOESN'T WORK" ...

Any clearer ?
If you WANT TO BE GOOD, do not deny the possibility to disprove an assumption by ONE example.

And furthermore, did you read my post ?

I do use a bool , i just left out initializations.
And I set it to TRUE by the way when finding what i search ....
and my code - segment uses a while loop ....

would this be fool proof ??
I guess not, which can be shown by example ....




Fluppe, did it occure to you that I may not have been addressing you? you did not have a post when I started mine.
Hi again,

well then sorry for that one ... i was sure you were adressing me, so i was quite upset.
Hope i wasn't too rude after all.

I thought you were refering to my post because i wrote

Hi,

first, i'm not sure, but it might be usefuk to assume the first element of your file to be a '3'.

....
and then you wrote

never assume anything. The populare axiom of programming is "if it CAN happen, it WILL happen". If you want to be good, then assume your first char might not be a 3. If you want to be average, then assume it is....

....
You'll have to admit that the thought comes naturally, that you adressed me.
However, as you didn't, i excuse myself and hope you are not affected too much.
@Fluppe:

Also, mabey you shouldn't assume the first element is a '3'. I'm thinking write a function to return a bool to recognize numbers, that way, we can skip any junk that might be at the beginning.

example:

psuedocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* string correct_the_format_of_line(const string&); 
const string& s is the variable to the emplementation*/
string temps(s);
if(temps.size() == 0)
{
     return temps;
}

//isnumber is a function that you will need to write
while(!isnumber(temps[0]) && (temps.size() > 0))
{
    temps.erase(temps.begin());
}
return temps;


so, this way, we can assume with absolute certainty. I like to make sure I can assume things like that, but I suppose you don't have to. As long as the file isn't going to be opened up and messed with. I think this is a reasonable suggestion.
Last edited on
Topic archived. No new replies allowed.