I was writing a program that would take a set and list out all of the numbers of that set. For example:
L1,2,3-5 would read 1,2,3,4,5 of L
LessonTwo1-10 would read 1,2,3,4,5,6,7,8,9,10 of LessonTwo
"Lesson 2" 2 , 11 - 15 , 3 , 4 , 7- 9 would read 2,3,4,7,8,9,11,12,13,14,15 of Lesson Two
I wrote some code that would read user-defined string input1 and then parse out the name of the set into string setname. Then it would parse out the problem numbers and (ex 13) and problem sets (ex 1-3) into vector<string> probSet. When I complied, it had no errors but if I run it, it gets hung up. I added cout<<"\n\na" (a,b,c,...) statements after each i++; statement and it seems to go into an infinite loop at no specific point even when entering the same input each time. Sometimes it doesn't even display any of the alphabets I used and it just stays hung. I thought it might have also been an input stream problem but I'm not sure about that. This is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
string input1;
string setname="";
vector<string> probSet;
vector<int> probNum;
cout<<"\n\n\nEnter in your problem list: ";
cin>>input1;
cout<<input1<<flush;
for(unsigned i=0; i<input1.size(); i++)
{
if(((isdigit(input1[i])!=0)&&(input1[i]!=',')&&(input1[i]!='-'))||(input1[i]=='\'')||(input1[i]=='"'))
{
if(input1[i]=='\''||input1[i]=='"')
{
do
{
setname+=input1[i];
i++;
}while(input1[i]!='\''||input1[i]!='"');
}
else if(isalpha(input1[i])==0)
{
do
{
setname+=input1[i];
i++;
}while(isalpha(input1[i])==0);
}
}
else if(isdigit(input1[i])==0)
{
probSet.push_back(string(1,input1[i]));
do
{
if(input1[i]==' ')
{
i++;
}
else if(isdigit(input1[i])==0||input1[i]=='-')
{
probSet.push_back(string(1,input1[i]));
i++;
}
}
while(input1[i]!=',');
}
}
|
I understand that affecting the loop counter unsigned i outside of the for loop condition is very iffy but code-tracing on my own revealed no obvious error.
Can you guys point out what I did wrong? Or if not can you guys give me an idea of what I should write instead? This is homework for a cs class but I'm really not able to find my error and it's really annoying me so please help me out here.