Help fixing error message....

I am getting the following error message:



lastassignment2.cpp:51:21: warning: comparison of integers of different signs:
'int' and 'size_type' (aka 'unsigned long') [-Wsign-compare]
for(int aa=0;aa<str.length();aa++)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include<iostream>

	#include<string>

	#include<cmath>

	#include<cstdlib>

	#include<fstream>

	#include<iomanip>

	#include<algorithm>

	using namespace std;

	//phrase (main)

	struct Phrase

	{

	     //

	     string text;

	     //characters in string are distinctive

	     string::size_type guessesRequired;

	     //lets know if phrase is used

	     bool isUnused;

	};

	int maxphrasecnt=100;

	/* Number of distinct characters in string */

	int distinctCount(const string &str)

	{

	     string uniqueString="";

	     int kk=-1;

	     /*Introduce loop used to find string */

	     for(int aa=0;aa<str.length();aa++)

	     {



	          if(uniqueString.find(toupper(str[aa]))==string::npos && uniqueString.find(tolower(str[aa]))==string::npos)

	              uniqueString+=str[aa];

	     }

	     /* Length of dinstinct character string */

	     return uniqueString.length();

	}

	/* Download phrases*/

	void loadPhrases(string filename, struct Phrase phrase[], int &phraseCount)

	{

	     //Open File

	     ifstream ff(filename);

	     string ph;

	     //Read from file

	     while(getline(ff,ph) && phraseCount<=maxphrasecnt)

	     {

	          phrase[phraseCount].text=ph;

	          phrase[phraseCount].guessesRequired=distinctCount(ph);

	          phrase[phraseCount].isUnused=false;

	          phraseCount++;

	     }

	     //Close file

	     ff.close();

	}

	//arange phrases

	void sortPhrases(struct Phrase phrase[], int phraseCount)

	{

	     int ii, jj, minIndx;

	     string tmp;

	     bool used;

	     string::size_type guess;

	      for (ii = 0; ii < phraseCount - 1; ii++)

	     {

	            minIndx = ii;

	            for (jj = ii + 1; jj < phraseCount; jj++)

	                  if (phrase[jj].guessesRequired < phrase[minIndx].guessesRequired)

	                        minIndx = jj;

	            if (minIndx != ii) {

	                  tmp = phrase[ii].text;

	                   guess=phrase[ii].guessesRequired;

	                   used=phrase[ii].isUnused;

	                   phrase[ii].text=phrase[minIndx].text;

	                   phrase[ii].guessesRequired=phrase[minIndx].guessesRequired;

	                   phrase[ii].isUnused=phrase[minIndx].isUnused;

	                   phrase[minIndx].text=tmp;

	                   phrase[minIndx].guessesRequired=guess;

	                   phrase[minIndx].isUnused=used;



	            }

	      }

	}

	//print our phrases

	void printPhrases(struct Phrase phrase[], int phraseCount)

	{

	     for (int ii = 0; ii < phraseCount ; ii++)

	     {

	          if(phrase[ii].isUnused==0)

	              cout<< setw(50)<<left<<phrase[ii].text<<setw(5)<<phrase[ii].guessesRequired<<setw(10)<<"unused"<<endl;

	          else

	              cout<< setw(50)<<left<<phrase[ii].text<<setw(5)<<phrase[ii].guessesRequired<<setw(10)<<"used"<<endl;

	     }

	}

	//main

	int main()

	{

	     struct Phrase phrase[100];

	     int phraseCount=0;

	     /*Load in phrases */

	     loadPhrases("phrases.txt",phrase,phraseCount);

	     /*Sort after printing */

	     cout<<"\n The phrases are:"<<endl;

	     printPhrases(phrase,phraseCount);

	     /*Sort*/

	     sortPhrases(phrase,phraseCount);

	     cout<<"Phrases after sorted by guesses Required:"<<endl;

	     /*Print */

	     printPhrases(phrase, phraseCount);

	     system("pause");

	     return 0;

	}
Because string.length() returns size_t which is an unsigned integral type, so to compare like-for-like variable aa should also be of type size_t
also there's an unused variable in this part of the program, kk, line 47
Did you try to run the code? I am not sure what you are telling me to do...Would you mind rewriting the code so that it compiles?
Last edited on
I am not sure what you are telling me to do

i'm telling you why you are getting the warning messages that you alluded to at the top of your OP
Would you mind rewriting the code so that it compiles?

the program compiles as it stands but whether or not it does what its supposed to is your job to find out
To avoid those warnings, you could modify the line 51 from
for(int aa=0;aa<str.length();aa++)
to
for(string::size_type aa=0; aa<str.length(); aa++)
and delete the line 47: int kk=-1;
However, it changes very little.

Even make the code more or less readable doesn't change a lot, but you can evaluate if the following style suit your needs or not:
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
118
119
120
121
122
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>

struct Phrase
{
   std::string text;
   //characters in string are distinctive
   std::string::size_type guessesRequired;
   //lets know if phrase is used
   bool isUnused;
};

/* Number of distinct characters in string */
const int MAXPHRASECNT = 100;

int loadPhrases(std::string filename, struct Phrase phrase[]);
int distinctCount(const std::string& str);
void printPhrases(struct Phrase const phrase[], int phraseCount);
void sortPhrases(struct Phrase phrase[], int phraseCount);

int main()
{
   struct Phrase phrase[100];

   /*Load in phrases */
   int phraseCount = loadPhrases("phrases.txt", phrase);
 
   std::cout << "\nThe phrases are: \n";
   std::cout << "\n============================\n";
   printPhrases(phrase, phraseCount);
   /*Sort*/
   sortPhrases(phrase,phraseCount);
   std::cout << "\n\n\nPhrases after sorted by guessesRequired:\n";
   std::cout << "\n============================\n";
   printPhrases(phrase, phraseCount);

   std::cout << "\n\nPress ENTER to continue...\n";
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   return 0;
}

/* Download phrases*/
int loadPhrases(std::string filename, struct Phrase phrase[])
{
   //Open File
   std::ifstream ff(filename);
   std::string ph;
   int ph_count {0};
   //Read from file
   while(std::getline(ff,ph) && ph_count<=MAXPHRASECNT)
   {
      phrase[ph_count].text=ph;
      phrase[ph_count].guessesRequired = distinctCount(ph);
      phrase[ph_count].isUnused=false;
      ph_count++;
   }
   //Close file
   ff.close();
   
   return ph_count;
}

int distinctCount(const std::string& str)
{
   std::string uniqueString;

   for(std::string::size_type aa=0; aa<str.length(); aa++)
   {
      if(uniqueString.find(std::toupper(str[aa]))==std::string::npos 
         && uniqueString.find(std::tolower(str[aa]))==std::string::npos) {
         uniqueString += str[aa];
      }
   }
   
   /* Length of dinstinct character string */
   return uniqueString.length();
}

//print our phrases
void printPhrases(struct Phrase const phrase[], int phraseCount)
{
   for (int ii = 0; ii < phraseCount; ii++)
   {
      std::cout << std::setw(50) << std::left << phrase[ii].text
                << std::setw(5) << phrase[ii].guessesRequired
                << std::setw(10) << std::boolalpha << phrase[ii].isUnused 
                << std::endl;
   }
}

//arange phrases
void sortPhrases(struct Phrase phrase[], int phraseCount)
{
   for (int ii = 0; ii < phraseCount - 1; ii++)
   {
      int minIndx = ii;
      for (int jj = ii + 1; jj < phraseCount; jj++) {
         if (phrase[jj].guessesRequired < phrase[minIndx].guessesRequired) {
            minIndx = jj;
         }
      }

      if (minIndx != ii) {
         std::string tmp = phrase[ii].text;
         std::string::size_type guess = phrase[ii].guessesRequired;
         bool used=phrase[ii].isUnused;

         phrase[ii].text=phrase[minIndx].text;
         phrase[ii].guessesRequired=phrase[minIndx].guessesRequired;
         phrase[ii].isUnused=phrase[minIndx].isUnused;

         phrase[minIndx].text = tmp;
         phrase[minIndx].guessesRequired = guess;
         phrase[minIndx].isUnused = used;
      }
   }
}

Topic archived. No new replies allowed.