Thread: programming with class

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    programming with class

    could some one please help me by telling me what is wrong with this program. all i want to do is to display all the asci characters 1-255 using classes because im just learning how to use them

    Code:
    #include <iostream.h>
    
    class hoha
    {
    public:
    	char current_asci_value=0;
    	int index_number =0;
    
    	int displayascivalues(current_asci_value, index_number)
    	{
    		while(current_asci_value < 255)
    		{
    			index_number++;
    			cout<<current_asci_value;
    		}
    	}
    };
    
    int main()
    {
    	hoha h;
    
    	h.displayascivalues();
    	return 0;
    }
    Last edited by c++.prog.newbie; 02-12-2002 at 05:40 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You never increment current_asci_value. So you have an infinite loop of the method printing the character equivilent of 0.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    the ascii variable doesnt change at all and you probably don't need the index variable. maybe....

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    thanx for pointing that out bet theres another error that stops it from even creating the .exe file

    pure specifier can only be specified for functions

    it says this for both the public variables

    index_number
    and
    current_asci_value

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You need to assign the value of your ascii char variable inside a constructor or in the main function. When you try to assign it to 0 in the class definition the compiler will think you are trying to declare a pure virtual function. You also need to declare the variable to be int and cast it or you'll get an infinite loop.
    Code:
    #include <iostream.h>
    
    class hoha
    {
      public:
        int current_asci_value;
        hoha()
        {
          current_asci_value = 0;
        }
        void displayascivalues()
        {
          while(current_asci_value < 255) {
            cout<<(char)current_asci_value<<" ";
            current_asci_value++;
          }
        }
    };
    
    int main()
    {
      hoha h;
      h.displayascivalues();
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok i incremented current_asci_value in the code and i moved
    were it said
    Code:
    ...
    private:
             char current_asci_value =0;
             int index_number           =0;
    ...
    and i changed it to
    Code:
    ...
    private:
              char current_asci_value;
              int index_value;
    ...
    int displayascivalue()
           current_asci_value=0;
           index_value           =0;
    while(...){
            index_value++;
            current_asci_value++;
          
    ...
    but now it works but keeps going on and on never stoping

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    you posted yoiur post just abit faster than me

    but what makes it loop forever if you dont cast it
    and is it nessicery(cant spell) to set the value to 0 in a function

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but what makes it loop forever if you dont cast it
    That's one option, the second option is to declare the variable as unsigned char. What is happening is your char wraps around into an infinite loop, it never gets to 254. A signed char only goes to 127 and then wraps around to -128. So, if you declare the variable as int and cast it as a char when you print it you can be sure that it will reach 254. The same goes with an unsigned char which has a range of 0 - 256, it's you're choice which you use.

    >and is it nessicery(cant spell) to set the value to 0 in a function
    Yes, if you want to set the value then you must either do it in a specific method or the constructor. Another alternative is to pass the value directly to your display method and forgo the class data member.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Unregistered
    Guest
    how would you cast an unsigned char or could you do it like this

    unsigned char current_asci_value;

    then later on in the class have a function that says

    unsigned char current_asci_value = 0;

    or would i have to do it like this

    cout<< <unsigned char>current_asci_value;
    and still initialize it to int

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM