Thread: expected primary expression before "." token

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    4

    expected primary expression before "." token

    Hi there, I'm trying to do a program that allows user to enter a number between 1-365 and convert that to month and day (for example, 32 -> Feb. 1st). I tried compiling the program but I got "expected primary expression before '.' token" in int main(). I can't seem to figure out what I did wrong. If any of you can just point me in the right direction, that'll be awesome. Thank you so much!

    Code:
    //This program is for chapter 14 - review question 2 - Day of the year
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    class DayOfYear
    {
          private:
                 static const int daysAtEnd[];
                 static const int monthNames[];
          public:
                 
    
    void DayOfYear::print()
    {
         int month = 0;
         int day;
         
         while (daysAtEnd[month] < day)
         month = (month+1)%12;
         if (month == 0)
         cout << "January" << day;
         else
         {
             cout << DayOfYear::monthNames[month]<< " "
             <<day - DayOfYear::monthNames[month-1];
         }
    }
    
    void DayOfYear::setNumber(int n)
    {
         int number;
         if (n >= 0 && n <= 365)
         number=n;
         else
         {
             cout << n << "is not a valid"
             << "value for the month.\n";
             exit (EXIT_FAILURE);
         }
    }
    
    };
    
    int daysAtEnd[12] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
    char monthNames[] = "January, February, March, April, May, June, July, August, September, October, November, December";
       
    int main()
    { 
        
    DayOfYear.print(); <--- expected primary expression before "." token
    
    
    system("PAUSE");
    return 0;
    }
    thanks!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    At a glance, you need a DayOfYear object. Beyond that, you might want to switch books, perhaps based on the recommendations in What is the best book for a beginner to learn c++, if that code was from a beginners' book. It appears to be teaching the use of classes where the class is used rather poorly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Kansas, USA
    Posts
    12
    Basically, the DayOfYear class is like a cookie cutter, you need to make some cookies with the cutter before you can do anything.

    Code:
    //doy is the object or the "cookie" in the above analogy.
    DayOfYear doy;
    
    // calls the print method
    doy.print();
    Last edited by aprescott_27; 07-10-2007 at 02:51 PM. Reason: Code tags

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    There are a lot of things that are done poorly in that program. You should get a good book if you want to lean C.

    What exactly do you want DayOfYear.print() to do?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503

    Talking

    Quote Originally Posted by aprescott_27 View Post
    Basically, the DayOfYear class is like a cookie cutter, you need to make some cookies with the cutter before you can do anything.
    You blew the analogy at the end. You should had said "the DayOfYear class is like a cookie cutter, you need to make some cookies with the cutter before you can eat them."

    Especially since using an object is often referred to as "consuming" them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM