I got weird outputs characters when I print

This is a discussion on I got weird outputs characters when I print within the C++ Programming forums, part of the General Programming Boards category; Problem: Printing weird characters instead of printing ID#s. The program's function: Add a new client's name and a new ID, ...

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

    I got weird outputs characters when I print

    Problem: Printing weird characters instead of printing ID#s.

    The program's function: Add a new client's name and a new ID, and then it is your choice if you want to print.

    it compilers without errors


    Code:
    #include <iostream>
    using namespace std;
    
    class healthClubMaintain{
    
    private:
    	 string names[15];
    	 string idnumbers[15];
    	 int counter,counter2,i,s;
    	
    
    public:
    	void addClient(string,int);
    	void printClient();
    	
    	healthClubMaintain() {  // default constructor
        counter = 0;
    	 counter2 = 0;
    
    }
    
    };
    
    
    void healthClubMaintain::addClient(string clientName, int clientIDNumber){
    	
    	if (counter<15){
    		names[counter]=clientName;
    		counter++;
    	}
    	
    	if (counter2<15){
    		idnumbers[counter2]=clientIDNumber;
    		counter2++;
    	} 
    		
    
    }
    
    void healthClubMaintain::printClient(){
    		
    		i=0;
    		s=0;
    		while(i<counter){
    			cout << "client's name:  " << names[i]<<endl;
    			cout << "ID#:  " << idnumbers[s]<<endl;
    			i++;
    			s++;
    		}
    		
    }
    
    
    
    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,clientIDNumber);
    }
    
    if ( number == 2){
    	 Club.printClient();
    }
    
    }
    
    }
    Last edited by aama100; 02-20-2008 at 07:54 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    if (counter2<15){
    		idnumbers[counter]=clientIDNumber;
    		counter++;
    	}
    I'm sure you have a reason for using two completely separate counters, although I wonder what it is.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Quote Originally Posted by tabstop View Post
    Code:
    if (counter2<15){
    		idnumbers[counter]=clientIDNumber;
    		counter++;
    	}
    I'm sure you have a reason for using two completely separate counters, although I wonder what it is.


    I fixed that and I updated the code up there but it still has weird characters appear instead of ID#s in the output

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    What kind of name are you inputting?

    If there are any spaces in the name (like John Doe) it won't work. The cin >> clientName; code will only read in a word and stop at any space, tab or newline. Then when you try to read cin >>clientIDNumber; it will fail because the second part of the name will be sitting there instead of a number.

    The solution is to use getline to read in the name. This will cause other small problems in your program, though. The most important problem will be that the calls to cin >> for the numbers will leave a newline in the stream that will be picked up by getline. To fix that you can simply add cin.ignore(); after and call to cin >> whatever;.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unicode 2 byte wide characters
    By davo666 in forum C Programming
    Replies: 18
    Last Post: 02-22-2009, 04:11 AM
  2. Junk Characters
    By johnchain in forum C Programming
    Replies: 10
    Last Post: 10-12-2005, 04:52 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 09:12 AM

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