so input would be
Q1,2,3-5
output would be
do problems 1,2,3,4,5 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;
    string number;
    
    //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;
    int lastdigit=0;
    while(num != '\n'){
        
        cin.get(num);
        cout<<"very beginning\n"<<num;
        
        if(num==',')
        {
            if(isdigit(cin.peek()))
            {
                cin.get(num);
                cout<<"from comma\n"<<number<<"\n";
            }
        }
       
        else if(num=='-')
        {
            
            if(isdigit(cin.peek()))
            {
                cin.get(num);
                cout<<"the value of # is\n"<<num;
                for(int a=lastdigit; a<num; a++)
                {
                }
            }
        }
        number=number+num;
    }
    
    cout<<"the final number is"<<number<<"\n"; 
        
    // print them all out

    return 0;
}

Recommended Answers

All 12 Replies

i can't figure out where the lastdigit which represenst beginning of the index which has a - would go to.
help would be appreciated.

cin.get(num);
                cout<<"the value of # is\n"<<num;
                for(int a=lastdigit; a<num; a++)
                {
                }

This section of your code is faulty due to 2 reason's

Firstly
The variable lastdigit has always been assigned the value 0(zero) Through Out your Program,

Secondly
The Variable num is of type char So it is implicitly converted into an integer, But Note that 0 is not equal to its char type '0'. Or any other Digit.

SOLUTIONS
There are many ways in which we can by-pass these faults,

1)I would prefer to use the function atoi() and convert num into an integer.
And then convert it back to char with itoa() but it becomes very hard to do so,

You will get many more suggestions by the way,

lotrsimp12345.

Some suggestions:
1. lastdigit must be char type.
2. Assign the value of num to the lastdigit variable at the bottom and inside the while loop.
3. Use char type loop counter variable.

for(char  a=lastdigit+1; a<num; a++)
      {
         number = number + a;
    }

i need the integers though because i want my final value to be 1,2,3,4,5 in this case. and it doens't like atoi.

thank you very much.

now how would i get a comma to appear between each of them. my updated code is

#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;
    string number;
    
    //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;
    char lastdigit=0;
    while(num != '\n'){
        
        cin.get(num);
        cout<<"very beginning\n"<<num;
        
        if(num==',')
        {
            if(isdigit(cin.peek()))
            {
                cin.get(num);
                cout<<"from comma\n"<<number<<"\n";
            }
        }
       
        else if(num=='-')
        {
            
            if(isdigit(cin.peek()))
            {
                cin.get(num);
                cout<<"the value of # is\n"<<num;
                   
                for(char a=lastdigit+1; a<num; a++)
                {
                    number = number + a;
                    lastdigit=num;                                                      
                }
            }
        }
        number=number+num;
        lastdigit=num;
    }
    
    cout<<"the final number is"<<number<<"\n"; 
        
    // print them all out

    return 0;
}

Should the program prevent input like: A3--5,9-01 ??
Is it allowed that the problemset's name contains more than 1 character ?
What to do with things like: A35,9-01,A-Z-9 ??

>And then convert it back to char with itoa()
itoa() is non-standard.

the loop doesn't quite work for 2 digit numbers.

so my original problem still remains for anything which is 2 char or above for a number.

Yes, The Program will not work for any number greater than 9.

Instead of editing your previous code, try to take in what we can from the previous code and write new code.

Think of a 3- Modeled Approach.

  1. Parser (Input Handler.)
  2. Processor (To Process our Data Set)
  3. Poster. (Output Handler)

Parser: In the Parser you will need to firstly understand what our need particularly is,

I think the best way to go accomplish this is with a Class

You could set up Something Like this.

struct  Sequencer {
string quoted; //Store the Quoted Information Here,
vector<string> listed; //Store All the string values between the comma's here.
};

Now we will need to fill them in our input function which would then send it to the processor
Processor:
what we want the processor to do is to take in the vector listed analyse them and come up with an answer of a string or a vector of ints.

Then the processor function will send it to the Output function,

Poster
There you need to remove duplicates, sort out the numbers add in ',' in between.

And then std::cout the answer :)

Yes, it's a little complicated But now we know what exactly need's to be done And by that we could write the program out.

commented: Looks like a nice approach :) +11
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.