Thread: Classes, what are they?

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    Classes, what are they?

    I realize the very basic idea of classes, but not how they work or how they are coded, could anyway point me to an online resource or explain classes to me themselves?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Classes, what are they?
    Classes are a means of information transfer from the teachers notebook to the students notebook without passing through the minds of either
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Me
    Join Date
    Jul 2006
    Posts
    71
    Quote Originally Posted by Salem
    > Classes, what are they?
    Classes are a means of information transfer from the teachers notebook to the students notebook without passing through the minds of either
    Aha! Thankyou, I just couldn't figure it out. I'm not sure what dave_sinkula is talking about...looks like some very complicated programming language

    Edit: I have another question, it has nothing to do with this. But I have this code and when I input something like "123" for the addmore variable it prints "Invalid Input" for however many characters there are in the variable. I am pretty sure the reason is because addmore is a char data type and it checks each character, how would you get around this?

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    int main()
    {
        vector <int> storage;
        int number;
        int cont=0;
        char addmore;
        cout<<"Enter a phone number to be stored(Example-7854439012): ";
        cin>>number;
        cin.ignore();
    
        storage.push_back(number);
    
        for(int i=0;i<1;i)
         {
          cout<<"Do you wish to add any more numbers?(y/n)";
          cin>>addmore;
          cin.ignore();
    
          if (isalpha(addmore))
           {
            cout<<"Input is a letter\n";
            break;
           }
          else
           {
            cout<<"Invalid input.\n";
           }
         }
    
        cin.get();
        return 0;
    }
    Last edited by relyt_123; 09-22-2006 at 07:12 PM.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, let's see what happens!

    When you enter "123", the characters '1', '2' and '3' are stored in a buffer -- a temporary storage thingamajig. When you cin>> into addmore, you only retrieve the first character '1', which is then processed. The loop will repeat (tip: you can use for(;;){} or while(1){} for an infinite loop) and retrieve '2' from the buffer, which is the next character. Again, it is an invalid character. This goes on until all characters are read from the buffer.
    Last edited by jafet; 09-22-2006 at 11:49 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Since valid input is 'y' or 'n', and (almost) anything the user types is a character, I would forget about isalpha and just check for 'y' or 'n' and leave everything else to the invalid input part. If you want to ignore the rest of the input after invalid input, use cin.ignore(numeric_limits<streamsize>::max(), '\n');. You need to include <limits> (and <ios> to be perfectly correct).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM