Syntax/Hints/Help

  • Don't confuse testing for equality with assigning a value
            
        == vs =
    
        == tests for equality
        = assigns (from right to left)
    
    
        including a = in an if is usually a mistake
             if (foo = bar)     mistake        
             if (foo == bar)      okay
    


  • The % operator ONLY works with type int

  • constructors do NOT have a return type
        void Fraction::Fraction() {
        ^^^^
        wrong
    
    
        }
    
        it should be
    
        Fraction::Fraction() {
    
    
        }
    


  • ***** ALL method and function calls must have a pair of ()

  • Don't redeclare a field from a class in your methods
        class Fraction {
        private:
            int num, den;
    
        public:
            void display();
            Fraction();
        };
    
    
        Fraction::Fraction() {
            int num, den;
            ^^^
            wrong
        }
    
        void Fraction::display() {
            int num, den;
            ^^^
            wrong    
        }
    


  • if you are unsure how to use #ifndef, #define, and #endif when creating a .h file look at any of the provided .h files for an example
    #ifndef _classname   // _classname is typically indicative of the class in the .h file
    #define _classname
    
    // the class declaration
    class Fraction {
    
        etc.
        
    };    <-- don't forget the semi-colon here
    
    #endif   <-- don't forget the #endif at the very end
    


  • to define a constant:
    const int MAX  = 20;
    


  • to open the file before reading
        SafeOpenForReading(infile, "File to run with? ");
    
     to read from the file that was just opened:  
     
        infile >> some_name;
    
     to test if the last read from the file failed
    
        if (infile.fail() == true)
    


  • to display to the screen use:
        cout << ....;
    


  • to read in a value from the keyboard
        cin >> ...;
    
  • syntax of an if/elseif/else
    if (test) {
        stmt;
        stmt;
        ...
    } if (test) {
        stmt;
        stmt;
        ...
    } else {
        stmt;
        stmt;
        ...
    }
    


  • syntax of a switch
    switch (test) {
        case i1:
                stmt;
                stmt;
                ...
                break;
        case i2:    
                stmt;
                stmt;
                break;
        .
        .
        .
        case n:
                stmt;
                stmt;
                ...
                break;
        default: 
    }
    


  • syntax of loops
    while ( test )  
    {
        stmt;
        stmt;
        ...
    }
    
    while ( test )  
    {
        stmt;
        stmt;
        ...
    }
    for (init; test; update) 
    {
        stmt;
        stmt;
        ...
    }
    


  • Sample Function Prototypes
        void    fname1(int &number, int count);
        
        // this function will require a return in it
        double    fname3(char letter, double value);            
    


  • Sample function prototype with an ifstream parameter it MUST be a reference parameter
        void    read(ifstream & infile)
    


  • Sample declarations and function Calls (these match the prototypes shown above)
        char letter;
        double value;
        int number, count;
        
    // sample function calls
                        
        fname1(number,count);    
    
        double answer ;
        answer = fname3(letter,value);
    
        read(infile);    
    


  • Sample Function Implementations
        void fname1(int number, int count) {
            
            stmt;
            stmt;
            ...
         }
    
        double    fname3(char letter, double value)
        {
            
            stmt;
            stmt;
            ...
            return (...);
        }
    
    
  • Vectors
    • Declaring a Vector of floats
          Vector <float> scores;
          
          Vector <float> scores(5);        
      


    • Vector methods
          Set_Used()
          e.g.
              scores.Set_Used(a value);
          
              
          Used()
          e.g.
              scores.Used(5);
          
              
          Append(a value)
          e.g. 
              scores.Append(26.5);
              
          
          Fill_With(a value)
          e.g. 
              scores.Fill_With(4.1);
      
      
          [a slot number]
          e.g.
              scores[3]        
      




  • Matrices
    • Declaring a Matrix of floats
          Matrix <float> mat;
      


    • Matrix methods
          Set_Used(row size, col size)
          e.g.
              mat.Set_Size(4, 5)
          
              
          Set_Size_From_Used()
          e.g.
              mat.Set_Size_From_Used();
          
                      
          Row_Used()
          e.g.
              mat.Row_Used();
          
              
          Col_Used()
          e.g.
              mat.Col_Used();
                      
              
          [a row number][a col number]
          e.g.
              mat[1][3]