"No operator '!=' matches these operands."
Hey guys I'm writing a program and attempting to use a Sentinel but the "not equal to" operator != doesn't seem to be working for me.
Can anyone help me correct what is wrong within my code?
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
|
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
const int SENTINEL = -1;
string team1;
int totalScore;
int points;
int score;
int count;
int topScore;
double countPoints;
double numberPoints;
double avgScore;
count = 0;
totalScore = 0;
while (team1 != SENTINEL)
cout << "Enter the name of the team: " << endl;
cin >> team1;
cout << endl;
cout << "Enter the score of the team: " << endl;
cin >> score;
cout << endl;
cout << team1 << " scored " << score << " points." << endl;
cout << endl;
cout << "When finished, enter " << SENTINEL << " to stop." << endl;
cout << endl;
{
totalScore += score;
count++;
}
if (score > topScore)
{
topScore = score;
}
if (count > 0)
{
avgScore = totalScore / count;
}
points = topScore - avgScore;
cout << "The highest number of points scored was " << topScore << endl;
cout << "The average number of points scored was " << avgScore << endl;
cout << "The highest number of points scored was " << points << " more than the average score." << endl;
return 0;
}
|
Look at the types of the things you are trying to compare. One is an int
, the other is an std::string
.
Should I simply change it to
1 2 3
|
while (score != SENTINEL)
|
To fix?
EDIT:
When I did this it gave me 2 different errors for uninitialized local variables for 'score' and 'topScore'
Last edited on
Okay I changed the code to
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
|
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
const int SENTINEL = -1;
string team1;
int totalScore;
int points;
int score;
int count;
int topScore;
double countPoints;
double numberPoints;
double avgScore;
count = 0;
totalScore = 0;cout << "Enter the name of the team: " << endl;
cin >> team1;
cout << endl;
cout << "Enter the score of the team: " << endl;
cin >> score;
cout << endl;
cout << team1 << " scored " << score << " points." << endl;
cout << endl;
cout << "When finished, enter " << SENTINEL << " in place of score to stop." << endl;
cout << endl;
while (score != SENTINEL)
{
totalScore += score;
count++;
if (score > topScore)
{
topScore = score;
}
if (count > 0)
{
avgScore = totalScore / count;
}
}
points = topScore - avgScore;
cout << "The highest number of points scored was " << topScore << endl;
cout << "The average number of points scored was " << avgScore << endl;
cout << "The highest number of points scored was " << points << " more than the average score." << endl;
return 0;
}
|
But now I am getting an uninitialized local variable error for 'topScore' in
int topScore;
Should be :
int topScore = 0;
Topic archived. No new replies allowed.