String comparison

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;

const int size = 200;
const int c = 200, r = 6;
string str[size];
int score[c][r];
int begin, end;

void value(int[][r], const int, const int);
void displayt(int [][r], int, int);
string genrandom();
void placestring();
void prompt(int&, int&);
void displayn(int&, int&);
void indepth(int&, int&);


int main()
{
	char choice;
	do
	{
		
		value(score, c, r);
		genrandom();
		placestring();
		displayt(score, c, r);
		prompt(begin, end);
		displayn(begin, end);
		indepth(begin, end);
		
		cout << "\nExit: <y or no> _ ";
		cin >> choice;
	}
	while(choice != 'y');
	return 0;
}

void indepth(int &b, int &e)
{
	string name;
	cout << "\nEnter name to see position: ";
    cin >> name;
	for (int i = b; i < (e+1); i++)
	{
		if(name == str[i])
		{
			cout << "Position: " << i;
			break;
		}
	}
	cout << "\nNot Found..";
}

void displayn(int &b, int &e)
{
	for(int i = b; i < (e+1); i++)
	{
		cout << endl;
		cout << i << ".  ";
		cout << str[i];
	}
}
void prompt(int &b, int &e)
{
	cout << "\nData Entry <> _ ";
	cin >> b >> e;
}

void value(int score [][r], const int column, const int row)
{
	for(int i = 0; i < column; i++)
	{
		for(int j = 0; j < row; j++)
		{
			score[i][j] = 0 + rand() % 101;
		}
	}
}
string genrandom()
{
	int L = 4 + rand() % 4;
	string strung = "       ";
	for(int i = 0; i < L; i++)
	{
		strung[i] = 'a' + rand() % 26;
	}
	return strung;
}
void placestring()
{
	for(int i = 0; i < size; i++)
	{
		str[i] = genrandom();
	}
}
void displayt(int x[][r], const int column, const int row)
{
	for(int i = 0; i < column; i++)
	{
		cout << i << ".  ";
		cout << str[i] << "\t\t";
		for(int j = 0; j < row; j++)
		{
			cout << x[i][j] << "\t";
			if(j == (row-1))
			{
				cout << endl;
			}
		}
	}
}



Data Entry <> _ 120 140

120.  rswsaz
121.  moijo
122.  vvgo
123.  qpnck
124.  vnhkeb
125.  tdhvyg
126.  jisu
127.  hatmu
128.  dqbh
129.  knhf
130.  axqxkjl
131.  zqtsj
132.  aeedf
133.  ujko
134.  xjoqk
135.  vfepvlh
136.  hrwtf
137.  ukxffjp
138.  swyxli
139.  jhevryx
140.  zbafpf
Enter name to see position: ujko

Not Found..
Exit: <y or no> _


Why is the string comparison not working? "ujko" should be found and the position 133 should be displayed.
The == operator is not sufficient compare values of strings.
To compare strings, you need to use the strcmp function.
1
2
3
4
5
std::string a = "Hi";
std::string b= "Bye";

if ( strcmp(a.c_str(), b.c_string()) == 0 ) std::cout << "Same";
else std::cout << "Different";


Alternatively, you could overload the == operator for strings if you need to compare often.
Last edited on
Wrong. With std::string you can compare using the == operator.

I think there's a problem with garbage white space in the cin buffer at the end of your prompt function. Try cin.ignore(...) (http://www.cplusplus.com/reference/istream/istream/ignore/) before reading name to see if that clears things up.
cin.ignore()
did not work . I placed it before cin >> name;
it is still saying not found.
First, I was certainly wrong about the equality operator not being defined.
Second, I found your problem. It lies in your genrandom() function. You give strung an initial value of eight spaces, and then you change the character in each of those places. The problem is, however, that if the resulting length you randomly generate is less than eight spaces, it will be padded with whitspace at the end, so when you check for "ujko" you're actually comparing it to "ujko " which are not equivalent strings.

This is what you should actually be using:
1
2
3
4
5
6
7
8
9
10
11
string genrandom()
{
	int L = 4 + rand() % 4;
	string strung;
	for(int i = 0; i < L; i++)
	{
		//append a random character to the end of the string
		strung.push_back('a' + rand() % 26);
	}
	return strung;
}
It worked, but can you explain the use of the push_back ?
.push_back() is a string class function that takes one char parameter which appends itself to the end of the string.

Let's say string1 is an empty string, and you call string1.push_back( 'a' );, now string1 == "a". Call string1.push_back( 'b' ), now string1 == "ab" and so on.

So what the code is doing is taking an empty string, and appending up to 8 characters to it (e.g. how many the loop calls for) which means you do not have any excess whitespace but rather a string of correct size.

http://www.cplusplus.com/reference/string/string/push_back/
Last edited on
Thanks. I appreciate your help, and everyone else as well.
Topic archived. No new replies allowed.