A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

C++ Program help the assignment is to create a program that reads a data file that should contain only integers values and thus should contain only digits, plus or minus signs, and whitespace. the program should print any invalid characters located in the file, and at the end it should print a count of the invalid characters located. (print means show in screen). my current attempt is this:

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    #include <iostream> #include <fstream> #include <cmath> using namespace std; int main() { ifstream fin; int x; fin.open("invalid.txt"); cout <<"state of fin stream:" << endl<<fin.bad()<<","<<fin.eof()<<","<<fin.fail()<<","<<fin.good()<<endl; if (fin.fail()) { cerr<<"error opening file"<<endl; system("pause"); return -1; } if(fin. fail()) { cerr<<"could not open input file"; } fin>>x; while (!fin.eof()) { cout<<x<<endl; fin>>x; } cout <<"state of fin stream:" << endl<<fin.bad()<<","<<fin.eof()<<","<<fin.fail()<<","<<fin.good()<<endl; cerr <<"Bad data."<<cout<<endl<<" "<<endl; system("pause"); return 0; }

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i have no idea how to make the program not just crash and burn when it sees a letter in the file, and no idea how to make it display the invalid characters. Pleas help!

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I would try to run through the file once, collecting all the invalid characters (into a vector or array of characters), and then print them all out at the end. This would involve: making a storage vector, running through the file finding invalid entries, closing the file, printing out the elements of the array, and then printing out the size of the vector.

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.