0

if it contains quote it cuts it out, then i am stuck on part when i just enter in a problemset without a quote or single quote. Then it doesn't gather numbers correctly but stores the entire string

int main()
{


    cout <<"enter the problemset and number""\n";
    //problems represents name and numbers
    string problems , probtitle;
    string xx;
    bool x=true;
    char quote;
    string str1, str2;
    string begin;
    //gather name
    if(cin.peek()=='"' || cin.peek() == '\'')
    {
        cin >>quote;
        getline(cin,problems,quote);
    }
    else
    {
            getline(cin,problems);
    }
cout<<problems;

    //gather problem numbers

    while(x==true)
    {
        if(cin.peek()=='0'||cin.peek()=='1'||cin.peek()=='2'||cin.peek()=='3'||cin.peek()=='4'||cin.peek()=='5'
||cin.peek()=='6'||cin.peek()=='7'||cin.peek()=='8'||cin.peek()=='9'||cin.peek()==','||cin.peek()=='-')
        {
            getline(cin,problems);
        }
        else
        {
            getline(cin,xx);
            if(xx.compare("\n"))
            {
            x=false;
            }
        }
    }
    cout<<problems;
3
Contributors
8
Replies
10
Views
7 Years
Discussion Span
Last Post by Salem
0

Then it doesn't gather numbers correctly but stores the entire string

That's what you tell it to do with getline(cin,problems) . I can help you fix it by taking the entire line and parsing it, but I need you to describe the format of the problemset. Just showing your code isn't enough.

0

Why not just read a whole line with getline, then use a utility function which examines the line to see if quotes are present, and if so remove them.

All this peek() stuff is overly complicating things.

0

so my problem is they enter a line of code which contains the problemname which may contain quotes or not.Then have to find the numbers. Say do problems what ever numbers each seperated by a comma and finally state what problem set from. Can't have duplicate numbers and must be ordered.

0

That's more helpful, but a format specification would be better. Show an example of the input and output of the program and explain how the input got to the output.

0

so input could be
"bob"1-5,6-10,11-15,57
or
'bob' 1-6,1-7,9-10
or
bob 1-7,12-18
spaces don't matter after problemset name
output could be
do problems 1,2,3,4,5,6,7,12,13,14,15,16,17,18 of bob. from last example

0

most recent code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{


    cout <<"enter the problemset and number""\n";
    //problems represents name and numbers
    string problems , probtitle;
    string xx;
    bool x=true;
    char quote;
    string str1, str2;
    string begin;
    //gather name
    if(cin.peek()=='"' || cin.peek() == '\'')
    {
        cin >>quote;
        getline(cin,problems,quote);
    }
    else
    {
            getline(cin,problems);
    }

    //gather problem numbers

    while(x==true)
    {
        if(cin.peek()=='0'||cin.peek()=='1'||cin.peek()=='2'||cin.peek()=='3'||cin.peek()=='4'||cin.peek()=='5'||cin.peek()=='6'||cin.peek()=='7'||cin.peek()=='8'||cin.peek()=='9'||cin.peek()==','||cin.peek()=='-')
        {
            getline(cin,problems);
        }
        else
        {
            getline(cin,xx);
            if(xx.compare("\n"))
            {
            x=false;
            }
        }
    }
    cout<<problems;


int length=problems.find(',');
do{
    if(problems.find(',') != string::npos)
    {
        size_t a,b;
        a=problems.find(',');
        b=problems.find('-');


         if(problems.find('-') != string::npos&&b<a)
         {
                    str1 = problems.substr(0,problems.find('-'));
                    cout<<"from do ifif1\n"<<str1;
                    problems=problems.erase(0,problems.find('-'));
                    cout<<"from erase after first erase"<<problems;
                    str2=problems.substr(problems.find('-')+1,problems.find(','));
                    cout<<"from doifif2\n"<<str2;
                    problems=problems.erase(0,problems.find(',')+1);
                    cout<<"the problems left after erase are"<<problems;

        }
        else
        {
                    cout << "from do if else\n" ;
                    str1 = problems.substr(0,problems.find(','));
                    cout<<"from elseelseelseelse\n"<<str1;
                    problems=problems.erase(0,problems.find(',')+1);
        }

    }
    else
    {
        cout <<"No Comma\n";


        if(problems.find('-') != string::npos)
        {
                    str1 = problems.substr(0,problems.find('-'));
                    cout<<str1;
                    str2=problems.substr(problems.find('-')+1);
                    cout<<str2;
                    problems=problems.erase(0,problems.find('-')+1);
        }
        else
        {cout << "\n IF ELSE" ;
                    str1 = problems.substr(0,problems.find(','));
                    cout<<str1;
                    if(problems.find(',') != string::npos)
                    {
                    problems=problems.erase(0,problems.find(',')+1);
                    }
                    else{
                        problems="";
                    }
        }

    }
}while(problems.length()>0 );
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.