Thread: what is the error please? HELP??

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    what is the error please? HELP??

    hi can some one note out what is wrong with this code:

    Code:
    int ifEven(string str)
    {
        string array[15];
    	int i=0;
    	int k=0;
    	
        while(i<str.size()-1)
        {
            array[k]=str.substr(i,2);
    		cout<<array[k]<<" ";
    		i+=2;
    		k+=1;
    	}		 
    	for(int i = 0; array[i]; i++)
    	{
           cout<<array[i];
        }    
    	    
    }
    the error come out is:

    function `int ifEven(std::basic_string<char, std::char_traits<char>, std::allocator<char>:

    could not convert `array[i]' to `bool'

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    for(int i = 0; array[i]; i++)

    the second argument of the for statement requires a condition - so something that evaluates to true or false (thus bool).

    array[i] is a string. you probably want
    for(int i = 0; i < sizeof(array); i++)
    signature under construction

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    My guess:

    Code:
    for(int i = 0; array[i]; i++)
    This is supposed to be your condition to exit the loop. It's supposed to be boolean. It's an integer, and you'd end up crashing your program when it goes out of the bounds of it's size.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    do you mean like this?

    Code:
    int ifEven(string str)
    {
        string array[15];
        char something[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	int i=0;
    	int k=0;
    	
        while(i<str.size()-1)
        {
            array[k]=str.substr(i,2);
    		cout<<array[k]<<" ";
    		i+=2;
    		k+=1;
    	} 
    	for(int i = 0; i < sizeof(array); i++)
    	{
           cout<<array[i];
        }    
    
    }

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    this code crash the program...

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    int ifEven(string str)
    {
        string array[15];
        char something[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	int i=0;
    	int k=0;
    	
        while(i<str.size()-1)
        {
            array[k]=str.substr(i,2);
    		cout<<array[k]<<" ";
    		i+=2;
    		k+=1;
    	} 
    	for(int i = 0; i < 15; i++)  // Size of the array. Don't bother using the
    	{                            // size of function.
           cout<<array[i];
        }    
    
    }
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    ooops, sizeof() returns the size of the array in bytes...
    you need to divide it by the size of one element:

    replace sizeof(array) by
    sizeof(array) / sizeof(*array)
    or simply:
    15


    edit:
    using the sizeof method allows changing the size of the array without having to change any code (except for the definition of array)
    of course sizeof won't work with dynamic memory
    signature under construction

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
      int array[10];
      
      cout << sizeof(array) << endl;
      cout << sizeof(array) / sizeof(int);
      cout << sizeof(array) / sizeof(*array); // As suggested by Raven Arkadon
                                              // This is good if you don't know your array's
                                              // datatype
      
      return 0;
    }
    
    // OUTPUT
    // 40
    // 10
    // 10
    Last edited by SlyMaelstrom; 12-09-2005 at 08:53 PM.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    gtr_s15,

    When are you going to post some code you wrote? That is the code someone wrote for you in another thread.

    You seem to think you can con people into writing your whole program for you.
    Last edited by 7stud; 12-09-2005 at 10:22 PM.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    thank you for helping but i have one error that makes me really headache...

    the problem is

    i want to change each number in string to character that i had assigned as char something[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";.
    for people who dont understand what i mean, here is an example,

    array[0]=44, array[1]=11, array[2]=00, array[3]=13, so this is the fomula tat i want to use :
    output for 44 is (44-26)+65 = 83, for character 83 = s, for 11 is 11+65 = 76 is character L, 00 is A because 00+65, and the last is 13 + 65 = 78 which is N.

    the final output will be S L A N.

    Code:
    int ifEven(string str)
    {
        string array[15];
        char something[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	int i=0;
    	int k=0;
    	
        while(i<str.size()-1)
        {
            array[k]=str.substr(i,2);
    		cout<<array[k]<<" ";
    		i+=2;
    		k+=1;
    	} 
       
    
    }

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Post your code. What have you got so far? What are your specific questions about some code you wrote in an attempt to solve your problem?
    Last edited by 7stud; 12-10-2005 at 07:43 AM.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    thank you for your concern....

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    what does this have to do with the function ifEven?

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    well for my program, i need to split those integer into two each, tats mean 44 11 00 44, there are four size. so if it is odd, we add '0' at the back of it to make it two integer and convert it to char.
    hope you undestand.

  15. #15
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Why don't you write an Encode and a Decode function instead of trying to encode the string by hand? Writing the Encode function should make the issues much clearer.

    31080620170434194020195014201718041105
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed