0

I have an assignment where I find the cost of shipping an item based on input from a file. One section asks me to terminate the program when an "An invalid character is present in the text file"

Expected output:

*************** File Read Error ***************
==> Reading of one or more package
==> parameters failed!!
==> Terminating program!!!
***********************************************`

My output:

    --------------------------------------------------
The package attributes are: 
Package type .................bulk
Total length .................45 inches
Weight .......................2111390112 pound(s)
--------------------------------------------------
The package rate factors are: 
Package Base Rate ............4.75 dollars
Length Factor Used ...........2.5
Weight Factor Used ...........4.5
--------------------------------------------------

Here is my Code:

        if (typeid(length) != typeid(int) || typeid(width) != typeid(int) || typeid(height) != typeid(int) || typeid(weight) != typeid(int)) // tests the type of these variables
    {
        cout << string(15,'*') << " File Read Error " << string(15,'*') << endl;
        cout << "==> Reading of one or more package" << endl;
        cout << "==> parameters failed!!" << endl;
        cout << "==> Terminating program!!!" << endl;
        cout << string(47,'*') << endl << endl;
        return 1;
    }

Here is the text file:

    // invalid character in height, width, length or weight
bulk
20
25
A15
25

Essentially, the program should terminate when it sees A15 because it has a char and expects just an integer.

  • 2
    Please post information as text, not images. – Scott Hunter Sep 23 '19 at 18:20
  • 2
    Don't give only links, provide relevant code directly in question. – Jarod42 Sep 23 '19 at 18:20
  • 1
    Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to create a [mcve] to show us *inside* the question itself. – Some programmer dude Sep 23 '19 at 18:21
  • 2
    `typeid(length) != typeid(int)` is compile time constant. it is independent of file content. – Jarod42 Sep 23 '19 at 18:22
  • Just changed it all to text. Sorry. – Nolan Anderson Sep 23 '19 at 18:28
  • If you try to read an `int` from a C++ stream (e.g. `std::ifstream`) and the input can't be parsed as an `int`, then the stream will set a state that you can check for. – Some programmer dude Sep 23 '19 at 18:34

0 Answers0