954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

why can't i convert please help

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;
}
lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

this is code that won't convert.

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

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

k thanks.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

still confused why it won't convert.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

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

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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?

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

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.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

i think that using stringstream might work.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

Char to string:
1
2
3
4
5
6
7

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

String to int:
1
2
3
4
5
6
7

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

i think this would work.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

thanks for help.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 
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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You