Program logic not working..

This is a discussion on Program logic not working.. within the C++ Programming forums, part of the General Programming Boards category; Hello, I am am writing a program that reads in data for a student record (first name, middle initial, last ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    14

    Unhappy Program logic not working..

    Hello,

    I am am writing a program that reads in data for a student record (first name, middle initial, last name, student id, etc) and then displays it on the screen for a report. Nothing fancy, just our first program assignment using structured data. Here are parts of my program:

    // Type declarations

    enum GenderType {MALE, FEMALE};
    enum RegistrationType {T, M, U};

    struct StudentRecord
    {
    char FirstName[31];
    char MiddleInitial;
    char LastName[31];
    int StudentID;
    GenderType Gender;
    RegistrationType Registration;
    float GPA;
    };

    StudentRecord Student;

    void InputStudentID( StudentRecord& ); // Prototype for the InputFirstNamefunction

    InputStudentID(Student);

    void InputStudentID(StudentRecord& aStudent) // Function heading
    {
    do
    {
    cout << endl << "Enter Student's ID #: ";
    // cin.clear();
    cin >> (aStudent.StudentID);

    if ((!cin) || (!(aStudent.StudentID >= 1000) && (aStudent.StudentID
    <= 9999999)))
    {
    cout << endl << "The Student's ID # must be numeric and be between
    4 - 7 digits long."
    << endl << "Please enter the amount again." << endl;
    cin.ignore(10, '\n');
    }
    }
    while ((!cin) || (!(aStudent.StudentID >= 1000) && (aStudent.StudentID
    <= 9999999)));

    return;
    }


    Basically what is happening here is I am trying to keep the user from entering any value other than a numeric value and within the range of 1000 to 9999999. The loop should keep repeating until the user keys in a correct value. When I type in the following:

    9999A
    999999A

    it SHOULD error out but instead it quits the entire program. I am using the notation "if (!cin)..." in order to check for numerics. For some reason, this is not working correctly or if it it, I can't see why it's quitting my program when I type in these two values.

    Can someone see where my logic is messed up? Thanks!

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    You were right it is just a logical mistake....here's the good compiled version of your program....


    ;;;;;;;;;;

    #include <iostream.h>
    #include <stdlib.h>

    enum GenderType {MALE, FEMALE};
    enum RegistrationType {T, M, U};

    struct StudentRecord
    {
    char FirstName[31];
    char MiddleInitial;
    char LastName[31];
    int StudentID;
    GenderType Gender;
    RegistrationType Registration;
    float GPA;
    };

    StudentRecord Student;

    void InputStudentID( StudentRecord& ); // Prototype for the InputFirstNamefunction

    int main()
    {
    StudentRecord Student;
    InputStudentID(Student);
    system("pause");
    return 0;
    }

    void InputStudentID(StudentRecord& aStudent) // Function heading
    {
    do
    {
    cout << endl << "Enter Student's ID #: ";
    // cin.clear();
    cin >> (aStudent.StudentID);

    if (aStudent.StudentID <= 1000 || aStudent.StudentID >= 9999999)
    {
    cout << endl << "The Student's ID # must be numeric and be between";
    cout << "4 - 7 digits long.";
    cout << endl << "Please enter the amount again." << endl;
    cin.ignore(10, '\n');
    }
    }
    while (aStudent.StudentID <= 1000 || aStudent.StudentID >= 9999999);
    }


    ....

    if you insist on using (!cin) then you need to include it before the "if" statement, for some reason the loop becomes infinite, maybe the whole (!cin) statement is wrong.....
    i got rid of it and all of it compiles fine....

    hope this helps....

    good luck....

    Regards,
    matheo917

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    gotta do it here...

    have to write here.....

    private messages only take 1000 characters....


    Hi again........
    of course there's another way to solve this...remember C++ is a very very powerful language....

    just an update:

    the reason why a program accepts the input 1111a is because the comparisons that you are doing pertain to "NUMBERS" not characters... so if you first enter a number followed by a character then the compiler will read only the numbers and if it encounters a characters it will ignore the rest....however if you will START with a character your loop most likely will become infinite and go on forever....

    the way to fix it is to send it to another function (right before it enters the loop) to have it examine the statement piece by piece whether it is a chracter or a number....

    --- first of all you would have to include another header file called
    .......... ctype.h ............

    - the function that would check for a character is ........ isalpha(char SomeLetterGoesHere)............. --- this function would return TRUE if your character is a letter...

    - the function that would check for a number is ....... isdigit(char SomeCharacterGoesHere) ............. --- this function would return TRUE if your character is a "DECIMAL" digit ....

    hope this helps a little bit....

    .... the only problem is that if you would have to "CAST" the integer so it would become a "STRING" or an array of character so that this function can examine your input "character-by-character" and then cast it back to a INTEGER ....

    well, as you can see, there's a lot more work involved in this...
    but don't get discouraged....

    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program logic
    By kermit in forum C Programming
    Replies: 12
    Last Post: 08-16-2003, 03:42 PM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 11:14 PM
  4. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21