Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Error: cannot convert `std::string' to `int' in initialization

    Please assist got the following code that I need to do calculations extracted from a substr.

    Code:
    // ID_Verification
    #include <iostream>
    #include <string>
    using namespace std;
         
    int main()
    {
        // Initialise strings - originaly id should be interger but made a string to split into substr
        string id, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen;
        // Initialise char for the yes or no answer to come
        char correct; 
           
        cout << "***** South African ID Verification *****" << endl << endl;
        cout << "Enter ID number: ";
        // Enter the id number
        cin >> id;
        
        // Initialise an length interger based on the length of the id string
        int length = id.size();
         
        // a While loop to ensure that the id number consists of 13 digits    
        while(length != 13) {
                     cout << "This is too short or too long for a SA ID number!" << endl;
                     cout << "Enter ID number: ";
                     cin >> id;
                     length = id.size();
                     }                 
        
        // Allocate specific position values of the id number to be used in later calculations
        one = id.substr(0,1);
        two = id.substr(1,1);
        three = id.substr(2,1);
        four = id.substr(3,1);
        five = id.substr(4,1);
        six = id.substr(5,1);
        seven = id.substr(6,1);
        eight = id.substr(7,1);
        nine = id.substr(8,1);
        ten = id.substr(9,1);
        eleven = id.substr(10,1);
        twelve = id.substr(11,1);
        thirteen = id.substr(12,1);
        
        // Print the entered id number
        cout << endl << "ID number: " << one << two << three << four << five << six
        << seven << eight << nine << ten << eleven << twelve << thirteen << endl;
        
        // Confirm correct entry
        cout << "Entered correctly? (Y / N) ";
        cin >> correct;
        
        // while loop to ensure char Y y N n only to be selected
        while(correct != 'Y' && correct != 'y' && correct != 'N' && correct != 'n') {
                   cout << "Entered correctly? (Y / N) ";
                   cin >> correct; 
                   }
        
        // while loop to restart if incorrect entry           
        while(correct == 'N' || correct == 'n') {
                   cout << "Enter ID number: ";
                   cin >> id;
        
                   length = id.size();
        
                   while(length != 13) {
                                cout << "This is too short or too long for a SA ID number!" << endl;
                                cout << "Enter ID number: ";
                                cin >> id;
                                length = id.size();
                                }
                                one = id.substr(0,1);
                                two = id.substr(1,1);
                                three = id.substr(2,1);
                                four = id.substr(3,1);
                                five = id.substr(4,1);
                                six = id.substr(5,1);
                                seven = id.substr(6,1);
                                eight = id.substr(7,1);
                                nine = id.substr(8,1);
                                ten = id.substr(9,1);
                                eleven = id.substr(10,1);
                                twelve = id.substr(11,1);
                                thirteen = id.substr(12,1);
        
                                cout << endl << "ID number: " << one << two << three << four << five << six
                                << seven << eight << nine << ten << eleven << twelve << thirteen << endl;
                                cout << "Entered correctly? (Y / N) ";
                                cin >> correct;  
                                }
        
        // The start of the calculations - However need string converted to interger
        int first = one + three + five + seven + nine + eleven; // Error: cannot convert `std::string' to `int' in initialization 
        
        // Test calculations thusfar
        cout << first << endl;
        
        return 0;
    }

  • #2
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Yes. Simply put, yes.

    You are concatenating (NOT summing) strings, then assigning this string to an integer.
    atoi(myString.c_str()) may be what you want to do for each string.

  • Users who have thanked Apothem for this post:

    hans_cellc (12-22-2011)

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Thanks a mil it worked perfectly as per the code below.
    Now however I need to convert an interger to a string.
    I can then use the same method to add the values but first need to convert it.
    Any ideas?

    Code:
    // ID_Verification
    #include <iostream>
    #include <string>
    using namespace std;
         
    int main()
    {
        // Initialise strings - originaly id should be interger but made a string to split into substr
        string id, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen;
        // Initialise char for the yes or no answer to come
        char correct; 
           
        cout << "***** South African ID Verification *****" << endl << endl;
        cout << "Enter ID number: ";
        // Enter the id number
        cin >> id;
        
        // Initialise an length interger based on the length of the id string
        int length = id.size();
         
        // a While loop to ensure that the id number consists of 13 digits    
        while(length != 13) {
                     cout << "This is too short or too long for a SA ID number!" << endl;
                     cout << "Enter ID number: ";
                     cin >> id;
                     length = id.size();
                     }                 
        
        // Allocate specific position values of the id number to be used in later calculations
        one = id.substr(0,1);
        two = id.substr(1,1);
        three = id.substr(2,1);
        four = id.substr(3,1);
        five = id.substr(4,1);
        six = id.substr(5,1);
        seven = id.substr(6,1);
        eight = id.substr(7,1);
        nine = id.substr(8,1);
        ten = id.substr(9,1);
        eleven = id.substr(10,1);
        twelve = id.substr(11,1);
        thirteen = id.substr(12,1);
        
        // Print the entered id number
        cout << endl << "ID number: " << one << two << three << four << five << six
        << seven << eight << nine << ten << eleven << twelve << thirteen << endl;
        
        // Confirm correct entry
        cout << "Entered correctly? (Y / N) ";
        cin >> correct;
        
        // while loop to ensure char Y y N n only to be selected
        while(correct != 'Y' && correct != 'y' && correct != 'N' && correct != 'n') {
                   cout << "Entered correctly? (Y / N) ";
                   cin >> correct; 
                   }
        
        // while loop to restart if incorrect entry           
        while(correct == 'N' || correct == 'n') {
                   cout << "Enter ID number: ";
                   cin >> id;
        
                   length = id.size();
        
                   while(length != 13) {
                                cout << "This is too short or too long for a SA ID number!" << endl;
                                cout << "Enter ID number: ";
                                cin >> id;
                                length = id.size();
                                }
                                one = id.substr(0,1);
                                two = id.substr(1,1);
                                three = id.substr(2,1);
                                four = id.substr(3,1);
                                five = id.substr(4,1);
                                six = id.substr(5,1);
                                seven = id.substr(6,1);
                                eight = id.substr(7,1);
                                nine = id.substr(8,1);
                                ten = id.substr(9,1);
                                eleven = id.substr(10,1);
                                twelve = id.substr(11,1);
                                thirteen = id.substr(12,1);
        
                                cout << endl << "ID number: " << one << two << three << four << five << six
                                << seven << eight << nine << ten << eleven << twelve << thirteen << endl;
                                cout << "Entered correctly? (Y / N) ";
                                cin >> correct;  
                                }
        
        // The start of the calculations - However need string converted to interger
        // int first = one + three + five + seven + nine + eleven; // Error: cannot convert `std::string' to `int' in initialization 
        // Convert strings to intergers
        int first = atoi(one.c_str());
        int second = atoi(two.c_str());
        int third = atoi(three.c_str());
        int fourth = atoi(four.c_str());
        int fith = atoi(five.c_str());
        int sixth = atoi(six.c_str());
        int seventh = atoi(seven.c_str());
        int eighth = atoi(eight.c_str());
        int ninth = atoi(nine.c_str());
        int tenth = atoi(ten.c_str());
        int eleventh = atoi(eleven.c_str());
        int twelth = atoi(twelve.c_str());
        int thirteenth = atoi(thirteen.c_str());
        
        // First Calculation - Add all the digits of the ID number in the odd positions 
        // (except for the last number, which is the control digit):
        int calc1 = first + third + fith + seventh + ninth + eleventh;
        
        // Second Calculation - Take all the even digits as one number and multiply that by 2
        string calc2a = two + four + six + eight + ten + twelve;
        int calc2 = atoi(calc2a.c_str());
        calc2 = calc2 * 2;
        
        // Third Calculation - Add the digits of this number together (in calc2)
        // Convert answer in calc2 back to a string and then add them together
        string calc3a = calc2;
        
        // Test calculations thusfar
        cout << calc1 << endl;
        cout << calc2 << endl;
        cout << calc3a << endl;
        
        return 0;
    }

  • #4
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Code:
    string convertInt(int number)
    {
       stringstream ss;  //create a stringstream
       ss << number;  //add number to the stream
       return ss.str();  //return a string with the contents of the stream
    }
    from: http://www.cplusplus.com/forum/beginner/7777/
    Last edited by Apothem; 12-22-2011 at 09:55 PM.

  • Users who have thanked Apothem for this post:

    hans_cellc (12-23-2011)

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Smile Complete working code

    thanks for help on this small project. First of many.

    Code:
    // ID_Verification
    // 24/12/11 10:56
    // Author: SJ Fouche
    // Email: hans_cellc@yahoo.com
    
    #include <iostream>
    #include <string>
    using namespace std;
    
         
    int main()
    {
        // Initialise strings - originaly id should be interger but made a string to split into substr
        string id, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen;
        // Initialise char for the yes or no answer to come
        char correct; 
           
        cout << "***** South African ID Verification *****" << endl << endl;
        cout << "Enter ID number: ";
        // Enter the id number
        cin >> id;
        
        // Initialise an length interger based on the length of the id string
        int length = id.size();
         
        // a While loop to ensure that the id number consists of 13 digits    
        while(length != 13) {
                     cout << "This is too short or too long for a SA ID number!" << endl;
                     cout << "Enter ID number: ";
                     cin >> id;
                     length = id.size();
                     }                 
        
        // Allocate specific position values of the id number to be used in later calculations
        one = id.substr(0,1);
        two = id.substr(1,1);
        three = id.substr(2,1);
        four = id.substr(3,1);
        five = id.substr(4,1);
        six = id.substr(5,1);
        seven = id.substr(6,1);
        eight = id.substr(7,1);
        nine = id.substr(8,1);
        ten = id.substr(9,1);
        eleven = id.substr(10,1);
        twelve = id.substr(11,1);
        thirteen = id.substr(12,1);
        
        // Print the entered id number
        cout << endl << "ID number: " << one << two << three << four << five << six
        << seven << eight << nine << ten << eleven << twelve << thirteen << endl;
        
        // Confirm correct entry
        cout << "Entered correctly? (Y / N) ";
        cin >> correct;
        
        // while loop to ensure char Y y N n only to be selected
        while(correct != 'Y' && correct != 'y' && correct != 'N' && correct != 'n') {
                   cout << "Entered correctly? (Y / N) ";
                   cin >> correct; 
                   }
        
        // while loop to restart if incorrect entry           
        while(correct == 'N' || correct == 'n') {
                   cout << "Enter ID number: ";
                   cin >> id;
        
                   length = id.size();
        
                   while(length != 13) {
                                cout << "This is too short or too long for a SA ID number!" << endl;
                                cout << "Enter ID number: ";
                                cin >> id;
                                length = id.size();
                                }
                                one = id.substr(0,1);
                                two = id.substr(1,1);
                                three = id.substr(2,1);
                                four = id.substr(3,1);
                                five = id.substr(4,1);
                                six = id.substr(5,1);
                                seven = id.substr(6,1);
                                eight = id.substr(7,1);
                                nine = id.substr(8,1);
                                ten = id.substr(9,1);
                                eleven = id.substr(10,1);
                                twelve = id.substr(11,1);
                                thirteen = id.substr(12,1);
        
                                cout << endl << "ID number: " << one << two << three << four << five << six
                                << seven << eight << nine << ten << eleven << twelve << thirteen << endl;
                                cout << "Entered correctly? (Y / N) ";
                                cin >> correct;  
                                }
        
        // The start of the calculations - However need string converted to interger
        // int first = one + three + five + seven + nine + eleven; // Error: cannot convert `std::string' to `int' in initialization 
        // Convert strings to intergers
        int first = atoi(one.c_str());
        int second = atoi(two.c_str());
        int third = atoi(three.c_str());
        int fourth = atoi(four.c_str());
        int fith = atoi(five.c_str());
        int sixth = atoi(six.c_str());
        int seventh = atoi(seven.c_str());
        int eighth = atoi(eight.c_str());
        int ninth = atoi(nine.c_str());
        int tenth = atoi(ten.c_str());
        int eleventh = atoi(eleven.c_str());
        int twelth = atoi(twelve.c_str());
        int thirteenth = atoi(thirteen.c_str());
        
        // First Calculation - Add all the digits of the ID number in the odd positions 
        // (except for the last number, which is the control digit):
        int calc1 = first + third + fith + seventh + ninth + eleventh;
        
        // Second Calculation - Take all the even digits as one number and multiply that by 2
        string calc2a = two + four + six + eight + ten + twelve;
        int calc2 = atoi(calc2a.c_str());
        calc2 = calc2 * 2;
        
        // Third Calculation - Add the digits of this number together (in calc2)
        // Convert answer in calc2 back to a string and then add them together
        char b[50];
        string calc3a;
        sprintf(b,"%d",calc2);// C-style string formed without null
        calc3a += b;
        string numberz[20];
        int numbers[20];
        int calc3 = 0;
        int stringLength = calc3a.size();
        
        for(int i = 0; i < stringLength; i++) {
               numberz[i] = calc3a.substr(i,1);
               numbers[i] = atoi(numberz[i].c_str());
        calc3 += numbers[i];
        }
        
        // Fourth Calculation - Add the answer of calc3 to the answer of calc1
        int calc4 = calc3 + calc1;
        
        // Fith calculation - Subtract the second character from calc4 from 10, 
        // this number should now equal the control character
        char c[50];
        string calc5a;
        sprintf(c,"%d",calc4);// C-style string formed without null
        calc5a += c;
        string secondNum = calc5a.substr(1, 1);
        int secondNumber = atoi(secondNum.c_str());
        int answer = 10 - secondNumber;
        
        // Test calculations thusfar
        //cout << calc1 << endl;
        //cout << calc2 << endl;
        //cout << calc3a << endl;
        //cout << calc3 << endl;
        //cout << calc4 << endl;
        //cout << calc5a << endl;
        //cout << secondNum << endl;
        //cout << secondNumber << endl;
        //cout << answer << endl;
        //cout << thirteenth << endl;
        
        // Check answer to equal control character (thirteenth)
        if(answer == thirteenth || answer == 10){
        cout << endl << "*****************************************" << endl;
        cout << "***** Valid South African ID number *****" << endl;
        cout << "*****************************************" << endl;
        }
        else {
        cout << endl << "*****************************************" << endl;
        cout << "***** INVALID South African ID number *****" << endl;
        cout << "*****************************************" << endl;
        }
        
        // Wait for enter to end execution
        cin.get();
        return 0;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •