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

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
 

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")) {
jmichae3
Junior Poster
105 posts since Jul 2011
Reputation Points: 14
Solved Threads: 11
 

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
Moderator
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
 

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
jmichae3
Junior Poster
105 posts since Jul 2011
Reputation Points: 14
Solved Threads: 11
 

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

jmichae3
Junior Poster
105 posts since Jul 2011
Reputation Points: 14
Solved Threads: 11
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You