how can i cast a string to int when i have this

//input problem numbers
    cout<<"\n""enter the numbers";
    //numbers represents what they input
    string numbers;
    getline(cin,numbers);
    //position represents at what location value comma is
    int position;

    while(numbers.find(',') !=string::npos)
    {
        //print out what the user inputs

        //used to split up part between each comma
        string substring;
        //find position of each comma
        position=numbers.find(',');
        //represents first character in string numbers
        int intial=0;
        //find each part which contains a "-" and a number before the comma and store it in variable substring
        substring=numbers.substr(intial,position);
        numbers=numbers.erase(intial,position+1);
        //if it has a - and a ' then do this
        if(int found=substring.find('-') !=string::npos)
            {
                int beginValue = atol(substring.substr(0, found));
                int endValue = atol(substring.substr(found+1));

                int size=endValue-beginValue;
                int substring_list[size];
                for(int a=0; a<=size; a++)
                {
                    for(int i=beginValue; i<=endValue; i++)
                    {
                        substring_list[a]=i;
                    }
                }
                cout<<"\n";
            }
    }


    return 0;
}

Recommended Answers

All 24 Replies

post an example of the input string because I don't have a clue what that code (lines 23-39) is attempting to do.

it says problem is on line 25 and 26 where i am trying to cast the string substring into an integer since it contains integer values. static_cast wouldn't work

so here is what i am supposed to do:

Some students (as early as junior high or even as late as college) find reading assignment lists difficult. They see the teacher write something like 4-10 and do problem 4 and problem 10 — but not the other 5 assigned problems!

To help these poor lost souls, we'll write a program that can take in the (possibly) hyphenated assignment list and print back out a list of all problems assigned. Just in case, we'll also make sure our list is sorted (non-decreasing order) and contains no duplicates.

Here are some examples:
User Types In... We Display...
L1 Do problem 1 of L.
L1-3 Do problems 1, 2, and 3 of L.
L1,2,5 Do problems 1, 2, and 5 of L.
L1-3,5 Do problems 1, 2, 3, and 5 of L.
L1-3,5-7 Do problems 1, 2, 3, 5, 6, and 7 of L.
L1-1,3-3,5-8 Do problems 1, 3, 5, 6, 7, and 8 of L.
L4-5,1-3,7-10 Do problems 1, 2, 3, 4, 5, 7, 8, 9, and 10 of L.
L4-5,1-3,7-10,8-12 Do problems 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, and 12 of L.

Let the problem set name be either a single character, a single word (containing no digits), a quoted string, or even a quoted, space-containing string. The problem set name will always be entered first. If the user uses a quoted problem set name, allow them to use either a single quote (tick-mark) or double quote — but the open and close can be assumed to match. So any of L, LssnTwo, "Lssn2", or even 'Lesson 2' would be a valid problem set name.

Spaces around the input should be optional (so L1-3 should result in the same answer as L 1 - 3). All items/hyphenated groups are to be comma separated (so L1,2,5 is okay but L1 2 5 and L125 are not; well, unless they want to do just problem 125, *grin*).

We'll assume that all problems are to be done for simplicity. (See the option below about evens vs. odds for more fun.)

If the display of the problem list is longer than a line, wrap it gracefully — don't let the terminal window wrap it for you (or the printer could very well chop it off!).

i got first part to work. stuck on second part.

the error message i get is :

cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char>>' to 'const char*...

What does that mean

Search for a number in the string, if the next character is also a number merge it into a single number, if the next character is a hyphen move to the next position and check the same conditions, loop through and print numbers such that all numbers in between are also printed. Repeat this procedure until the end of the string.

but i don't know what number they could input, so i can't use find command.

yeah that is what i am trying to do in my if condition but i cannot convert my string to a int even though it contain nothing but integers.

atoi function might be helpful

tried it gives same error.

already tried that it didn't work.

suppose there are two characters, convert the first character to a number, and similarly the second character.
Now actual number must be = atoi(1st character)*10+atoi(2nd character).
Must be easy to generalise.

k. I will give that a try. So you can't input a string is that what you are saying?

also i want it to stop at found which represents position of the '-'. So why doesn't static_cast or atoi work?

so what you are saying is only store it as char?

take it as string, and define another integer variable and store it over there appropriately.

Move through converting each blocks character sets into integers.

would this work?
The reinterpret_cast operator takes the form

apparently that won't work for the same reason.

this doesn't seem to work.
char a=static_cast<char>(beginValue);

thanks for the help. will ask teacher tom.

can't use that class he says only C-string or string class may be used.

don't need to complicate things, simple logic will be fine, take a string convert it into numbers....................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.