Thread: Displaying output

  1. #1
    Registered User hungrymouth's Avatar
    Join Date
    Oct 2012
    Posts
    19

    Displaying output

    So I have problem right now. I want the social security, first name, middle initial, last name, date of birth, state, zip code to display all at once. Right now it displays one at a time. Except for the first,middle initial, and last name. Those are displaying all at once. I just need the social security, state and zip code to display along with the first and last name and middle initial. I tried doing it but I just can't seem to get it so anyone have an idea how to do it.

    Code:
    #include "stdafx.h"
    
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    
    
    #include "Header.h"
    using namespace std;
    
    
    int main()
    
    {
    
        Heading();
        ClearTheScreen();
        WriteData();
        ReadData();
        
    
    return 0;
    }
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    #include <windows.h>
    #include <string>
    #include <ctime>
    #include <sstream>
    #include <string>
    
    
    using namespace std;
    
    
    void SetCursorPosition(int X, int Y)
    {
        COORD XY = { Y,X };
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),XY);
    }
    void SetTextColor(int Color)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),Color);
    }
    void PauseTheScreen(int x, int y, int Color)
    {
        SetCursorPosition(x,y);
        SetTextColor(Color);
        system("pause");
    }
    
    
    void ClearLine (int LineNumber)
    {
        SetCursorPosition(LineNumber ,0);
        for ( int k = 0; k < 80; k++)
            cout << " ";
    } 
    void ClearTheScreen()
    {
        system("cls");
    }
    void PauseTheScreen()
    {
        system("pause");
    }
    
    void Heading()
    {
        ClearTheScreen();
        SetCursorPosition(0,20);
        SetTextColor(11);
        cout <<    "VENTURA COUNTY COMMUNITY COLLEGE DISTRICT";
        SetCursorPosition(0,20);
        SetCursorPosition(1,32);
        cout << "Ventura College";
        SetCursorPosition(3,37);
        cout << "CS V30";
        SetCursorPosition(4,33);
        cout << "Beginning C++";
        SetCursorPosition(6,39);
        cout << "by";
        SetCursorPosition(7,33);
        SetTextColor(11);
        cout << "blah";
        SetCursorPosition(8,8);
        SetTextColor(14);
    }
    
    
    fstream ReadWrite;
    
    char Buffer[65];
    char SocialSecurityNumber[12];
    char FirstName[16];
    char MiddleInitial[2];
    char LastName[21];
    char DateOfBirth[11];
    char State[3];
    char ZipCode[6];
    
    void OpenFile()
    {
        ReadWrite.open("Records.txt",ios::in | ios::out | ios::app);
    }
    void CloseFile()
    {
        ReadWrite.close();
    }
    void FormatData()
    {  
        unsigned int k;
        for ( k = 0; k < 65; k++)
        {
            Buffer[k] = 32;  // 32 is the ASCII code for space
        }
        Buffer[64] = '\0';
        for ( k = 0; k < strlen(SocialSecurityNumber); k++)
        {
            Buffer[k] = SocialSecurityNumber[k];
        }
        
        for ( k = 0; k < strlen(FirstName); k++)
        {
            Buffer[k+11] = FirstName[k];
        }
    
        for ( k = 0; k < strlen(MiddleInitial); k++)
        {
            Buffer[k+26] = MiddleInitial[k];
        }
    
        for ( k = 0; k < strlen(LastName); k++)
        {
            Buffer[k+27] = LastName[k];
        }
    
        
    
        for ( k = 0; k < strlen(DateOfBirth); k++)
        {
            Buffer[k+47] = DateOfBirth[k];
        }
        
        for ( k = 0; k < strlen(State); k++)
        {
            Buffer[k+57] = State[k];
        }
    
        for ( k = 0; k < strlen(ZipCode); k++)
        {
            Buffer[k+59] = ZipCode[k];
        }
    }
    void ExplodeData()
    {
        Heading();
        int k;
        for (k = 0; k < 11 ; k++)
        {
            SocialSecurityNumber[k] = Buffer[k];
        }
        SocialSecurityNumber[11] = '\0';
        
        for ( k = 0; k < 15 ; k++)
        {
            FirstName[k] = Buffer[k+11];
        }
        FirstName[15] = '\0';
    
        for ( k = 0; k < 1 ; k++)
        {
            MiddleInitial[k] = Buffer[k+26];
        }
        MiddleInitial[1] = '\0';
    
        for ( k = 0; k < 20 ; k++)
        {
            LastName[k] = Buffer[k+27];
        }
        LastName[20] = '\0';
    
        for ( k = 0; k < 10 ; k++)
        {
            DateOfBirth[k] = Buffer[k+47];
        }
        DateOfBirth[10] = '\0';
    
        
        for ( k = 0; k < 2 ; k++)
        {
            State[k] = Buffer[k+57];
        }
        State[2] = '\0';
    
    
        for ( k = 0; k < 5 ; k++)
        {
            ZipCode[k] = Buffer[k+59];
        }
        ZipCode[5] = '\0';
        
        SetTextColor(10);
        SetCursorPosition(10,24);
        cout << "SocialSecurity : " << SocialSecurityNumber;
        SetCursorPosition(28,24);
        SetCursorPosition(12,24);
        SetTextColor(10);
        
    
        cout << "FirstName      : " << FirstName;
        SetCursorPosition(28,24);
        SetCursorPosition(14,24);
        SetTextColor(10);
    
        cout << "Middle Initial : " << MiddleInitial;
        SetCursorPosition(28,24);
        SetCursorPosition(16,24);
        SetTextColor(10);
    
        cout << "Last Name      : " << LastName;
        SetCursorPosition(28,24);
        SetCursorPosition(18,24);
        SetTextColor(10);
    
        cout << "DateOfBirth    : " << DateOfBirth;
        SetCursorPosition(28,24);
        SetCursorPosition(20,24);
        SetTextColor(10);
    
        cout << "State          : " << State;
        SetCursorPosition(28,24);
        SetCursorPosition(22,24); 
        SetTextColor(10);
    
        cout << "ZipCode        : " << ZipCode;
        SetCursorPosition(28,24);
        SetCursorPosition(24,24);
        SetTextColor(10);
        PauseTheScreen();
        ClearTheScreen();
    }
    
    
    // Bonus
    
    bool isValidSsn()
    {
        for(int index = 0; index < 11; index++)
        { 
            if(index == 3|| index == 6)
            {
                if(SocialSecurityNumber[index] != '-')
                    return false;
            }    
            else
            {
                if(SocialSecurityNumber[index] < '0' || SocialSecurityNumber[index] > '9') 
                {
                    return false;            
                }
            }
        }
        return true;
    }
    
    bool isValidDateofBirth()
    
    {
        for(int index = 0; index < 10; index++)
        { 
            if(index == 2|| index == 5)
            {
                if(DateOfBirth[index] != '/')
                    return false;
            }    
            else
            {
                if(DateOfBirth[index] < '0' || DateOfBirth[index] > '9') 
                {
                    return false;
                }
            }
        }
    
        int month = (DateOfBirth[0] - '0') * 10 + (DateOfBirth[1] - '0');
        int day = (DateOfBirth[3] - '0') * 10 + (DateOfBirth[4] - '0');
    
        if(month < 1 || month > 12)
                return false;
    
        if(day < 1 || day > 31)
            return false;
    
        if (month ==2)
        {
            int year = (DateOfBirth[6] - '0');
            year = year * 10 + (DateOfBirth[7] - '0');
            year = year * 10 + (DateOfBirth[8] - '0');
            year = year * 10 + (DateOfBirth[9] - '0');
    
            bool leapyear = year % 4 == 0;
    
            if(leapyear) 
            {
                if ( day > 29 )
                    return false;
            }
            else 
            {
                if ( day > 28 )
                    return false;
    
            }
    
        }
        else if( month == 4 || month == 6 || month == 9 || month == 11 )
        {
            if ( day > 30 )
                    return false;
        }
        else 
        {
            if ( day > 31 )
                    return false;
        }
    
        return true;
    }
    
    
    
    void ReadData()
    {
        int NumberOfRecords,LengthOfFile,k;
        OpenFile();
        ReadWrite.seekg(0,ios::end);
        LengthOfFile = ReadWrite.tellg();
        NumberOfRecords = LengthOfFile/65;
    
        for ( k = 0; k < NumberOfRecords; k++)
        {
            ReadWrite.seekg( k * 65,ios::beg);
            ReadWrite.read(Buffer,65);
            ExplodeData();
        }
    
      CloseFile();
    }
    
    
    void WriteData()
    {
        int AddMore = 1;
    
         while ( AddMore )
        {
            Heading();
            bool breakToEnd = false;
            do {
                   SetCursorPosition(14,14);
                   SetTextColor(12);
                   cout << "Social Security Number : ";
                
                   SetCursorPosition(14,39);
                   cin>>SocialSecurityNumber;
                   if ( SocialSecurityNumber[0] == 'Z' && SocialSecurityNumber[1] == 'Z' && SocialSecurityNumber[2] == 'Z' )
                   {
                       ClearTheScreen();
                       Heading();
                       AddMore = 0;
                       breakToEnd = true;
                       break;
                    }
    
                ClearLine(15);
    
                    if(!isValidSsn())
                    {
                       ClearLine(14);
                       SetCursorPosition(14, 40);
                       cout << "ERROR";
                    }
                
               }
                    while(!isValidSsn());
    
                    if(breakToEnd)
                    break;
    
                    SetCursorPosition(16,14);
                    SetTextColor(12);
                    cout << "First Name             : ";
                    
                    SetCursorPosition(18,14);
                    cout << "Middle Initial         : ";
                    SetTextColor(12);
    
                    SetCursorPosition(20,14);
                    cout << "Last Name              : ";
                    SetTextColor(12);
    
                    SetCursorPosition(16,39);
                    cin >> FirstName;
    
                    SetCursorPosition(18,39);
                    cin >> MiddleInitial;
    
                    SetCursorPosition(20,39);
                    cin >> LastName;
    
          do {  
                SetCursorPosition(22,14);
                cout << "Date Of Birth          : ";
                SetTextColor(12);
                SetCursorPosition(22,39);
                cin >> DateOfBirth;
    
                ClearLine(23);
    
                if(!isValidDateofBirth())
                { 
                    ClearLine(22);
                    SetCursorPosition(22,30);
                    cout << "ERROR";
                    
                }
    
             }
                while(!isValidDateofBirth());
                SetCursorPosition(24,14);
                cout << "State                  : ";
                SetTextColor(12);
        
    
                SetCursorPosition(26,14);
                cout << "Zip Code               : ";
                SetTextColor(12);
    
    
                SetCursorPosition(24,39);
                cin >> ZipCode;
                SetCursorPosition(26,39);
                cin >> State;
    
        
                FormatData();
                OpenFile();
                ReadWrite.write(Buffer,65);
                CloseFile();
        }
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    While not related to your problem, there are a couple of things you should fix:

    - Don't use global variables. Use local variables and pass required variables as parameters instead.
    - Don't use char arrays. Use std::string along with its rich interface. You can easily assign, append and even check its length. You may also want to take a look at its constructors or the resize member.
    - Stop assuming length! Sure, a social security number may be 11 digits or something, but what if the user didn't enter 11 characters, or what if it's longer than 11 characters? You need to be careful about out-of-bounds accesses with strings and arrays, so check the length instead of saying it's always N characters. Check the .size() function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User hungrymouth's Avatar
    Join Date
    Oct 2012
    Posts
    19
    Well for our project, my professor wants us to use char arrays so I can't really do much about that. And I already have my program set up so if a user enters 11 or more characters or less than 11 I have an error message displayed so they would have to retype it until they get it right. I just need everything to display at once that's my only issue.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    What do you mean "all at once"? What is it doing that is unexpected?

    Give us a sample run of what is and also a sample of the desired result.

    Soma

  5. #5
    Registered User hungrymouth's Avatar
    Join Date
    Oct 2012
    Posts
    19
    Ok I want mine to display like this:


    Writing Data:

    Social Security Number:

    First Name:

    Middle Initial:

    Last Name:

    DateOfBirth

    State:

    ZipCode:

    Mine Currently is like this:

    Writing Data:

    Social Security Number:

    And once you press enter it displays the next section which is First Name
    So everytime you are done entering the data and you press enter, the next data pops out and I want everything to display at once like how I showed it in the above example

    The problem lies in my void writedata. The while loops are messing up my format and I just can't seem to make it display all at once
    Last edited by hungrymouth; 04-29-2013 at 04:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with displaying the output
    By yoda in forum C Programming
    Replies: 4
    Last Post: 02-04-2013, 11:24 PM
  2. Need help displaying output from function
    By c++prog in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2008, 10:01 PM
  3. displaying a array to output
    By gkoenig in forum C Programming
    Replies: 19
    Last Post: 02-11-2008, 04:18 AM
  4. Help with displaying output
    By tallguy in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2007, 06:38 PM
  5. Trouble displaying output
    By Guti14 in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2003, 08:39 PM