-3

Now help me with this. Tell me where I am going wrong. I am making a simple movie-guessing game.

With check_movie() I am trying to update the movie from its temporary array after every guess.

I am getting the following error: incompatible types in assignment of char* to char[100] Its something with the check_movie() Where am I going wrong?

I am trying to return an array from any function for the first time. I am just a beginner. I googled a lot about returning arrays but the examples were out of my brain's reach.

#include<iostream.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>

void display_movie(char movie_temp[], int);
void display_life(int);
int win_player2(char movie_temp[]);
int check_life(char movie[], char, int);
char *check_movie(char movie[], char movie_temp[], char ch); //not sure about this func.

char *check_movie(char movie[], char movie_temp[], char ch)
{
    for(int i=0 ; movie[i]!='\0' ; i++)
    {
        if(movie[i]==ch)
        movie_temp[i]=movie[i];
    }

    return movie_temp;
}

void display_movie(char movie_temp[], int len)
{
    for(int i=0 ; i<len ; i++)
    cout<<movie_temp[i];
}

void display_life(int life)
{
    for(int i=0 ; i<=life ; i++)
       cout<<"\3";
}


int check_life(char movie[], char ch, int life) 
{
    int count1=0;
    for(int i=0 ; movie[i]!='\0' ; i++)
    {
        if(movie[i]==ch)
           count1++;
    }
    if(count1==0)
       return --life;
    else
       return life;
}

int win_player2(char movie_temp[])
{
    int count=0;
    for(int i=0 ; movie_temp[i]!='\0' ; i++)
    {
        if(movie_temp[i]=='_')
          count++;
    }
    if(count==0)
       return 0;
    else
       return 1;
}


int main()
{
    char movie[100], movie_temp[100], ch;
    cout<<"Enter the movie: ";
    cin.getline(movie,100);
    int len= strlen(movie);
    system("cls");


    for(int i=0 ; movie[i]!='\0' ; i++)
    {
        if(movie[i]=='a' || movie[i]=='e' || movie[i]=='i' || movie[i]=='o' || 
           movie[i]=='u' || movie[i]==' ')
              movie_temp[i]= movie[i];
    else
        movie_temp[i]='_';
    }

    int life=9;
    cout<<"\nLives left: ";
    display_life(life);


    while(life!=0 || win_player2(movie_temp)!=0)
    {
        cout<<"\n";
        display_movie(movie_temp, len);
        cout<<"\nEnter your guess: ";
        cin>>ch;
        life=check_life(movie, ch, life); //Here I update life
        //Updates life after every guess.

        movie_temp=check_movie(movie, movie_temp, ch);
        /*This part is getting me errors. I am trying to update movie_temp after every         
          guess.*/          


        cout<<"\n\nLives left: ";
        display_life(life);

   }
   getch();
   return 0;
}
lazygeek
  • 51
  • 1
  • 7
  • 1
    You are mixin C and C++. Why don't you define movies as `string` rather that as `char []` ? – hivert Feb 13 '14 at 16:09
  • `check_movie` should be `std::copy_if`. Also, `` is, and never was, standard and `` is meant for DOS. `display_movie` would be a simple `std::cout << something` with a `std::string`. `display_life` uses an invalid escape character. `check_life` duplicates `std::count`, but would be better off just using `std::find`. Same with `win_player2`. – chris Feb 13 '14 at 16:09
  • possible duplicate of [EDIT: Array return func movie\_check](http://stackoverflow.com/questions/21752905/edit-array-return-func-movie-check) – user1810087 Feb 13 '14 at 16:13
  • @hivert : I want to have access of individual character of movie. So I thought `char[]` would be a good idea. – lazygeek Feb 13 '14 at 16:16
  • @lazygeek, If you have a `std::string s`, `s[i]` does the same thing. – chris Feb 13 '14 at 16:17
  • @user1810087 : look at the comments bro. Everyone screwed my post. Now no one's interested in answering that even on my continuous edits. – lazygeek Feb 13 '14 at 16:17
  • and Thats what we are taught at school. I just started learning C++ in this grade. I have no idea about `std::` thing. I would appreciate if you had help me sorting out this problem using concept of pointers. – lazygeek Feb 13 '14 at 16:20
  • @Everyone. Thanks for showing your concern. m24p solved my problem. – lazygeek Feb 13 '14 at 16:24
  • 1
    @lazygeek: Unfortunately, you're being taught a dialect of C++ that's been obsolete for a couple of decades. If you want to learn C++, drop the course and get yourself a [good modern book](http://stackoverflow.com/questions/388242). If you don't, just drop the course. – Mike Seymour Feb 13 '14 at 16:28
  • @MikeSeymour Thats my school bro. Can't do anything about it. I will join some good college in 2 years probably. – lazygeek Feb 13 '14 at 16:30

2 Answers2

1

Don't return anything from movie check. Just update temp_array in the function. Arrays are not passed by value. See Why can't we pass arrays to function by value?

Community
  • 1
  • 1
m24p
  • 664
  • 4
  • 12
1

Look at your variable called movie_temp

char movie_temp[100]
// ...
movie_temp=check_movie(movie, movie_temp, ch);

Arrays can be thought of as hardcoded memory addresses. So this would be analagous to the following code....

5 = this_function_returns_an_int();

Which is obviously wrong. You can't assign to a constant. If you want your memory address to be an assignable variable, you need a pointer, rather than an array.

If I might suggest, do something like this for check_movie.

// Updates movie_temp[] with characters from movie[] matching ch.  Wheel Of Fortune style.
// return true if any characters matched ch.  return false otherwise.
bool check_movie(char movie[], char movie_temp[], char ch);
QuestionC
  • 10,006
  • 4
  • 26
  • 44