its in the num=="-".

#include <iostream>
#include <cstdlib>

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);
        // can't use cin >> ws since it would eat the newline
       /* while (cin.peek() != '\n' && isspace(cin.peek()))
        {
            cin.ignore();
        }*/
        if(num==',')
        {
            if(isdigit(cin.peek()))
            {
                cin.get(num);
                cout<<"from comma"<<number<<"\n";
            }
                        
        }
        else if (num=='-')
        {
            if(isdigit(cin.peek()))
            {
            // remember num
             
            int b=atoi(cin.get(num));
            for(int a=lastdigit+1; a<=b; a++)
            {
                num++;  
            }
        }
        else if(cin.peek()=='\n')// must be a newline!
        {
//          
            break;
        }
        else if(isdigit(cin.peek()))
        {

        }
        if(isdigit(num))
        {
        lastdigit=num;
        }
        number=number+num;
    }
    
    cout<<"the final number is"<<number<<"\n"; 
        
    // print them all out

    return 0;
}

Recommended Answers

All 13 Replies

this is code that won't convert.

int b=atoi(cin.get(num));

1->atoi signature is this:

int atoi ( const char * str );

2-> read some more threads here to find out alternatives to atoi. You should avoid using it.

commented: A valiant effort, but I'm afraid the OP hasn't learn anything from any of their other posts either. +36

still confused why it won't convert.

b=atoi(num,num.c_str());

it gives me error that request for member'c_str' in'num', which is of non-class type 'char'

what does that mean, i am passsing in a character

Hmmm.. don't know where to start from. First things first, 'num' is of type 'char', do you have a class 'char' which has a fn c_str defined? No. Its a fn of the 'string' class. Then you didn't even read my earlier post properly.
I guess you need to read up the chapters on the 'data types' a couple of times over. Read about int,char,double etc. Then read about arrays, chars and pointers. And then about the 'string' class. And then come back to this problem again.

i am supposed to this without pointers. The next topic is pointers.

Why don't you post the problem statement first. Then explain how you are trying to achieve it in your code. Then as I said, read about data-types. how did you find out about c_str fn? And I posted the correct function signature for atoi in my post above, does it have 2 parameters? Did you even read that?

they enter a problem set name and number. I am supposed to get in my problem name which i can do fine. for the numbers though i need to have it print out all the numbers they are supposed to do.

Example
input:
L1,2,3-5
output:
do problems 1,2,3,4,5 of L.

i think that using stringstream might work.

Char to string:
1
2
3
4
5
6
7


#include <sstream>
#include <string>
stringstream ss;
string s;
char c = 'a';
ss << c;
ss >> s;

String to int:
1
2
3
4
5
6
7


#include <sstream>
#include <string>
stringstream ss;
string s;
int n = 1;
ss << n;
ss >> s;

i think this would work.

thanks for help.

i think that using stringstream might work.

All sorts of things would work. If it were me, I'd read in the whole line with getline, then parse it. Delimiters would be commas and dashes, after you extracted the first letter.

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.