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);
}
}

Recommended Answers

All 10 Replies

whenver ye' mix formatted and unformatted types o' input, ye' are acksin' for trouble. try cin.ignore() after line #5.

flush your buffer.

can i avoid mixing it here
cin.ignore(); is not working

Member Avatar for jmichae3

string.getline() docs
string.compare() docs
string docs
iostream docs


is the docs for std::cin.getline() or cin.getline() depending on whether or not you use using namespace std;
.getline() uses char arrays unfortunately, but you can always convert them to type string. maybe they fixed this in c++11 (c++0x), they did with fstream filepaths.


it is not possible without modifying operator== to compare with char* C strings, at least it's not built into string (it should be).
yeah, I know, it's what it should be. you weren't able to do it before in C, and you aren't able to do it by default with C++ either.

as for s, you should be using

if (0==s.compare("insert")) {

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.

is something wrong with using getline();

2 things:
1) stop using red -- it's annoying an unnecessary
2) format your code better. It's close to unreadable.

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

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

Member Avatar for jmichae3

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

this is a CASE SENSITIVE compare. perhaps you have a compiler bug or you have redefined string? some of your code is not STL, notably the red black tree. perhaps you are using boost?

it is always safer to do == comparisons with the constant on the left hand side. it will flag a compiler error if you miss an = and turn it into an assignment by accident.
otherwise you have strange unexplainable values, your program doesn't work right and you don't know why and you have long debug sessions.

If I could see your full code, I could debug it, but as it is, I don't, so I can't help you. I have not had problems with compare using gcc (mingw-w64 auto build).

#include <string>
#include <iostream>
using namespace std;
int main(void) {
    string s("insert");
    if (0==s.compare("insert")) {
        cout<<"matches"<<endl;
    } else {
        cout<<"no match"<<endl;
    }
    return 0;
}
//Fri 11/04/2011 15:19:01.73|C:\prj\test\mingw-w64\compare|>compare
//matches

so it's something wrong with your input.

#include <string>
#include <iostream>
using namespace std;
int main(void) {
    string s;
    cout<<"type in insert and hit enter"<<endl;
    cin>>s;//"insert"
    if (0==s.compare("insert")) {
        cout<<"matches"<<endl;
    } else {
        cout<<"no match"<<endl;
    }
    return 0;
}
Fri 11/04/2011 15:23:00.97|C:\prj\test\mingw-w64\compare|>compare
type in insert and hit enter
insert
matches
Member Avatar for jmichae3

I am sorry, it's not string::getline() it's cin.getline() read the documentat5ion I gave anyway.

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.