Menu

Data types problem

Carl Simon
2008-04-28
2012-09-26
  • Carl Simon

    Carl Simon - 2008-04-28

    I create a program to register an person with 4 avail's, the data type is:
    struct SPERSON
    {
    char NAME[40];
    float AVAIL[4];
    };
    struct SPERSON PERSON[10];

    the mask for the user is:
    cout << "Enter the name .....: "; // Read the person name
    cin.getline(PERSON[I].NAME, sizeof(PERSON[I].NAME));
    cout << "Enter the 1a. avail : ";
    cin >> PERSON[I].AVAIL[J]; // Read the person's 4 avail
    .
    .
    .
    cout << "Enter the 4a. avail : ";
    cin >> PERSON[I].AVAIL[J];
    cout << endl;
    //Back to the for...

    when go to register next person the program write this:
    "Enter the name .....: Enter the 1a. avail : "

    i war reading when you are working with diferent data types you will need a special atention on it!

    i have soved this problem using only entrys with data type of char, and the variables avails received the value of char converted to float, but this tech is so strange,
    and i need of the answer for this.
    I use the function atof() of the header cstdlib.h

    i write on dev 4.9.9.2 this error occur, someone know why and a better solution??

    ** My english is poor, because i am brazilian without training my english...

     
    • cpns

      cpns - 2008-04-28

      Your solution is not a bad one, but it may help for you to understand why this happens.

      The console input is line oriented, so nothing is available to console input functions until an entire line has been entered. So when you input into a numeric variable, to have to type:

      <number><newline>

      but the input function only consumes the <numeric> part, leaving the <newline> in the input buffer to be consumed by the subsequent input.

      As I said using string input throughout is a good solution, not least because it allows the possibility of more extensive error checking, but if you want a simple solution, you need to purge the input buffer after each input statement.

      include <limits>

      ...

      cin >> PERSON[I].AVAIL[J];
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ignore the rest of the line

      or more succinctly but less portably:

      cin >> PERSON[I].AVAIL[J];
      cin.sync() ; // Discard all remaining buffered input

      this second solution does not work on all platforms, the semantics of istream::sync() for console input is rather vague in the standard.

      If you do use string input I strongly urge you to use C++ std::string objects rather than C strings.

      You are also flying in the face of convention and readability by using ALLCAPS identifiers. Capitalisation is conventionally reserved for pre-processor macros (and you should avoid them in any case!).

      Clifford

       
      • Carl Simon

        Carl Simon - 2008-04-29

        Thanks a lot, i was trying to flush the buffer but i wasn't knew, i search in various book such as of Deitel, Hebert Shildt, but i dont find my answer, i only thank to you, and this tech what i use is my professor's teachs and he knows other manners, but he is so strange... but you solved my problem thanks!

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.