首页 > 解决方案 > 第二次使用findMovie函数,Visual Studio抛出异常

问题描述

Visual Studio 在案例 c 下对 find movie 的第二个实例抛出异常,它表示它尚未初始化,尽管在本地窗口中它显示了包含内部信息的结构?运行时检查失败 #3 - 变量“tempMovie”正在使用而未初始化。这仅在我第二次尝试使用 struct tempMovie 时发生

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define CONST 100
typedef struct movies {
    char title[30];
    char UPC[12];
    int qnty;
    double price;
}movies;
void sortMovies(struct movies main[], int nOfMovies);
void changeMovie(struct movies main);
int findMovie(struct movies main, int nOfMovies, struct movies tempMovie);
struct movies newMovie();


int main()
{
    int nOfMovies = 0, findR, stop = 0, nOfMoviesArr[CONST];
    char decider;
    while (stop != 1)
    {
        movies main[CONST];
        movies tempMovie;
        printf("(A)dd a new movie\n(C)hange a Movie's Information \n(D)elete a Movie \n(L)ist All Movies\n(Q)uit\n\n");
        scanf(" %c", &decider);

        switch (decider)
        {
        case 'a':
        case 'A':
            tempMovie = newMovie();
            findR = findMovie(main[nOfMovies], nOfMovies, tempMovie);
            if (findR == -1)
            {
                nOfMovies += findR;
                break;
            }
            else
            main[nOfMovies] = tempMovie;
            nOfMovies++;
            break;
        case 'c':
        case 'C':
            findR = findMovie(main[nOfMovies], nOfMovies, tempMovie);
            changeMovie(main[findR]);
            break;
        case 'd':
        case 'D':

            break;
        case 'l':
        case 'L':
            sortMovies(main, nOfMovies);
            break;
        case 'q':
        case 'Q':
            return 0;
            break;
        default:
            printf("An invalid option was selected!");
        }
    }


}

struct movies newMovie() {
    movies new;

    printf("enter movie upc:");
    scanf("%s", &new.UPC);
    printf("enter movie title:");
    scanf("%s", &new.title);
    printf("enter movie qauntity:");
    scanf("%d", &new.qnty);
    if (new.qnty <= 0)
    {
        printf("quanitity must be greater than 0");
        printf("enter movie qauntity:");
        scanf("%d", &new.qnty);
    }

    printf("enter movie price:");
    scanf("%lf", &new.price);
    if (new.price <= 0)
    {
        printf("price must be greater than 0");
        printf("enter movie price:");
        scanf("%lf", &new.price);
    }
    return new;
}


int findMovie(struct movies main, int nOfMovies, struct movies tempMovie)
{

    int count;
    for (count = 0; count < nOfMovies; count++)
    {
        if (tempMovie.UPC != main.UPC)
        {
            return count;
        }
        else
        {
            printf("error");
            return -1;
        }
    }

}
void changeMovie(struct movies main)
{
    char decider;

    printf("would you like to change the value? y or no input");
    switch (decider)
    {
    case 'y':
    case 'Y':
        printf("enter movie title:");
        scanf("%c", main.title);
        printf("enter movie qauntity:");
        scanf("%d", main.qnty);
        if (main.qnty <= 0)
        {
            printf("quanitity must be greater than 0");
            printf("enter movie qauntity:");
            scanf("%d", main.qnty);
        }

        printf("enter movie price:");
        scanf("%lf", main.price);
        if (main.price <= 0)
        {
            printf("price must be greater than 0");
            printf("enter movie price:");
            scanf("%lf", main.price);
        }
        break;
    default:
        printf("An invalid option was selected!");



    }

}
void sortMovies(struct movies main[], int nOfMovies)
{
    int i, sflag, count = 0;
    char* temp;

    do
    {
        sflag = 0;
        for (i = 1; i < nOfMovies; i++)
        {
            if (main[i - 1].title > main[i].title)
            {
                temp = main[i - 1].title;
                strcpy(main[i - 1].title, main[i].title);
                strcpy(main[i].title, temp);
                sflag = 1;
            }
        }
        count++;
    } while (sflag);

    for (i = 0; i < nOfMovies; i++)
    {
        printf("%s\t%s\t%d\t%.2lf\n", main[i].title, main[i].UPC, main[i].qnty, main[i].price);
    }


}```




标签: cfunctionstruct

解决方案


错误可能是由于以下任何或多种原因造成的:

  1. 在 changeMovie() 中,不扫描变量决定器。

  2. "%c"在 scanf() 中,使用and之前省略的空格"%s"(使用" %c"and " %s"

  3. 在 changeMovie() 中,扫描时出现语法错误(使用&main.qnty代替main.qnty

  4. 函数 changeMovie() 正在更改形式参数而不是更改实际参数

此外,还有一些逻辑错误。例如,在行中应该使用 a 而if (new.price <= 0)不是whileif


推荐阅读