storing data into an array

This is a discussion on storing data into an array within the C++ Programming forums, part of the General Programming Boards category; I want to store 15 names in an array! but it doesn't seem working correctly... anyone can figure it out ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    storing data into an array

    I want to store 15 names in an array! but it doesn't seem working correctly... anyone can figure it out ?

    The problem:
    Every time I promote the program to enter a name it will only be stored in the first position in the array. For example, when I promote the program to enter 3 names then print them.. the program will only prints the last input, unlike what it supposed to do and print all inputs

    Code:
    #include <iostream>
    using namespace std;
    
    class healthClubMaintain{
    
    private:
    	 string Al[15];
    	 string Al2[15];
    	 	 
    	 int counter,i,s;
    	
    	int classCleintName;
    	int classClientIDNumber;
    public:
    	void addClient(string);
    	void printClient();
    
    };
    
    
    void healthClubMaintain::addClient(string clientName){
    	counter=0;
    	
    	if (counter ==15){
    		cout << " STOP!";
    
    	}
    	else{
    		Al[counter]=clientName;
    		counter++;
    	}
    		
    
    }
    
    void healthClubMaintain::printClient(){
    	
    		cout<<Al[0];
    		cout <<Al[1];
    }
    
    
    
    
    int main(){
    
    int number,loop=0;
    int clientIDNumber,programNumber,approvalIDNumber,clientIDA,programNumberA;
    string clientName,programName,approvalName;
    healthClubMaintain Club;
    
    while ( loop==0){
    
    	cout<< "Select one of the following option:"<<endl;
    	cout<< "(1) Add a new client to the program"<<endl;
    	cout<< "(2) print"<<endl;
    
    	cin >> number;
    
    
    if ( number ==1 ){
    	cout << "Insert the client's name" << endl;
    	cin >> clientName;
    //	cout << "Insert a new ID" << endl;
    //	cin >>clientIDNumber;
    	Club.addClient(clientName);
    }
    
    if ( number == 2){
    	 Club.printClient();
    }
    
    }
    
    }
    Last edited by aama100; 02-20-2008 at 04:44 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    In the future, it would help if you explained how it is not working. Are there compiler errors? Is it running but giving bad output? What's wrong?

    In this code, why is counter being re-set to 0 inside addClient? The counter should store how many strings have been added. You probably want a constructor for your class to initialize it.

    There are a few other strange things about your design. Make sure you start small and add only a few things at a time when coding. Also you should come up with a plan before you start. Answer design questions, like whether HealthClubMaintain should hold one client or all clients, before you dive in.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I explained the problem up there

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The issue I mentioned sounds like it could be the source of the problem you described.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Quote Originally Posted by Daved View Post
    The issue I mentioned sounds like it could be the source of the problem you described.

    hey thanks a lot I followed what you have said and it is working!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    May I suggest you indent a little better? Here's a good resource for learning some: http://cpwiki.sf.net/User:Elysia/Indentation
    Indentation helps others read code and helps you find bugs too!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 03:03 PM
  5. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM

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