Using char array?

I am completely lost in my progress and have no idea what path to take. I've spend several days searching for ideas online, but I haven't been able to compile my program correctly.

What I need to do is take an assignment list(may include a range) and print back a list of the problems assigned.

Here's an example:

User types in:

L4-5,1-3,7-10,8-12

Computer displays:

Do problems 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, and 12 of L.

The spaces around the input aren't necessary. So L1-3 should result in the same answer as L 1 - 3.

Here's my progress. I am aware that it is a mess; however, it is a result of my lack of understanding of how to go about it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
        char problem_set[256];
        cout << "ENTER problem set: ";
        getline(cin, problem_set);
        int i, j, k;
        int l;
        l = sizeof(problem_set);
        cout << l;
        while(i < l)
                for(i = 0;i < l ;i++)
                {
                        string a;//Output string that displays entire problem set.
                        i = j;
                        if(problem_set[j] == isdigit(problem_set[j]))
                                string a = a + problem_set[i];
                                cout << "Do Problems: " << problem_set[i] << " of L";
                        if(problem_set[i] == '-')
                        {
                                digit_after_dash = problem_set[i + 1];
                                digit_before_dash = problem_set[i - 1];
                                for(i = 0; i < l; i++)
                                        range = dbd + ", " + (dbd + i)
                        }
                }

        return 0;
}
Line 11: You're using the string version of getline. while problem_set is declared as a char array. If you're going to use a char array, you want
 
  cin.getline (problem_set, 256);

Or change problem_set to a std::string.

Line 14: You want to use strlen(), not sizeof, unless you change problem_set to std::string, then you want problem_set.size().

Line 16: i is uninitialized. You're going to be testing garbage.

Line 16-17: No need for two loops here. The termination condition of the two loops are the same.

Line 20: Where is j initialized? You're going to be setting your loop variable to garbage. It's not a good idea to change your loop variable inside your loop.

Line 22: You're introducing a new instance of a which is different from the variable declared on line 19.

Line 26: digits_after_dash is undefined.

Line 27: digits_before_dash is undefined.

Line 28: This for loop is going to change your outter loop variable. Are you sure you want to loop l times here?

Line 29: Where is dbd defined? range is undefined.

I don't see that your code handles two digit problem numbers.

Your code doesn't sort the problems. Your input example is out of order, but the example output is ordered.

Also, I don't see you allowing for spaces in the input.

Have you even tried to compile this?


Last edited on
I realize compiling it will get me numerous errors because it is not complete. This was the working progress. I was actually looking for feedback in regards to the approach I am taking. What would be the simplest approach? Another approach I was considering was removing all the commas and spaces, sorting the values numerically, and then adding the commas back in where they belong.
I find that compiling as I go along to be a quick way of finding syntax errors early on in the development of a program.

Otherwise, if you wait until the program is complete, you will spend a lot of time fixing syntax errors that could have been caught and fixed earlier when the program was smaller and the errors easier to fix.

As to the approach, I would parse the input field by field, placing numbers in a vector (expanding ranges), then sort the vector and finally print it.

Last edited on
It looks like the problems must be sorted in the output, so you can't do the output until you read and process all of the input.

I'd use vector<bool> to store whether a specific problem is in the set.
Write a function to addRange(vector<bool> &probs, size_t low, size_t high) to set bits probs[low] through probs[high]. Expand probs if it's too small.
In your parsing code, if you see a N, then call addRange(probs, N, N). If you see M-N then call addRange(M,N);
Finally, iterate over probs and print the result.
Topic archived. No new replies allowed.