Thread: Help me with this error Please

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Help me with this error Please

    How to pass string(arr) to function and perform operation.

    i m receiving an error:
    ....main.cpp|35|error: cannot convert 'std::string' to 'char*' for argument '1' to 'int check(char*)'|


    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    using namespace std;
    
    int check(char *arr);
    
    int main()
    {
        string arr;
        int cont=1;
        int chck;
    
        while (1)
        {
            cout << "Enter your Date of Birth (Format: DD/MM/YYYY e.g: 11/09/1984): ";
            getline(cin,arr);
            if (arr == "0")
            {
                cout << "Thankyou for using... :)";
                exit(1);
            }
            else
            {
    
                if (arr.length() != 10 )
                {
                    cout << "Syntax Error! Please follow the syntax\n\n";
    
                    continue;
                }
    
    
    
                if(check(arr) != 0)    // Checking the Syntax
                {
                    if (check(arr) == 1)
                    {
                        cout << "\nError in Date. Please try again and follow the syntax\n";
                    }
                    if (check(arr) == 2)
                    {
                        cout << "\nError in Month. Please try again and follow the syntax\n";
                    }
                    if (check(arr) == 3)
                    {
                        cout << "\nError in Year. Please try again and follow the syntax\n";
                    }
                    if (check(arr) == 4)
                    {
                        cout << "\nError in Syntax. Please try again and follow the syntax\n";
                    }
                    cont=1;
                }
    
    
    
            }
        }
    
        return 0;
    }
    
    int check(char arr[10])
    {
        if ((int)arr[0] <= 47 || (int)arr[0] >= 50 || (int)arr[1] <= 47 || (int)arr[1] >= 57 )
        {
            return 1;// Date problem
        }
    
        if ( (int)arr[3] == 47 && (int)arr[4] <= 48 || (int)arr[4] >= 57)
        {
            return 2; // month problem
        }
    
        if( (int)arr[3] == 48 && (int)arr[4] <= 47 || (int)arr[4] >= 49)
        {
            return 2; // month problem
        }
    
        if( (int)arr[6] <= 48 || (int)arr[6] >= 49)
        {
            return 3; // Year problem
        }
        if( (int)arr[2] == '/' && (int)arr[5] == '/')
        {
            return 4;
        }
    
        return 0;
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    2,642
    Pass
    Code:
    arr.c_str()
    Instead.

    Also, consider using constants instead of magic numbers.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    magic nos?? :\ ??

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    still error :(

    error: ...\main.cpp|34|error: invalid conversion from 'const char*' to 'char*'|

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    using namespace std;
    
    int check(char *arr);
    
    int main()
    {
        string arr;
        int cont=1;
        int chck;
    
        while (1)
        {
            cout << "Enter your Date of Birth (Format: DD/MM/YYYY e.g: 11/09/1984): ";
            getline(cin,arr);
            if (arr == "0")
            {
                cout << "Thankyou for using... :)";
                exit(1);
            }
            else
            {
    
                if (arr.length() != 10 )
                {
                    cout << "Syntax Error! Please follow the syntax\n\n";
    
                    continue;
                }
    
    
                chck = check(arr.c_str());
                if(chck != 0)    // Checking the Syntax
                {
                    if (chck == 1)
                    {
                        cout << "\nError in Date. Please try again and follow the syntax\n";
                    }
                    if (chck == 2)
                    {
                        cout << "\nError in Month. Please try again and follow the syntax\n";
                    }
                    if (chck == 3)
                    {
                        cout << "\nError in Year. Please try again and follow the syntax\n";
                    }
                    if (chck == 4)
                    {
                        cout << "\nError in Syntax. Please try again and follow the syntax\n";
                    }
                    cont=1;
                }
    
    
    
            }
        }
    
        return 0;
    }
    
    int check(char arr[10])
    {
        if ((int)arr[0] <= 47 || (int)arr[0] >= 50 || (int)arr[1] <= 47 || (int)arr[1] >= 57 )
        {
            return 1;// Date problem
        }
    
        if ( (int)arr[3] == 47 && (int)arr[4] <= 48 || (int)arr[4] >= 57)
        {
            return 2; // month problem
        }
    
        if( (int)arr[3] == 48 && (int)arr[4] <= 47 || (int)arr[4] >= 49)
        {
            return 2; // month problem
        }
    
        if( (int)arr[6] <= 48 || (int)arr[6] >= 49)
        {
            return 3; // Year problem
        }
        if( (int)arr[2] == '/' && (int)arr[5] == '/')
        {
            return 4;
        }
    
        return 0;
    }

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    2,642
    Change the prototype and definition to use:
    Code:
     int check(const char* arr)
    Btw.. Why not use strings there too ? That would make it nicer to read.

    Also..try fixing these warnings:
    a.cpp: In function ‘int main()’:a.cpp:11:9: warning: variable ‘cont’ set but not used [-Wunused-but-set-variable]
    a.cpp: In function ‘int check(const char*)’:
    a.cpp:71:67: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    a.cpp:76:66: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
    (Though it may not seem important, it could turn out to be)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,438
    > if ((int)arr[0] <= 47 || (int)arr[0] >= 50 || (int)arr[1] <= 47 || (int)arr[1] >= 57 )
    47 etc are magic numbers.

    Constants are both more portable and more readable, as in
    if ((int)arr[0] < '0' || (int)arr[0] >= '3' || (int)arr[1] < '0' || (int)arr[1] > '9' )
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    24,456
    Change
    int check(char *arr);
    to
    int check(const std::string& arr);
    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: 15
    Last Post: 11-28-2011, 10:48 AM
  2. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  3. Replies: 1
    Last Post: 11-15-2010, 10:14 AM
  4. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  5. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM