Thread: using vector is not working like the examples I am using.

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    using vector is not working like the examples I am using.

    the original problem is using two arrays type char then string, I am getting run off into the other array when referencing it by int, if the number goes over the size of the array what is before the other one it will slip into the next array and return its value. if the number is out of sync of that it blows up with a seg fault, I have no idea why.

    now comes this problem, in trying to use something else to remove that first problem, I came across the vector thing in C++ ( and yes I do not know C++)
    but using the examples in how to set up and use vector I am getting

    examples I am using before I forget to put them in,
    the last example on this page:
    Array of Strings in C++ (3 Different Ways to Create) - GeeksforGeeks

    and on this page
    C++ Tutorial: A Beginner's Guide to std::vector, Part 1


    Code:
    date.cpp:55:1: error: ‘the_month’ does not name a type
     the_month.push_back("dec");
    for everything within the two arrays. I am getting error(s) like that.

    here is my code:
    Code:
    #ifndef DATE_H
    #define DATE_H
    
    class TheDate 
    {
    private:
        int year;
        int month;
        int day;
    
        void validate_dates(int dd, int mm, int yy);
       
    public:
        TheDate(int dd = 0, int mm = 0, int yy = 0000)
        {
           validate_dates(dd,mm,yy);
        }
        void setDay(int dd);
        int getDay();
        void setMonth(int mm);
        int getMonth();
        void setYear(int yy);
        int getYear();
        void get_Day_of_week(int day);
        int get_is_leap_year(int year);
        void get_what_month(int month);
    
    
    };
    
    #endif
    cpp
    Code:
    #include <iostream>
    #include <vector>
    #include "date.h"
    
    //using std::string;
    using namespace std;
    
    /*
    const char* day_of_week[] = {
        "Monday", "Tuesday", "Wensday", "Thursday", "Firday", "Saturday", "Sunday"
    };
    
    const char* The_Month[] = {
        "January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"
    };
    
    
    const string day_of_week[] = {
        "Monday", "Tuesday", "Wensday", "Thursday", "Firday", "Saturday", "Sunday"
    };
    
    const string The_Month[] = {
        "January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"
    };
    */
    
    
    
    std::vector<string> the_day;
    the_day.reserve(7);
    the_day.push_back("monday");
    the_day.push_back("tues");
    the_day.push_back("wens");
    the_day.push_back("thurs");
    the_day.push_back("fri");
    the_day.push_back("sat");
    the_day.push_back("sun");
    
    
    std::vector<string> the_month;
    the_month.reserve(12);
    the_month.push_back("Jan");
    the_month.push_back("feb");
    the_month.push_back("mar");
    the_month.push_back("apr");
    the_month.push_back("may");
    the_month.push_back("jun");
    the_month.push_back("july");
    the_month.push_back("aug");
    the_month.push_back("sep");
    the_month.push_back("oct");
    the_month.push_back("nov");
    the_month.push_back("dec");
    
    
    void TheDate::setDay(int dd)
    {
        day = dd;
    }
    int TheDate::getDay()
    {
        return day;
    }
    void TheDate::setMonth(int mm)
    {
       month = mm;
    }
    int TheDate::getMonth()
    {
        return TheDate::month;
    }
    void TheDate::setYear(int yy)
    {
        TheDate::year = yy;
    }
    int TheDate::getYear()
    {
        return TheDate::year;
    }
    void TheDate::get_Day_of_week(int day)
    {
        using std::cout;
        using std::endl;
        std::cout << " in get day of week day : " << day <<std::endl;
        std::cout  << the_day[day - 1] << std::endl;
    }
    int TheDate::get_is_leap_year(int year)
    {
        using std::cout;
        using std::endl;
        //set return values for bool.
        if (year % 4 == 0)
        {
            if (year % 100 == 0)
            {
                if (year % 400 == 0)
                {
                    std::cout << year << " is a leap year." << '\n';
                    return 1;
                }
                else
                {
                    std::cout << year << " is not a leap year." << '\n';
                    return 0;
                }
            }
            else
            {
                std::cout << year << " is a leap year." << '\n';
                return 1;
            }
        }
        else
        {
            std::cout << year << " is not a leap year." << '\n';
            return 0;
        }
        // because it has to return something.
        // well it should not even reach this actually. 
        return year;
    }
    void TheDate::get_what_month(int month)
    {
        using std::cout;
        using std::endl;
        
        std::cout << the_month[month - 1] << endl;
    }
    void TheDate::validate_dates(int dd, int mm, int yy)
    {
      int leap;
      using std::cout;
      using std::endl;
    
      std::cout << "in validate dates" << '\n';
      // make sure proper year other wise it all off anyways, or just cuz. ;) 
      if (yy <= 2000)
      {
        setYear(2001);
      }
      else
      {
        setYear(yy);
      }
     // Now find out if year is leap year or not.
     // ensures proper year is being checked.
     // by passing in getYear.
     leap = get_is_leap_year(getYear());
     
     //if true check for month is feb.
     // if it is a leap year and feb check day
     if (leap == 1 && mm == 2)
     {
         if (dd <= 0 || dd > 29)
         {
            setDay(1);
         }
         else
         {
            setDay(dd);
         }
         
     }
     else 
     { 
         std::cout << " month is " << mm << std::endl;
        //check for which months have same amount of ending days grouping case.
        switch(mm)
        {  
            case 2: // feb
                if (dd <= 0 || dd > 28)
                {
                    setDay(1); 
                    setMonth(mm);
                }
                else
                {
                    setDay(dd); 
                    setMonth(mm);
                }
                break;
            //31 days
            case 1: //jan
            case 3: //march
            case 5: //may
            case 7: //july
            case 8: //augsut
            case 10: //Oct
            case 12: //dec
            if ( dd <= 0 || dd > 31)
            {
                setDay(1);
                setMonth(mm);
                std::cout << " in 31 days months " << getDay() <<std::endl;
            }
            else
            {
              setDay(dd);
              setMonth(mm);
                std::cout << " dd = dd in 31 days day " << getDay() <<std::endl;
                std::cout << dd << " day " << mm << " month " << std::endl;
                get_Day_of_week(getDay() ); 
                get_what_month(getMonth());
            }
            break;
            //look for 30 days
            case 4: // april 
            case 6: //june
            case 9: //sept
            if ( dd <= 0 || dd > 30)
            {
                setDay(1);
                setMonth(mm);
            }
            else
            {
                setDay(dd);
                setMonth(mm);
            }
           
            break;
            default:
                // if it gets here then the month has to then fall out of specs
                // then day too is also invalid by default,
                //so just set them both to a default day, month.             
                    setMonth(1);
                    setDay(1);
                break;
        } // end switch
     } // end else if
    
    }
    main
    Code:
    #include <iostream>
    #include "date.h"
    
    
    
    int main()
    {
        using namespace std;
        int d,m,y;
    
        std::cout << "enter day, month, year in number form" << std::endl;
        std::cin >> d >> m >> y ;
    
        TheDate trying(d,m,y);
    
        /*
        trying.setDay(d);
        trying.setMonth(m);
        trying.setYear(y);
        */
        std::cout << '\n' << " in main " << '\n';
        std::cout << '\n' << "day " << trying.getDay() << " month " << trying.getMonth() << " year " << trying.getYear() << "\n";
        std::cout << std::endl;
        trying.get_Day_of_week(trying.getDay());
        trying.get_what_month(trying.getMonth());
        trying.get_is_leap_year(trying.getYear());
      
        std::cout << '\n' << "day " << trying.getDay() << " month " << trying.getMonth() << " year " << trying.getYear() << "\n";
        
        return 0;
    }
    output when it worked, kind of
    Code:
    userx@/media/data/C-Projects/VSC/C++/class-date <> g++ -Wall date.cpp main.cpp
    userx@/media/data/C-Projects/VSC/C++/class-date <> ./a.out
    enter day, month, year in number form
    12 3 2012
    in validate dates
    2012 is a leap year.
     month is 3
     dd = dd in 31 days day 12
    12 day 3 month // day is 12 no 12 for day array so it goes to month array and 
    April // <-- returns this instead
    March
    
     in main
    
    day 12 month 3 year 2012
    April
    
    day 12 month 3 year 2012
    userx@/media/data/C-Projects/VSC/C++/class-date <>
    if you look at the code and count the elements in day array then go to month array math says its jumping into that one to complete the job.

    now with vectors it is just not working at all.
    Code:
    userx@/media/data/C-Projects/VSC/C++/class-date <> g++ -Wall date.cpp main.cpp
    date.cpp:32:1: error: ‘the_day’ does not name a type
     the_day.reserve(7);
     ^
    date.cpp:33:1: error: ‘the_day’ does not name a type
     the_day.push_back("monday");
     ^
    .....
    
    date.cpp:43:1: error: ‘the_month’ does not name a type
     the_month.reserve(12);
     ^
    date.cpp:44:1: error: ‘the_month’ does not name a type
     the_month.push_back("Jan");
     ^
    .....
    but per example I have done what is needed, but I guess not due to the errors.

    so now what?

    PS yes I swiped this off someone else post in here trying to help him out then seen what I was getting, now my psyche is just not letting me let go of this until I figure out how to get it to work like it is suppose to.
    Last edited by userxbw; 09-28-2017 at 11:16 AM.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861

    Update:

    I've got it to almost work like this, not that it explains why it is not working the other way,

    Code:
    #include <iostream>
    #include <vector>
    #include "date.h"
    
    //using std::string;
    using namespace std;
    
    
    
    const string day_of_week[] = {
        "Monday", "Tuesday", "Wensday", "Thursday", "Firday", "Saturday", "Sunday"
    };
    
    const string The_Month[] = {
        "January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"
    };
    
    
    
    
     // the iterator constructor can also be used to construct from arrays:
     
     std::vector<string> day (day_of_week, day_of_week + sizeof(day_of_week) / sizeof(string) );
      
     std::vector<string> month (The_Month, The_Month + sizeof(The_Month) / sizeof(string) );
    
    // I am not sure if that last part should be type string in sizeof(string)
    // I just replaced the example showing sizeof(int) with string.
    
    void TheDate::setDay(int dd)
    {
        day = dd;
    }
    int TheDate::getDay()
    {
        return day;
    }
    void TheDate::setMonth(int mm)
    {
       month = mm;
    }
    int TheDate::getMonth()
    {
        return TheDate::month;
    }
    void TheDate::setYear(int yy)
    {
        TheDate::year = yy;
    }
    int TheDate::getYear()
    {
        return TheDate::year;
    }
    void TheDate::get_Day_of_week(int day)
    {
        using std::cout;
        using std::endl;
        day = (day -1);
        std::cout << " in get day of week day : " << day <<std::endl;
        std::cout  << day[day] << std::endl;
    }
    int TheDate::get_is_leap_year(int year)
    {
        using std::cout;
        using std::endl;
        //set return values for bool.
        if (year % 4 == 0)
        {
            if (year % 100 == 0)
            {
                if (year % 400 == 0)
                {
                    std::cout << year << " is a leap year." << '\n';
                    return 1;
                }
                else
                {
                    std::cout << year << " is not a leap year." << '\n';
                    return 0;
                }
            }
            else
            {
                std::cout << year << " is a leap year." << '\n';
                return 1;
            }
        }
        else
        {
            std::cout << year << " is not a leap year." << '\n';
            return 0;
        }
        // because it has to return something.
        // well it should not even reach this actually. 
        return year;
    }
    void TheDate::get_what_month(int T_month)
    {
        using std::cout;
        using std::endl;
        
        std::cout << month[T_month - 1] << endl;
    }
    void TheDate::validate_dates(int dd, int mm, int yy)
    {
      int leap;
      using std::cout;
      using std::endl;
    
      std::cout << "in validate dates" << '\n';
      // make sure proper year other wise it all off anyways, or just cuz. ;) 
      if (yy <= 2000)
      {
        setYear(2001);
      }
      else
      {
        setYear(yy);
      }
     // Now find out if year is leap year or not.
     // ensures proper year is being checked.
     // by passing in getYear.
     leap = get_is_leap_year(getYear());
     
     //if true check for month is feb.
     // if it is a leap year and feb check day
     if (leap == 1 && mm == 2)
     {
         if (dd <= 0 || dd > 29)
         {
            setDay(1);
         }
         else
         {
            setDay(dd);
         }
         
     }
     else 
     { 
         std::cout << " month is " << mm << std::endl;
        //check for which months have same amount of ending days grouping case.
        switch(mm)
        {  
            case 2: // feb
                if (dd <= 0 || dd > 28)
                {
                    setDay(1); 
                    setMonth(mm);
                }
                else
                {
                    setDay(dd); 
                    setMonth(mm);
                }
                break;
            //31 days
            case 1: //jan
            case 3: //march
            case 5: //may
            case 7: //july
            case 8: //augsut
            case 10: //Oct
            case 12: //dec
            if ( dd <= 0 || dd > 31)
            {
                setDay(1);
                setMonth(mm);
                std::cout << " in 31 days months " << getDay() <<std::endl;
            }
            else
            {
              setDay(dd);
              setMonth(mm);
                std::cout << " dd = dd in 31 days day " << getDay() <<std::endl;
                std::cout << dd << " day " << mm << " month " << std::endl;
                get_Day_of_week(getDay() ); 
                get_what_month(getMonth());
            }
            break;
            //look for 30 days
            case 4: // april 
            case 6: //june
            case 9: //sept
            if ( dd <= 0 || dd > 30)
            {
                setDay(1);
                setMonth(mm);
            }
            else
            {
                setDay(dd);
                setMonth(mm);
            }
           
            break;
            default:
                // if it gets here then the month has to then fall out of specs
                // then day too is also invalid by default,
                //so just set them both to a default day, month.             
                    setMonth(1);
                    setDay(1);
                break;
        } // end switch
     } // end else if
    
    }
    error is now trying to reference element,
    Code:
    userx@/media/data/C-Projects/VSC/C++/class-date <> g++ -Wall date.cpp main.cpp
    date.cpp: In member function ‘void TheDate::get_Day_of_week(int)’:
    date.cpp:95:30: error: invalid types ‘int[int]’ for array subscript
         std::cout  << day[day - 1] << std::endl;
                                  ^
    date.cpp: In member function ‘void TheDate::get_what_month(int)’:
    date.cpp:137:35: error: invalid types ‘int[int]’ for array subscript
         std::cout << month[T_month - 1] << endl;
                                       ^
    userx@/media/data/C-Projects/VSC/C++/class-date <> g++ -Wall date.cpp main.cpp
    date.cpp: In member function ‘void TheDate::get_Day_of_week(int)’:
    date.cpp:96:26: error: invalid types ‘int[int]’ for array subscript
         std::cout  << day[day] << std::endl;
                              ^
    date.cpp: In member function ‘void TheDate::get_what_month(int)’:
    date.cpp:138:35: error: invalid types ‘int[int]’ for array subscript
         std::cout << month[T_month - 1] << endl;
                                       ^
    userx@/media/data/C-Projects/VSC/C++/class-date <>

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why not just initialize the vector with the days like you did with the array?
    Code:
      
        std::vector<std::string> day { "Monday", "Tuesday", "Wensday", "Thursday", 
                                                      "Firday", "Saturday", "Sunday"};
    Then you don't even need the array.

    Code:
    void TheDate::get_Day_of_week(int day)
    {
        using std::cout;
        using std::endl;
        day = (day -1);
        std::cout << " in get day of week day : " << day <<std::endl;
        std::cout  << day[day] << std::endl;
    }
    Why are you using the using statements? Your properly scoping cout and endl so there is no need for the using statements.

    What is the purpose of this function? Do you know that you can't have multiple variables with the same name? Remember you have a class member variable named day so you need to change the name of the parameter so it doesn't conflict with the member variable. By the way why are you even passing a parameter into this function? It should probably be using the class member variable and returning something to the calling function instead of just trying to print some value.

    You're also need to decide on what type of variable day really is. It appears that the class member variable and the parameter day are both a single int variable, not an array.

    Jim

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by jimblumberg View Post
    Why not just initialize the vector with the days like you did with the array?
    Code:
      
        std::vector<std::string> day { "Monday", "Tuesday", "Wensday", "Thursday", 
                                                      "Firday", "Saturday", "Sunday"};
    Then you don't even need the array.

    Code:
    void TheDate::get_Day_of_week(int day)
    {
        using std::cout;
        using std::endl;
        day = (day -1);
        std::cout << " in get day of week day : " << day <<std::endl;
        std::cout  << day[day] << std::endl;
    }
    Why are you using the using statements? Your properly scoping cout and endl so there is no need for the using statements.

    What is the purpose of this function? Do you know that you can't have multiple variables with the same name? Remember you have a class member variable named day so you need to change the name of the parameter so it doesn't conflict with the member variable. By the way why are you even passing a parameter into this function? It should probably be using the class member variable and returning something to the calling function instead of just trying to print some value.

    You're also need to decide on what type of variable day really is. It appears that the class member variable and the parameter day are both a single int variable, not an array.

    Jim
    My first time I ever did a class.
    using statements : monkey see, monkey do.

    multiple variables with the same name,
    yes and no.
    if you look at the post , first one then second one you'll see it has chaned, and my same name is
    just testing if that holds true, because I couldn't do anything else with it at the time.

    By the way why are you even passing a parameter into this function?

    it gets the day inside the class. look at main,

    Code:
    get_Day_of_week( getDay());
    getDay() returns int day by number; that references the element within the array, to print out the name of day using the Alphabet. it is public so it can be called within main by whatever means wanted to get the day printed out alpha like using an int to represent what day it is.
    monday - sunday
    0 - 6

    You're also need to decide on what type of variable day really is.
    it is an int, look at class header.
    as far as what var name day, or day of week is, yeah that is a no brainier, one is int, the other is char, or string.
    I haven't decided yet. it was 1/2 working without vector

    you should know the name inside of the prams does not got to match the var name it is attached to.

    int day sets day by number of the day 1 thru 28,29,30, or 31 depending on month.
    look at
    Code:
    void TheDate::validate_dates(int dd, int mm, int yy)



    Code:
    std::vector<std::string> day { "Monday", "Tuesday", "Wensday", "Thursday", 
                                                  "Firday", "Saturday", "Sunday"};
    elements are referenced by what? an int, 0 through N - 1.

    and no it is not a fully functional part of the class because that day is not made so it calculates if it is a mon - Sunday beyond the fist week. but the month array is set up the same way and will work as such because months are a constant 1 thru 12 or array 0 thru 11

    so as you know jan is 1 but in an array it is 0

    I too changed it to return and not cout within that function, so either way that part works I am just trying to get it to stop going into the other array ...then return the month instead of a day, by alpha.

    now to try this suggestion of yours.

    eidt:
    oh yeah and BTW while waiting I forgot I found out this,
    Code:
    [Running] cd "/media/data/C-Projects/VSC/C++/class-date/" && g++ date.cpp -o date && "/media/data/C-Projects/VSC/C++/class-date/"date
    date.cpp:76:39: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
      std::vector<std::string> day_of_week { "Monday", "Tuesday", "Wensday", "Thursday", 
                                           ^
    date.cpp:77:32: error: in C++98 ‘day_of_week’ must be initialized by constructor, not by ‘{...}’
      "Firday", "Saturday", "Sunday"};
    I'm not sure what my gnu g++ is using and it too stated -std=c++11 or -std=gnu++11 is experimental.
    Last edited by userxbw; 09-28-2017 at 03:55 PM.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by jimblumberg View Post
    Why not just initialize the vector with the days like you did with the array?
    Code:
      
        std::vector<std::string> day { "Monday", "Tuesday", "Wensday", "Thursday", 
                                                      "Firday", "Saturday", "Sunday"};
    Jim
    Much better after I set my g++ -std=gnu++11 and got that setup right, it is doing what it is suppose to be doing. I even get the proper errors so I can figure out what's really going on.
    Code:
     in get day of week day : 22
    terminate called after throwing an instance of 'std::out_of_range'
      what():  vector::_M_range_check: __n (which is 21) >= this->size() (which is 7)
    Aborted
    no more run over into the other array. now if I get around to feeling like it I can figure out how to get it to know what day of the week it is in alpha by month year. haha hehe hoho

    as you see day is int,
    Code:
    Tuesday
    March
    2020 is a leap year.
    
    day 2 month 3 year 2020
    sting for the array

    Code:
    std::string TheDate::get_day_of_week(int day)
    {
     return ( day_of_week.at(day - 1) );
    }
    THANKS
    Last edited by userxbw; 09-28-2017 at 04:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector unique not working on char*
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2014, 10:19 AM
  2. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  3. Vector os structs not working how it is supposed to...
    By Gabriel Chamon in forum C Programming
    Replies: 5
    Last Post: 06-16-2012, 11:31 PM
  4. Vector not working!!
    By aaroroge in forum C++ Programming
    Replies: 4
    Last Post: 07-16-2005, 05:17 PM
  5. Any MFC examples on the web , please?
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-10-2002, 08:57 AM

Tags for this Thread