Thread: i'm a beginner and i wrote this program but its not working

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    3

    i'm a beginner and i wrote this program but its not working

    Code:
    /*This program gets the amount of movies a person has seen in a 
    year and asks them to rate each one*/
    
    
    
    
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    main()
    {
    
    
    int ctr, numMovies, rating, favRating, leastRating;
    char movieName[40], favourite[40], least[40];
    
    
    favRating = 0;
    leastRating = 10;
    
    
    
    
    do
    
    
    {
                //This part is if someone entered a 0 or a negative number
    
    
            printf("How many movies have you seen this year?\n\n");
            scanf (" %d", &numMovies);
    
    
                if (numMovies < 1)
                {
                    printf("No movies! How can you rank them?\n\nTry again\n\n");
                }
    
    
    }
    
    
    while (numMovies < 1);
    
    
    for (ctr = 1; ctr <= numMovies; ctr++)
    
    
    {
                //This part asks for name and the rating of the movie
                
            printf("\nWhat was the name of the movie?");
            printf("\n (1 word only and include capitals)\n");
            scanf(" %s", movieName);
            printf("\n\nOn a scale of 1 to 10, what would \nyou rate it?\n");
            scanf(" %d", &rating);
    
    
                while ( rating > 10) /*This part is if someone enters a number 
                                        that's not between 1 and 10*/
                {
                    printf("That's not between 1 and 10!\n\n");
                    printf("Try again.\n\n");
                    scanf(" %d", &rating);    
                }
                
                while ( rating < 0)
                
                {
                    
                    printf("That's not between 1 and 10!\n\n");
                    printf("Try again.\n\n");
                    scanf(" %d", &rating);    
                }
            
            printf("\nEnter the name of the next movie\n\n\n");
    
    
                    /*This part calculates which movies 
                    were the most hated and the most loved by the user*/
    
    
                if ( rating > favRating)
                {
                    strcpy(favourite, movieName);
                    favRating = rating;
    
    
                }
    
    
    
    
                if ( rating < leastRating)
                {
                    strcpy(least, movieName);
                    leastRating = rating;
                }
    
    
    }
    
    
    
    
        printf("\nYour Least Favourite Movie was %s.\n\n", least);
    
    
            if(favourite != 'Inception') /*My favourite movie was Inception so 
                                            if they don't enter Inception this shows*/
            {
                printf("\nYour Favourite Movie was %s.\n\n", favourite);
            }
    
    
            else if(favourite == 'Inception') /*Here's where it's not working.
                                                Everytime I put in Inception and 
                                                rated it 10 the other one would still show up
                                                I don't know what's wrong*/
            {
                printf("\nYour Favourite Movie was %s.\nThat was my favourite movie too!\n\n", favourite);
    
    
            }
    
    
    
    
    return 0;
    }
    help please

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    > else if(favourite == 'Inception')
    Use strcmp to compare strings.
    For the same reason you used strcpy instead of assignment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    3
    i dont understand

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    > strcpy(least, movieName);
    When you did this, why didn't you try this wrong thing? least = movieName;

    > if(favourite != 'Inception')
    So to compare, use strcmp
    like
    strcmp(favourite,"Inception")
    I'll leave you to rtfm to find out what strcmp returns when strings match.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    on that strcmp trip.
    Code:
    if ( strcmp("me", m) == 0 )
    {
     // if true
     doSomething;
    }
    else
    {
     // if false
    doSomethingElse
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting a program I wrote in c++ to c
    By jaytounit in forum C Programming
    Replies: 5
    Last Post: 05-07-2012, 10:20 PM
  2. A little program I wrote for fun !
    By manasij7479 in forum C++ Programming
    Replies: 43
    Last Post: 07-25-2011, 02:53 AM
  3. Just wrote my first Curses program
    By guesst in forum Linux Programming
    Replies: 14
    Last Post: 04-02-2008, 10:44 AM
  4. filesplitting program I wrote
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-30-2007, 03:24 PM
  5. Can someone look over the program I wrote?
    By brooklyn in forum C++ Programming
    Replies: 10
    Last Post: 04-16-2006, 07:23 AM

Tags for this Thread