inputting format
i don't know why but my compiler reads only upto insert statement.
this is what compiler looks like:
nchy@ubuntu:~/Desktop$ g++ tree.cpp
nchy@ubuntu:~/Desktop$ ./a.out
2
insert 0 14 10 85 15 70 20 60 30 50 65 80 90 40 5 55
i want to implement following input format
2
insert 0 5 3 5 8 6 0
select 0 2
first line=> number of operations
second line=>
insert =>insert operation
0=> 0th tree
5=>5 insertions
then 5 numbers to be inserted
third line=>
select=>select operation
0=>0th tree
2=>2nd smallest element thus selecting 2nd smallest element in 0th tree.
this is what i tried to implement above input format.let me know where it went wrong
int no_of_trees,tree_no,no_of_insertions,number,k;
string s;
cin>>no_of_trees;
redblacktree<int> rb[no_of_trees];
getline(cin,s,' ');
for(int i=0;i<no_of_trees;i++)
{
if(s=="insert")
{
cin>>tree_no;
cin>>no_of_insertions;
for(int i=0;i<no_of_insertions;i++)
{
cin>>number;
rb[tree_no].insert(number);
}
}
if(s=="select")
{
cin>>tree_no;
cin>>k;
rb[tree_no].kthsmallest(k);
}
}
nchy13
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
whenver ye' mix formatted and unformatted types o' input, ye' are acksin' for trouble. try cin.ignore() after line #5.
flush your buffer.
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
can i avoid mixing it here
cin.ignore(); is not working
nchy13
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
i am using
using namespace std;
this is what it looks like after modification.
int no_of_trees,tree_no,no_of_insertions,number,k;
string s;
cin>>no_of_trees;
cout<<"no.of trees="<<no_of_trees<<endl;
redblacktree<int> rb[no_of_trees];
for(int i=0;i<no_of_trees;i++)
{
getline(cin,s,' ');
cout<<s<<endl;
if(s.compare("insert")==0)
{
cout<<"entered"<<endl;
cin>>tree_no;
cout<<"tree_no="<<tree_no;
cin>>no_of_insertions;
cout<<"no of insertions="<<no_of_insertions<<endl;
for(int i=0;i<no_of_insertions;i++)
{
cin>>number;
cout<<"number= "<<number<<endl;
rb[tree_no].insert(number);
}
}
if(s.compare("select")==0)
{
cin>>tree_no;
cout<<"tree_no="<<tree_no<<endl;
cin>>k;
cout<<"k="<<k<<endl;
rb[tree_no].kthsmallest(k);
}
}
this is what compiler shows
nchy@ubuntu:~/Desktop$ g++ tree.cpp
nchy@ubuntu:~/Desktop$ ./a.out
2
no.of trees=2
insert 0 1 1
insert
0
this is not even printing "entered" implies it doesn't pass if check.
can u tell me why it is printing 0 after insert.
and please let me know why it doesn't pass check.
nchy13
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
is something wrong with using getline();
nchy13
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
2 things:
1) stop using red -- it's annoying an unnecessary
2) format your code better. It's close to unreadable.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
sorry for inconvenience...
i provided this input
2
insert 0 1 1
i just wanted to say is that it is not passing the check if(s.compare("insert")==0)
int no_of_trees,tree_no,no_of_insertions,number,k;
string s;
cin>>no_of_trees;
cout<<"no.of trees="<<no_of_trees<<endl; //printing no. of trees or no. of operations
redblacktree<int> rb[no_of_trees];
for(int i=0;i<no_of_trees;i++) //starting for loop
{
getline(cin,s,' ');
cout<<s<<endl; //just to check what is there in string s
if(s.compare("insert")==0)
{
cout<<"entered"<<endl;
cin>>tree_no;
cin>>no_of_insertions;
for(int i=0;i<no_of_insertions;i++)
{
cin>>number;
rb[tree_no].insert(number);
}
}
if(s.compare("select")==0)
{
cin>>tree_no;
cin>>k;
rb[tree_no].kthsmallest(k);
}
} //for loop ends
nchy13
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
i got it. it is passing check after this modification
write
if(s.compare(1,6,"insert")==0)
instead of
s.compare("insert")==0)
i wanted to know what is stored at 0th index of string s as insert is stored starting from 1st index
can anyone help
nchy13
Junior Poster in Training
59 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1