it needs to make sure numbers aren't duplicate and are ordered. maybe change my cout statements to arrays?
Thanks.
This is what program is supposed to do
input
Q 1,2,3-5,6-7
output
do problem 1,2,3,4,5,6,7 of Q.

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
using namespace std;

int main()
{


    cout <<"enter the problemset and number""\n";
    //problems represents name and numbers
    string problems;
    char quote;
    char num;
    short number, number2;
    
    //gather name
    if(cin.peek()=='"' || cin.peek() == '\'')
    {
        cin >>quote;
        getline(cin,problems,quote);
    }
    else
    {
        while (!isdigit(cin.peek()) && !isspace(cin.peek()))
        {
         //   (char)cin.peek();
            problems += cin.get();
        }
    }


    //gather problem numbers
    //cin >> num;
    
        cin>>number;
        cout<<"very beginning\n"<<number;
        
    while(cin.peek() != '\n')
    {
        
        if(cin.peek()==',')
        {
            cin.ignore();
            cin >> ws;
            if(isdigit(cin.peek()))
            {
                cin>>number;
                cout<<"from comma\n"<<number<<"\n";
            }
        }
       
        else if(cin.peek()=='-')
        {
            cin.ignore();
            cin >> ws;
            if(isdigit(cin.peek()))
            {
                cin>>number2;
                cout<<"the value of # is\n"<<number2;
                   
                for(short a=number+1; a<=number2; a++)
                {
                    cout<<"next do #"<<a;
               
                }
            }
        }
        
    }
    
    //check for dulplicate and put in order
        
    // print them all out

    return 0;
}

Dani AI

Generated

asked for a clean way to take a labeled input that contains single numbers and ranges, remove duplicates, and produce the numbers in order. pointed to a related thread. Below is a compact, robust approach and a small C++ example that parses a full input line, extracts the label, expands ranges, removes duplicates and returns a sorted list of integers.

Basic approach:

  1. Read the entire input line with getline.
  2. Separate the label from the numeric part by finding the first digit. Trim whitespace and surrounding quotes from the label.
  3. Split the numeric part on commas. For each token, trim spaces. If it contains a dash, parse the two endpoints and generate the inclusive range; otherwise parse a single number.
  4. Insert numbers into a std::set<int> to both dedupe and keep them sorted. Convert to std::vector<int> if you need random access or a specific output format.
  5. Validate numeric parsing with std::stoi inside a try/catch (or use strtol) and decide how to handle reversed ranges or malformed tokens.

Example implementation:

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <sstream>
#include <algorithm>
#include <cctype>

std::pair<std::string,std::vector<int>> parseLine(const std::string& line) {
    std::string s = line;
    size_t pos = s.find_first_of("0123456789");
    std::string label = (pos==std::string::npos) ? s : s.substr(0,pos);
    std::string rest = (pos==std::string::npos) ? "" : s.substr(pos);
    auto trim_inplace = [](std::string &t){
        size_t a=0; while(a<t.size() && std::isspace((unsigned char)t[a])) ++a;
        size_t b=t.size(); while(b>a && std::isspace((unsigned char)t[b-1])) --b;
        t = t.substr(a,b-a);
        if (t.size()>=2 && (t.front()=='\"' || t.front()=='\'') && t.back()==t.front()) t = t.substr(1,t.size()-2);
    };
    trim_inplace(label);
    std::set<int> nums;
    std::stringstream ss(rest); std::string token;
    while (std::getline(ss, token, ',')) {
        trim_inplace(token);
        if (token.empty()) continue;
        size_t dash = token.find('-');
        try {
            if (dash==std::string::npos) nums.insert(std::stoi(token));
            else {
                int a = std::stoi(token.substr(0,dash));
                int b = std::stoi(token.substr(dash+1));
                if (a<=b) for (int k=a;k<=b;++k) nums.insert(k);
                else for (int k=b;k<=a;++k) nums.insert(k);
            }
        } catch(...) { /* ignore invalid token */ }
    }
    return {label, std::vector<int>(nums.begin(), nums.end())};
}

Notes and cautions: use getline so trailing input isn't lost; handle very large ranges carefully (expanding enormous ranges can exhaust memory); decide whether reversed ranges should be accepted or treated as errors; std::set simplifies dedupe+sorting but costs log(n) per insert—acceptable for typical problem lists.

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.