Thread: problems with string

  1. #1
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90

    problems with string

    Code:
    //* String to Int *//
    
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int sum_string_numbers(string x);
    
    
    int main
    {
        string my_string = "1,2,5,4,3,7";
        sum_string_numbers(my_string);
        
        return 0;
    }
    
    
    int sum_string_numbers(string x)
    {
        int sum=0;
        for(i=0; x!='/0';i++)
        {
            if (isdigit(x[i]) == 1)
            {
                sum += stoi(x);
                i++;
            }
            i++;
        }
        return sum;
    }
    well, I try to use string var, but I get this error:

    expected primary expression before 'my_string'.

    please helpppp

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Missing brackets (argument list) for main().

    That's not the only problem in your code that will prevent it compiling, but is the one you asked about.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    This really has nothing to do with the string, that "before" in your error message is very important. So start looking at things "before" that line. For example how do you implement a function, remember main() is a function.

    Jim

  4. #4
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    Code:
    //* String to Int *//
    
    
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int sum_string_numbers(string x);
    
    
    int main()
    {
        string my_string = "1,2,5,4,3,7";
        sum_string_numbers(my_string);
    
    
        return 0;
    }
    
    
    int sum_string_numbers(string x)
    {
        int sum=0;
        int i;
        for(i=0; x[i] != '\0';i++)
        {
            if (isdigit(x[i]) == 1)
            {
                sum += stoi(x[i]);
                i++;
            }
            i++;
        }
        return sum;
    }
    THANK YOU!!!

    now the last problem is with stoi function
    'stoi' was not declared in this scope <----------------

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What is the exact version of the compiler are you using? The stoX() series of functions are C++11 features and may not be available with your compiler.

    Jim

  6. #6
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    GNU GCC Compiler (from CodeBlocks)

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And the version number of the compiler is? The version of your IDE (Code::Blocks) is? Also did you modify the project settings to enable C++11 mode?

    Jim

  8. #8
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    code block 13.12, no ver on compiler, and I didnt modify... how can I do that ?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not that you need to use stoi() anyways.
    Code:
    string digits = "0123456789";
    for (string::size_type i = 0; i < digits.size(); i++) {
      int iNum = digits[i] - '0';
      cout << iNum << '\n';
    }
    Control your loops with string::size(). std::string does not need to be zero terminated, so comparisons that rely on it being terminated may fail to work.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    how can I do that ?
    From the command line type "gcc --version".


    Jim

  11. #11
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90
    can you explain please "string::size_type" ..?

  12. #12
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by noririco View Post
    can you explain please "string::size_type" ..?
    It's just a typedef, usually for std::size_t, which in turn is usually just a typedef for unsigned int.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's just a name for an unsigned integer type. You can use unsigned int I think, and there will be no problem, unless the string is extra long.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can use unsigned int I think, and there will be no problem
    A size_t or string::size_type are not necessarily an unsigned int, it could be any unsigned type, an unsigned long for example. You are better off using the correct type, size_t or std::string size_type then you will be correct no matter what operating system or compiler you happen to be using.

    Jim

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Thanks Jim. The rest of my post was good too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with std::string
    By EverydayDiesel in forum C++ Programming
    Replies: 40
    Last Post: 02-20-2014, 11:36 PM
  2. string problems
    By Hunterofman in forum C++ Programming
    Replies: 12
    Last Post: 05-05-2008, 08:34 AM
  3. String Problems
    By Junior89 in forum C++ Programming
    Replies: 8
    Last Post: 12-21-2004, 07:00 PM
  4. String problems
    By sirSolarius in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2003, 08:07 PM
  5. string problems
    By mart_man00 in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 06:45 PM