Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 08-12-2006, 05:04 PM   #1 (permalink)
Chase's Avatar
 
CECS Major

Join Date: Jul 2005

Location: Louisville, KY

Posts: 387

Chase is on a distinguished road

Send a message via MSN to Chase Send a message via Yahoo to Chase Send a message via Skype™ to Chase
Default c++ troubles...again

I just wrote the following program so I could mess around/practice with my class skills:

Code:
////////////////////////////////////////////////////////////////////////
//Class Program - This program illustrates the properites of a class///
//////////////////////////////////////////////////////////////////////
//By: Chase//
//////////////////

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
//Declare Class

class userInfo {
               public:
                   int birthMonth, ssn, phone;
                   char firstName [128];
                   char lastName [128];
                   char middleName [128];
                   
                   };

//delcare protos
bool getData (userInfo& userOne);
void displayData (userInfo& userOne);


//start main ()                   
int main(int nNumberofArgs, char* pszArgs[])
{
       //set max accounts
    const int maxClassInfo = 10;
   
    //declare variable of userInfo class
    userInfo userOne[maxClassInfo];
    
    //tell the user what is going on
        cout << "This program will allow you to input your profile and\n"
             << "I will spit it back at you."
             << endl
             << "Type 'exit' to terminate the program.\n\n";
    
    //declare index for array storage  and set it to the first array compartment
    int index = 0;
    
    //set while loop to check for user 'exit' input and to make sure
    // there are no more than 9 accounts already created
    while (getData(userOne[index]) && index < maxClassInfo)
          {                        
                                   //add one to index
                                   index++;
}

//spit back to the user what they just input
cout << "Profile Information: \n";
for (int i = 0; i < maxClassInfo; i++)
{
    displayData(userOne[i]);
    
}

//allow user time to look at results then press any key to terminate
system ("PAUSE");
return 0;
}

//program the protos I declared earlier

//displayData - spit back to the user what they input
void displayData(userInfo& userOne)
{    
     cout << "\n"
          << userOne.birthMonth
          << "\n"
          << userOne.ssn
          << "\n"
          << userOne.phone
          << "\n"
          << userOne.firstName
          << "\n"
          << userOne.middleName
          << "\n"
          << userOne.lastName
          << "\n";
          
          }

// getData - add property info to the userInfo class object userOne
bool getData (userInfo& userOne)
{
     cout << "First Name: ";
     cin >> userOne.firstName;
     cout << "\n...checking...\n";
     //check to see if the user wants to quit
     char terminate[10] = "exit";
     if (stricmp(userOne.firstName, "exit") == 0)
     {
                                    return false;
                                    
                                    }
                                    
     //if everything is kosher, keep asking questions
     cout << "Middle Name: ";
     cin >> userOne.middleName;
     cout << "\n";
     
     cout << "Last Name: ";
     cin >> userOne.lastName;
     cout << "\n";
     
     cout << "Birth month (numerical): ";
     cin >> userOne.birthMonth;
     cout << "\n";
     
     cout << "SSN: ";
     cin >> userOne.ssn;
     cout << "\n";
     
     cout << "Phone: ";
     cin >> userOne.phone;
     cout << "\n";
     
     return true;
     
     }
Everytime I run the program eveything works fine until I type exit to exit, then my data + all sorts of other random data is spit out from the array. I know the cause of this is that the positions in the array are currently being used and if they are all not replaced I will get whatever data is currently in them instead of blank data. How can I fix this...I don't want that random data spouted out after every exit if I haven't filled up all of the array slots.
Thanks for any help,

::-Chase-::
Chase is offline  
Old 08-12-2006, 06:38 PM   #2 (permalink)
Chase's Avatar
 
CECS Major

Join Date: Jul 2005

Location: Louisville, KY

Posts: 387

Chase is on a distinguished road

Send a message via MSN to Chase Send a message via Yahoo to Chase Send a message via Skype™ to Chase
Default

::-Disregard-::
I found that I had a mistake in the FOR loop and fixed it.

::-Chase-::
Chase is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On