Thread: I know I am missing something simple plz help

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    I know I am missing something simple plz help

    Here is what I have. I am trying to add days to the days I already have. Not sure how to set it up. I want to enter a number and it add to tell what the day will be how would I set that up here is what I have:

    Code:
    #include <iostream>  
    
     using namespace std;  
    
     const char* dayNames[] =  
     {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};  
    
     class Day  
     {  
         public:  
    
             Day() { dayNumber = 0; }  
             const char* getDay() { return dayNames[dayNumber]; }  
             const char* getNextDay() { return dayNames[(dayNumber+1) % 7]; }  
             const char* getPrevDay() { return dayNames[(dayNumber+7-1) % 7]; }  
            const char* getDayPlus( int n ) { return dayNames[(dayNumber+n) % 7]; }  
                
             void setDay( int n ) { dayNumber = n; normalize(); }  
             void printDay() { cout << dayNames[dayNumber]; }  
    
         private:  
             int dayNumber;  
             void normalize() { dayNumber = dayNumber % 7; }  
     };  
    
     int main()  
     {  
         Day d;  
         cout << "The (default) day is " << d.getDay() <<endl  
              << "The next day is " << d.getNextDay() <<endl 
    		  << "How many days do you want to add?" <<endl
              << "The day plus is " << d.getDayPlus() <<endl  
              << "The prev day is " << d.getPrevDay() <<endl;  
    
         d.setDay(6);  
         cout << "After 'd.setDay(6)' ... the day now is " << d.getDay() <<endl  
              << "The next day is " << d.getNextDay() <<endl  
              << "The day plus is " << d.getDayPlus() <<endl  
              << "The prev day is " << d.getPrevDay() <<endl;  
    
         d.setDay(3);  
         cout << "After 'd.setDay(3)' ... the day now is " << d.getDay() <<endl  
              << "The next day is " << d.getNextDay() <<endl  
              << "The day plus is " << d.getDayPlus() <<endl  
              << "The prev day is " << d.getPrevDay() <<endl;  
    
          
         d.printDay();  
         cout << "\nPress 'Enter' to continue ... " << flush;  
         cin.get();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Apart from getDayPlus expecting a parameter (how is this even compiling?), what output do you see, and how exactly is it wrong?
    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.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    It isn't wrong, but the problem I have is where I inserted the << "How many days do you want to add?" <<endl I want to let the user insert. I know it is something like cin >> but after that I forget. Then to call it when it says The day plus. I can put the intiger in and it will figure and guive me the correct answer but only if you put it in the code I want the user to insert a number.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yes....
    After that statement do
    cin>>days;
    But must declare the variable days as integer before getting input....

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Add a variable to your class " int numOfDays; "

    Then use this with the cin call as suggested

    Code:
    cin >> d.numOfDays;
    I would consider a slightly more descriptive name for the object itself by the way..
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iamnewtothis View Post
    It isn't wrong...
    It is wrong. This code will not compile.
    Either you have a very broken compiler or this code is not your real code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by iamnewtothis View Post
    It isn't wrong, but the problem I have is where I inserted the << "How many days do you want to add?" <<endl I want to let the user insert. I know it is something like cin >> but after that I forget. Then to call it when it says The day plus. I can put the intiger in and it will figure and guive me the correct answer but only if you put it in the code I want the user to insert a number.
    You declare classes, but you forgot how to get user input?

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Ok so I decided to go different. I have it to where it will work except the minus days. I can't get the days to show up.

    Code:
    #include <iostream>  
    
     using namespace std;  
    
     const char* dayNames[] =  
     {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};  
    
     class Day  
     {  
         public:  
    
    		 Day() { dayNumber = 1; }  
             const char* getDay() { return dayNames[dayNumber]; }  
             const char* getNextDay() { return dayNames[(dayNumber+1) % 7]; }  
             const char* getPrevDay() { return dayNames[(dayNumber+7-1) % 7]; }  
             const char* getDayPlus( int n ) { return dayNames[(dayNumber+n) % 7]; }
             const char* getDayMinus( int n ) { return dayNames[(dayNumber-n) % 7]; }
                
             void setDay( int n ) { dayNumber = n; normalize(); }  
             void printDay() { cout << dayNames[dayNumber]; }  
    
         private:  
             
    		 int dayNumber;  
             void normalize() { dayNumber = dayNumber % 7; }  
     };  
    
     int main()  
     {  
         Day d;  
         cout << "The (default) day is " << d.getDay() <<endl  
              << "The next day is " << d.getNextDay() <<endl   
              << "The prev day is " << d.getPrevDay() <<endl
    		  << "The day + 3 is " << d.getDayPlus(3) <<endl
    		  << "The day - 3 is " << d.getDayMinus(3) <<endl;
    
    cout << "\nPress 'Enter' to continue ... " << flush; 
    
    return 0;
     }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your formula is wrong. If DayNumber = 1, then the formula becomes
    (1 - 3) % 7
    -2 % 7 = -2
    (Undefined behavior!)

    The proper logic should be:
    if DayNumber - n > 0 then return DayNumber - n;
    else if DayNumber - n < 0 then return 7 + DayNumber - n;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 07-14-2009, 08:16 AM
  2. plz help me with simple string comparison.
    By MegaManZZ in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2008, 01:11 PM
  3. Replies: 16
    Last Post: 01-10-2008, 11:38 PM
  4. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM