The output of my code repeats the same number

The problem with my code is that it just gives an output of the "return -3" for example: "//User name exists, non zero rating
cout << getCountWatchedMovies("emma",users,5,4) <<endl;" expected is 2, and I got -3.
//User name exists, non zero rating
cout << getCountWatchedMovies("DaNieL",users,5,4) <<endl; expected 3, and I got 3.
for all the test cases my code gets -3 regardless

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
#include <iostream>

using namespace std;

class Movie
{
        private:
                string title,year;
                
        public:
                Movie();
                string getTitle();
                string getReleaseYear();
                void setTitle(string title);
                void setReleaseYear(string year);
                 
};

Movie::Movie()
{
        this->title = "Unknown";
        this->year  = "Unknown";
}

string Movie::getTitle()
{
        return (this->title);
}

string Movie::getReleaseYear()
{
        return (this->year);
}

void Movie::setTitle(string title)
{
        this->title = title;
}

void Movie::setReleaseYear(string year)
{
        this->year  = year;
}

#define MAX 64

class User
{
      
                
        public:
                User();
                string username;
                string getUsername();
                int numRatings,ratings[MAX];
                int getNumRatings();
                int getRatingAt(int ind);
                int getCountWatchedMovies();
                void setUsername(string username);
                void setNumRatings(int numRatings);
                void setRatingAt(int ind,int rating);
};

User::User()
{
        this->username   = "Unknown";
        this->numRatings = 0;
}

string User::getUsername()
{
        return (this->username);
}

int User::getNumRatings()
{
        return (this->numRatings);
}

int User::getRatingAt(int ind)
{
        if(ind>=0 && ind<numRatings)
        {
                return (this->ratings[ind]);
        }
        else
        {
                return 0;
        }
}

void User::setUsername(string username)
{
        this->username   = username;
        
}

void User::setNumRatings(int numRatings)
{
        this->numRatings = numRatings;
}

void User::setRatingAt(int ind,int rating)
{
        if(ind>=0 && ind<numRatings)
        {
                ratings[ind] = rating;
        }
        else
        {
                ratings[ind] = 0;
        }
};
//HERE IS WHERE THE PROBLEM IS:

int getCountWatchedMovies(string username,User users[],int numUsersStored,int numMoviesStored){
    for(int i=0;i<numUsersStored;i++){
        //means, user exits
        if(users[i].username == username){
            int count = 0; //number of movies watched
            for(int j=0;j<numMoviesStored;){
                //means user have watched movie/rated
                if(users[i].ratings[j] > 0)
                count++; //so increase count
            }
    
            //now return count (number of movies watched)
            return count;
        }
    
}
    //means no user found
   return -3;
}

Last edited on
You assume that you have users and what the name of one of them is. Prove it.

How about you do on line 116:
std::cout << "User " << i << ": #" << users[i].username << "# vs #" << username << "#\n";
(Or run with debugger and watch those variable at that point.)

That doesn't really solve my problem, I get:
Expected:
2
Got:
User 0: #Emma# vs #emma#
User 1: #Daniel# vs #emma#
User 2: #Rupert# vs #emma#
User 3: #Tom# vs #emma#
User 4: #Bonnie# vs #emma#
-3
You have five "User"s and none of them has name "emma".

C++ is case sensitive. 'E' is not 'e'. It is possible to make case insensitive comparison, but the string's operator== is case sensitive.
The thing is that I should not get a name as an output, just a numerical output, which is what I have the problem with always getting -3 instead of 4, 2, 3, etc. I'm using codeRunner for this.
We were not fixing the problem yet. We were just figuring out what the problem is.

The thing is that -3 is the correct answer, if you look for "emma". With "Emma" you would get something else.

[EDIT]: https://thispointer.com/c-case-insensitive-string-comparison-using-stl-c11-boost-library/
Last edited on
Topic archived. No new replies allowed.