Thread: need a clue

  1. #1
    srfrrgrrl
    Guest

    need a clue

    hi everybody...
    i want to test to see if an input string
    is part of an array of srtings
    for example
    if I declare a string of 8 months:
    Code:
    const char *months[8] = {"January", "February", "March", "April", "May", "June", "July", "August"};
    how do i test if a cin'd month name is part of that set
    (so i can have a trap if invalid data is entered)
    I have tried many combination of string compare and am not having any luck
    thanks
    surfer grrl

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A simple for-loop to loop through the months and then compare it to the input:
    Code:
    const int NrOfMonths = 12;
    const char* Month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    
    bool CheckMonth(char* MonthToCheckWith)
    {
       for(int i=0; i<NrOfMonths; i++)
       {
          if(strcmp(Month[i], MonthToCheckWith) == 0) return true;
       }
       return false;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    srfrrgrrl
    Guest

    can't

    sorry i have tried EVERY imaginable combo of that am am still getting;
    Code:
    C:\Program Files\Microsoft Visual Studio\MyProjects_M\Sem_2\4_1\4_1.cpp(125) : error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    here is the entire program ...if you can:PLEASE help!!
    Code:
    # include <iomanip>
    #include <string>
    # include <fstream>
    # include <iostream>
    using namespace std; 
    
    class Month{
    public:
     void Month::Load(Month[]);
     int Month::calcdays(const Month[], int);
     int Month::calcdays(const Month[], char*);
     void Month:: printResult(const Month[],int, int); 
    
    
     int MonthNumber;
     char MonthName[10];
     char MonthAbrv[5];
     int MonthDays;
     }; 
    
    char month[12][10] = { "January", "February", "March", "April", "May", "June", "July", "August"};
    const MAXMONTHS = 12; 
    
    //extern file with Month load data
    ifstream infile("C:\\CODE\\INF\\3abc\\IF3abc.TXT"); 
    
    int main ()
    { 
    
    Month AM[MAXMONTHS];
    int nm = 0;
    int total = 0;
    char month[10]; 
    
    if (!infile) 
    {
    cerr << "Cannot open input file" << endl;
    }//if
     else 
     { 
    
     AM[MAXMONTHS].Load(AM); 
    
      cout << "\n\n" << setw(55) << "Year-To-Date Number of Days Calculation" << endl; 
    
     for (int i = 0; i< MAXMONTHS; i++)
     {
      cout << "\n\n" << setw(55) << "Enter Number of Month (i.e. Dec = 12): ";
      cin >> nm;
      
      total = AM[MAXMONTHS].calcdays(AM, nm);
      if (total ==0)
      {}//don't print result if entry is invalid
      else
      {AM[MAXMONTHS].printResult(AM,nm, total);}
      
      
      cout << "\n\n" << setw(61) << "Enter Name of Month (capitalize 1st letter): ";
      cin >> month;
     
      total = AM[MAXMONTHS].calcdays(AM, month);
     
    
      AM[MAXMONTHS].printResult(AM, nm, total);
      
    
     }//for 
    
    }//else 
    
    return 0; 
    
    }//main 
    
      
    
    //data load-----------------------------
    void Month::Load(Month AM[])
    //in:AM; out:none
    { 
     for (int n = 0; n < 12; n++) {
     infile >> AM[n].MonthNumber;
     infile >>(AM[n].MonthName); 
     infile >>(AM[n].MonthAbrv);
     infile >>(AM[n].MonthDays); 
    
     }//for 
    }//fx load 
    
    
    //calculate days 1-----------------------
    int Month::calcdays(const Month AM[], int nm)
    //in:AM, nm; out:none
    {
     int total=0;
     int n = 0;
    if (nm < 1 || nm > 12) 
      {
       cout << "\n\n" << setw(33) << "<<Invalid Entry>>";
       return 0;
       
      }//if
      else
      {
       while (AM[n].MonthNumber <= nm) 
       {
       total += AM[n++].MonthDays;
       }//while 
    
       return total;
      }//else 
    
    }//fx calcdays 1 
    
    //calculate days 2--------------------------
    int Month::calcdays(const Month AM[], char *month)
    //in:AM, month; out:none
    {
    
    
    int total=0; 
    for (int i =0; i < MAXMONTHS; i++)
    {
    	
        if ( strcmp(month[i], *month) == 0 ) 
    	{
    		cout << "\n\n" << setw(33) << "<<Invalid Entry>>";
    	return 0;
    	}
    
     for (int n = 0; n < 12; n++) 
     {
     total += AM[n].MonthDays;
     if ( strcmp(month,AM[n].MonthName ) == 0)
     break;
     }//for
    
    
     return total; 
    }
    }//fx calcdays 2 
    
      
    
    void Month:: printResult(const Month AM[], int nm, int total) 
    //in:AM, nm; out:none
    { 
    
       cout << "\n\n" << setw(63) << "The total days in the year up to and including "
      <<AM[nm-1].MonthName<< " = " 
      <<total<<endl; 
    
    }//fx PrintResult

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if (strcmp(month[i], *month) == 0)
    What exactly are you trying to do on this line?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    srfrrgrrl
    Guest

    compare

    see if the input month is one of the valid months of the yr - if not return 0 or say "entry is invalid"

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Your code
    Code:
    if (strcmp(month[i], *month) == 0)
    Magos code
    Code:
    if(strcmp(Month[i], MonthToCheckWith) == 0)

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Don't dereferense the month's name. Doing so will get you the first character, and won't be a valid input to the strcmp function.
    Only pass the pointer...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Any ideas on why JANUARY is not found in the program below? If you add some other string as the first element in the set, JANUARY is found, but the new string is not.

    Code:
    #pragma warning(disable:4786)
    
    #include <set>
    #include <iostream>
    using namespace std;
    
    template <typename T>
    class mycomp {
    	public:
        bool operator()(T s1, T s2) const
        {
    			return strcmp(s1, s2) == 0; 
    }
    };
    
    
    int main()
    {
    	typedef set<const char*, mycomp<const char*> > MonthSet;
    	
    	MonthSet months;
    
    	// months.insert("HUH?!");
    	months.insert("JANUARY");
    	months.insert("FEBRUARY");
    	months.insert("MARCH");
    	months.insert("APRIL");
    	months.insert("MAY");
    	months.insert("JUNE");
    	months.insert("JULY");
    	months.insert("AUGUST");
    	months.insert("SEPTEMBER");
    	months.insert("OCTOBER");
    	months.insert("NOVEMBER");
    	months.insert("DECEMBER");
    
    	char input[64];
    	
    	cout<<"Enter month: ";
    	cin>>input;
    
    	MonthSet::iterator i = months.find(input);
    	if ( i != months.end() ) {
    		cout<<endl<<"Found!"<<endl;
    	}
    
    	return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The iterator starts at 1 instead of 0 ?
    I've never used iterators myself, I prefer good ol' loops where you know what is happening .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I reject the idea that something isn't good because you don't understand it. And I agree with the STL concept that explicit loops should be avoided in favor of algorithms when possible.

    And the documentation seems to indicate that it will start searching from the beginning of the sequence, until a match is found:

    set::find
    const_iterator find(const Key& key) const;
    The member function returns an iterator that designates the earliest element in the controlled sequence whose sort key equals key. If no such element exists, the iterator equals end().


    I personally think this is a bug in Visual C++ 6. But I was wondering if anyone else knows for sure that the example is incorrect, or if someone can verify that the example works, with say, g++ or something. Thanks.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I'm a blockhead. The comparison function was wrong. Supposed to be a less-than function.

    Code:
    template <typename T>
    class mycomp {
    	public:
    	bool operator()(T s1, T s2) const
    	{
    	        return strcmp(s1, s2) < 0; 
    	}
    };
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Replies: 8
    Last Post: 04-19-2006, 09:07 AM
  3. No clue what the problem is...
    By FingerPrint in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2006, 03:16 PM
  4. A really weird problem. I understand the error, but not a clue why I get the errors.
    By Finchie_88 in forum Networking/Device Communication
    Replies: 1
    Last Post: 02-20-2005, 09:48 AM
  5. No CLUE what to do...
    By stewade in forum C Programming
    Replies: 7
    Last Post: 05-03-2004, 12:02 AM