comparing strings with space

Dec 3, 2011 at 4:18am
why doesnt this code work if i try to compare two identical strings. If i run it without the cin it works, but with the cin it doesnt. I made the to test this issue that i am coming across in a larger program, any help is appreciated, thanks!

#include <iostream>
#include <string>
using namespace std;


int main(){
string a= "hi there";
string b= "hi there";
cout << "input to test";
cin >> b;
if (a==b){
cout << "good"<<endl;
}
else{
cout << "bad"<<endl;
}
}

Dec 3, 2011 at 4:23am
don't initialize b, in other words:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main()
{
string a= "hi there", b;

cout << "input to test";
getline(cin, b);

if (a == b)
{
cout << "good"<<endl;
}

else
{
cout << "bad"<<endl;
}
}


im sorry i found the real reason
Last edited on Dec 3, 2011 at 4:23am
Dec 3, 2011 at 4:24am
so cin>> for a string can only take a single char. so to fix that with strings use getline(cin, var_name);
Dec 3, 2011 at 4:25am
but still don't initialize b if you don't need to
Dec 3, 2011 at 10:36am
cin>>operator by default split the sentence into words using white space as separators so there is no way you can use cin>> to pass "hi there" as a string to your b.

That's why you need to use getiline
Dec 3, 2011 at 5:04pm
he means getline
Dec 3, 2011 at 5:19pm
I just was initializing b to test something else. the getline works well. but for some reason it isnt working in my library code. When it passes right over the getline and outputs "Please enter the title: Is by the correct book? Y/N".


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int get_array_length(ifstream & in);
void fill_array(ifstream & in, int isbnarray[], string titlearray[], string author[], char availarray[]);
int search_isbn(int isbn_input, int isbnarray[], int size);
int search_title(string title_input, string titlearray[], int size);
void check_avail(int index_num, char availarray[], string titlearray[], string author[]);

int main(){
int x = 0, isbn_input, index_num, size, selection, cardnum, temp = 0;
string title_input;
char yesno;
ifstream in;
ifstream idin;
size = get_array_length(in);
int isbnarray[size];
string titlearray[size];
string author[size];
char availarray[size];
fill_array(in, isbnarray, titlearray, author, availarray);
/*while (temp != 1){
cout << "Please enter you library card number (between 1000-9999): ";
cin >> cardnum;
while (cardnum<1000 || cardnum>9999){
cout<< "INVALID CARD NUMBER: Please enter again between 1000-9999: ";
cin >> cardnum;
}
idin.open("/Temp/iddata.txt");
while (!idin.eof()){
idin >> temp;
if (temp == cardnum){
temp = 1;
break;
}
}
idin.close();
}*/
while (x<4){
string titletest;
selection = 0;
index_num = 0;
cout << "\n\nWelcome to Wentworth Library's search engine.\n\n"
"1. Search by ISBN#\n"
"2. Search by Book Title\n"
"3. Exit"<<endl;
cin >> selection;
switch (selection){
case 1:
cout << "\nPlease enter the ISBN #: ";
cin >> isbn_input;
index_num = search_isbn(isbn_input, isbnarray, size);
if (index_num ==-1){
cout << "Invalid ISBN"<< endl;
break;
}
cout << "Is "<<titlearray[index_num]<< " by "<<author[index_num]<<" the correct book? Y/N"<<endl;
cin >> yesno;
if (yesno == 'Y' || yesno == 'y'){
check_avail(index_num, availarray, titlearray, author);}
break;
case 2:
cout << "\nPlease enter the title: ";
cin >> title_input;
getline (cin,title_input);
index_num = search_title(title_input, titlearray, size);
if (index_num ==-1){
cout << "Invalid title"<< endl;
break;
}
cout << "Is "<<titlearray[index_num]<< " by "<<author[index_num]<<" the correct book? Y/N"<<endl;
cin >> yesno;
if (yesno == 'Y' || yesno == 'y'){
check_avail(index_num, availarray, titlearray, author);}
break;
case 3:
cout << "\nEXIT: Y/N" <<endl;
cin >> yesno;
if (yesno == 'Y' || yesno == 'y'){
exit(0);
}
break;
}
}
return 0;
}

int get_array_length(ifstream & in){
int x = 0;
char line[1024];
in.open("/Temp/database.txt");
do {
in.getline(line, 1024);
x++;
}
while (in.good());
in.close();
return x;
}
void fill_array(ifstream & in, int isbnarray[], string titlearray[], string author[], char availarray[]){
in.open("/Temp/database.txt");
int count = 0;
char a[128], b[128], c[128], d[2];
while (!in.eof()){
for (int i =1; i<5; i++){
switch (i){
case 1: in.getline(a,128,'\t');
isbnarray[count]= atoi(a); break;
case 2: in.getline(b,128,'\t');
titlearray[count] = b; break;
case 3: in.getline(c,128,'\t');
author[count] = c; break;
case 4: in.getline(d,2,'\n');
availarray[count]=d[0]; break;
}
}
count++;
}
in.close();
}
int search_isbn(int isbn_input, int isbnarray[], int size){
int x=0, y = 0;
for (int i=0; i<size;i++){
if (isbn_input == isbnarray[i]){
x=i;
i = size;
y = 1;
}
}
if (y != 1){
x = -1;
}
return x;
}
int search_title(string title_input, string titlearray[], int size){
int x=0,y = 0;
for (int i=0; i<size;i++){
if (title_input == titlearray[i]){
x=i;
i=size;
y = 1;
}
}
if (y != 1){
x = -1;
}
return x;
}
void check_avail(int index_num, char availarray[], string titlearray[], string author[]){
if (availarray[index_num] == 'Y' || 'y'){
cout << "\n"<<titlearray[index_num]<<" by "<<author[index_num]<<" is available";
}
else{
cout << "\n"<<titlearray[index_num]<<" by "<<author[index_num]<<" is not available";
}
}


I made up a fake input file to test with (database.txt):

54325 titlea authora y
64324 titleb asADSFOI authorb sdafoihf n
1 titlec asdf authorc dsfkj n
4 sebastian junger authord n
2 In the heart of the Sea Nathaniel Philbrick y
6532 titlef f authorf fFF n


any ideas?
Dec 3, 2011 at 9:05pm
I needed to flush the input stream. So i put cin.ignore(1000, '\n'); in front of the getline. It works now, thanks for everyones help!

Matt
Topic archived. No new replies allowed.