-2

Currently, I'm having some trouble implementing a parse integer program I've made. I get two yellow lines appearing under the function when it is called in the program

using namespace std;

char parseInt(char userInput[40], char& integers);

int main() 
{
    char userInput[40]; 
    char integers[40];

    cout << "After you enter string input, the program will parse all the integers "
         <<  "inside the string and display them.  \n  Enter a string:  \n";
    cin.ignore();
    cin.getline(userInput, 40);

    parseInt(userInput, integers);   //Here lies the problem

    cout << "The integers inside the string you entered were: \n" << integers;

    return 0;
}

char parseInt(char userInput[40], char& integers)
{
    int j = 0;
    for(int i = 0; i<sizeof(userInput); i++)
    {
        if( userInput[i] == 1 || userInput[i] == 2 || userInput[i] == 3 || 
            userInput[i] == 4 || userInput[i] == 5 || userInput[i] == 6 ||
            userInput[i] == 7 || userInput[i] == 8 || userInput[i] == 9 ||
            userInput[i] == 0 )
        {
            integers = userInput[i];
            j++;
        }
    }
}

When the parse Int function is called, the two error messages I'm getting are : -cannot bind rvalue (char) (char*) (&integers) to '(char&)' -invalid conversion from 'char*' to char [-fpermissive]

I'm just having a hard time understanding exactly what the error codes are trying to say, I'm trying to understand them more.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Doe J.
  • 9
  • 2

3 Answers3

1

In parseInt, integers is declared like single character but you are passing an array. Furthermore, you compare userInput as integer instead of as ASCII.

void parseInt(char userInput[40], char integers[40])
{
    int j = 0;
    for(int i = 0; i<sizeof(userInput); i++)
    {
        if( userInput[i] == '1' || userInput[i] == '2' || userInput[i] == '3' || 
        userInput[i] == '4' || userInput[i] == '5' || userInput[i] == '6' ||
        userInput[i] == '7' || userInput[i] == '8' || userInput[i] == '9' ||
        userInput[i] == '0' )
        {
            integers[j] = userInput[i];
            j++;

        }
    }


}

If you use your code,

It return a single character by reference, but not return a chain of characters.

char parseInt(char userInput[40], char& integers)
{
    int j = 0;
    for(int i = 0; i<sizeof(userInput); i++)
    {

Here, you are comparing the userInput with the first elements of the ASCII table.

        if( userInput[i] == 1 || userInput[i] == 2 || userInput[i] == 3 || 
            userInput[i] == 4 || userInput[i] == 5 || userInput[i] == 6 ||
            userInput[i] == 7 || userInput[i] == 8 || userInput[i] == 9 ||
            userInput[i] == 0 )
        {

In the next line, you change the value of the single character but you do not adding a new character, because it is not a array (I supuse you got the idea because you declare j)

            integers = userInput[i];
            j++;
        }
    }
}

In the main,

cin.ignore()

is unnecesary,it makes that you lose the first character of the chain.

  1. Declare integers like an array of characters
  2. Compare userInput as ASCII not as integer (you can compare it as integer but you have to use the integer that corresponds with the character on the ASCII table)
  3. Delete the cin.ignore() line
EloyBG
  • 46
  • 2
1

Many bugs here.

  1. sizeof(userInput) doesn't work like you expect (user4581301 mentioned this in a comment). userInput is a parameter, and due to parameter type adjustment.

    The type of a function is determined using the following rules. The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type "array of T" or of function type T is adjusted to be "pointer to T". After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type.

    As a result, your loop only goes up to sizeof (char*).

  2. You are reading in a string but checking the numeric value of each character. Don't expect that '1' == 1, it's not true on any character encoding in common use. (EloyBG mentioned this one)

  3. You declared integers as a reference to just one integer, but tried to pass a whole array. (Ilan mentioned this)

  4. You're trying to stuff many values into a reference to just one integer. (EloyBG mentioned this too, but his fix is misleading for the same reason as #1)

  5. Reaching the end of a non-void function is illegal, you have to return a value or leave abnormally (user4581301 mentioned this too)


The C way to fix it would be to pass the length of the array as a separate parameter. In C++, you can use a reference to array to either enforce a fixed length or even infer the actual length:

void parseInt(char (&userInput)[40], char (&integers)[40])

Parameter adjustment only changes arrays, not references to arrays, and therefore your sizeof attempt will again work.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Prefer your answer to mine. Much more concise. Recommend an edit to add the `ignore` discarding out the first character of the user input. – user4581301 Jun 08 '18 at 02:01
-1

You are passing in char integers[40] into a function that it's second parameter requires a char to be passed by reference.

change char& to char[40]

char parseInt(char userInput[40], char integers[40])
Ilan Keshet
  • 514
  • 5
  • 19
  • 1
    Not the down voter but this breaks the OP's code in the function. – NathanOliver Jun 07 '18 at 23:52
  • That's true, but a few other things in the OP's function break the OP's function. Your answer may get more love if you straighten Doe out a bit and help deal with the rest of the problems coming their way. – user4581301 Jun 07 '18 at 23:54
  • He only asked why the compiler was complaining -- and I answered him Why is it my task to also fix all his code? – Ilan Keshet Jun 07 '18 at 23:57
  • Fix, no. That's their problem, up to a point. Sometimes you do have to drop code to explain the solution. In this case, you clear one mistake the compiler's catching and replace it with another. You should address what your solution will do to `integers = userInput[i];` and explain why your solution is the right way to go. – user4581301 Jun 08 '18 at 00:03
  • Pointing out that 1 is not the same as '1' or giving them a link to an `isdigit` documentation page would be friendly. Warning them that they are not returning anything in `parseInt` when the code promised to would probably be saintly. Pointing out the problem with `sizeof(userInput)`, that would help them out a fantastic amount. You don't have to fix any of it. They should be able to do that themselves. – user4581301 Jun 08 '18 at 00:07