2

I am aware that there are ways of converting from std::string to c-style but the problem I'm having is this error: 4 IntelliSense: expression must be a modifiable lvalue can anyone tell me what the problem is? Also can you please clarify how to effectively convert to a c-style string and assign it in this particular case?

Thank you

#include <iostream>
#include <string>
#include <algorithm>
#include <stdlib.h>

using namespace std;

class Addition
{
private:
    int num1[255], num2[255], sum[255];
    char str1[255], str2[255];
    string str;
    int len1, len2;
public:
    Addition(){};
    void m_add();
};

void Addition::m_add()
{
    scanf("%s", str);
    int pos = find(str[0], str[255], ' ');
    &str1 = str.substr(0, pos);
    &str2 = str.substr(++pos);
    //scanf("%s", &str1);
    //scanf("%s", &str2);
    /*convert from a character to an int*/
    for (len1 = 0; str1[len1] != '\0'; len1++)
    {
        num1[len1] = str1[len1] - '0';
    }
    for (len2 = 0; str2[len2] != '\0'; len2++)
    {
        num2[len2] = str2[len2] - '0';
    }
    if (str1 <= 0)
    {
        cout << "Invalid input\n";
    }
    int carry = 0;
    int k = 0; //keeps track of index loop stopped at
    //start adding from the end of the array
    int idx1 = len1 - 1;
    int idx2 = len2 - 1;
    //adds only for the size of teh smallest array
    for (; idx1 >= 0 && idx2 >= 0; idx1--, idx2--, k++)
    {
        //we will have to read values stored in sum in reversed order
        sum[k] = (num1[idx1] + num2[idx2] + carry) % 10;
        //using truncation to our benefit
        //carry over can only ever be one thats why we use /10 not %10
        carry = (num1[idx1] + num2[idx2] + carry) / 10;
    }
    /*takes care of the digits not added to sum from bigger array*/
    //if the first array is bigger...
    if (len1 > len2)
    {
        while (idx1 >= 0){
            sum[k++] = (num1[idx1] + carry) % 10;
            carry = (num1[idx1--] + carry) / 10;
        }
    }
    //if the second array is bigger
    else if (len1 < len2)
    {
        while (idx2 >= 0){
            sum[k++] = (num2[idx2] + carry) % 10;
            carry = (num2[idx2--] + carry) / 10;
        }
    }
    //that you have a carry ove to the very end of the number
    if (carry != 0){
        sum[k++] = carry;
    }

    cout << "sum = ";
    //print out digits in 'sum' array from back to front
    for (k--; k >= 0; k--)
    {
        cout << sum[k];
    }
    cout << '\n';
}
int main(){

    Addition inst1;
    int n;
    cin >> n;

    for (int i = 0; i <= n; i++){
        inst1.m_add();
    }
    system("pause");
}  
6
  • 2
    The class string has a function c_str() to convert. Commented Nov 18, 2015 at 0:20
  • scanf("%s", str); is bad because %s calls for char* and str is string. find(str[0], str[255], ' '); may be bad because str[255] may be out-of-range. &str1 = str.substr(0, pos); and &str2 = str.substr(++pos); are bad because &str1 and &str2 are just address values, not variables, and the compiler won't know where to store the data. Commented Nov 18, 2015 at 0:30
  • @MartinZabel Then, use strcpy() or strncpy() (safer) to store the data to the arrays. To use them, add #include <cstring> to the code. Commented Nov 18, 2015 at 0:31
  • Note: scanf("%s", &str1); is also bad because %s calls for char* and &str1 has type char (*)[255]. You should use scanf("%s", str1); instead of this. The same things can be said about scanf("%s", &str2);. Commented Nov 18, 2015 at 0:37
  • if (str1 <= 0) is bad because str1 is a pointer and this comparison seems meaningless. Commented Nov 18, 2015 at 0:41

1 Answer 1

1

I really doubt this is what you want to really do, but since you asked...

You need to do a strcpy:

strcpy(str1, str.substr(0, pos).c_str());

Review man page/cppreference for strcpy for what the parameters are.

Safer may be to use strncpy but that function is not actually meant to do what people use it for. Unfortunately I don't believe there actually is a standard function in either C++ or C that does a C-style string copy limited by length...no safe strcpy. The strncpy function does in a pinch but review the docs on that one too because it's actually meant for an entirely different string format than null-terminated, c-style strings.

It might be tempting to use c_str() on the substr return but this could be dangerous if the function you call is attempting to store a pointer to that space. The return of substr will be destroyed after the call is done. This will destroy the c_str return.

Sign up to request clarification or add additional context in comments.

2 Comments

'cin >> str; int pos = find(str[0], str[255], ' '); string tempstr = str.substr(0, pos); strcpy_s(str1, tempstr.c_str()); tempstr = str.substr(++pos); strcpy(str2, tempstr.c_str());'
@AniekanUmoren Didn't know strcpy_s was added. Was about to say it's common but not standard. You're making extra copies by assigning to tempstr. It shouldn't be necessary to do this because temporaries will last through the function call unless you're calling across threads...and you're not. The C++ thread api in fact takes care of the only likely situation I can think of (starting thread with the copy call).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.