0

以下代码的目的是建立一个伪电影数据库,并能够通过两个查询来搜索它。程序本身一直有效,直到我也尝试输入用户名和密码(密码在屏幕上显示为星号)。为了使用用户名和密码,我设置了一个 if 语句,这样 if (username == "user" && password == "word") 它会显示“Hello, user”。而不是预期的输出,我遇到了

Unhandled exception at 0x00cf3e36 in Movies.exe: 0xC0000005: Access violation writing location 0xcccccccc.

代码如下

//global variables
char title [20], y, n;
int year;
string search;
//function 1
void sort_on_title(movies_iit films[], int n)
{
//Local struct variable used to swap records
movies_iit temp;    

for(int i=0; i<n-1; i++)
    {
for(int i=0; i<n-1; i++)
        {
/*If s[i].title is later in alphabet than
s[i+1].title, swap the two records*/
        if(films[i].title>films[i+1].title)
            {
                temp = films[i];
                films[i] = films[i+1];
                films[i+1] = temp;
            }  
        }
    }
}
//end function 1
//function query1 prototype
void query1 (movies_iit movie);
//function query2 prototype
void query2 (movies_iit movie);
//function 2 prototype
void printmovie (movies_iit movie);
//beginning of main
int main ()
{
//login
//username: username
//password: password
string mystr;
int n;
char response;
string c[9];
string name;
cout << "enter your username "<<endl;
cin >> name;
cout << "enter your password "<<endl;
for (int i=0;i<9;i++)
{
c[i] = getch();
printf ("*");
}

cout << "\n" << endl;

if (name == "username" && c[9] == "password")
    cout << "Welcome, user." << endl;
else
{cout << "###" <<  "unrecognized username/password combination" << "\t" << "please try again"     << "###" << endl;
    system("PAUSE");
return 0;
    }
for (n=0; n<NUM_MOVIES; n++)
{
    cout << "Enter title: ";
    getline (cin,films[n].title);
    cout << "Enter year: ";
    getline (cin,mystr);
    stringstream(mystr) >> films[n].year;
}
//sort records, function 1 call
sort_on_title(films, NUM_MOVIES);
cout << "\nYou have entered these movies:\n";
for (n=0; n<NUM_MOVIES; n++) 
    printmovie (films[n]);  //function 2 call
//Query 1
cout << "Perform an alphabetical search? (y/n)" << endl;
cin >> response;

if (response == 'y')
    {cout << "Please enter title" << endl;
    cin >> title;
    for (n=0; n<NUM_MOVIES; n++)
        {query1 (films[n]);
            response == n;
        }
    }
else if (response == 'n')
    cout << "\n" << endl;
else
    cout << "invalid entry" << endl;
//Query 2
cout << "Perform a chronological search? (y/n)" << endl;
cin >> response;
//greater than
if (response == 'y')
{   cout << "greater than what year?" << endl;
    cin >> year;
    for (n=0; n<NUM_MOVIES; n++)
        {   query2 (films[n]);
        }
}
else if (response == 'n')
    cout << "Thank you, goodbye." << endl;
else
    cout << "invalid entry" << endl;
system("pause");
return 0;
}
//end of  main
//function 2 definition
void printmovie (movies_iit movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}
//function query1 defintion
void query1 (movies_iit movie)
{
  if (movie.title == title)
      {cout << " >> " << movie.title;
  cout << " (" << movie.year << ")\n";}
  else
      {cout << movie.title;
  cout << " (" << movie.year << ")\n";}
}
//function query2 definition
void query2 (movies_iit movie)
{
  if (movie.year >= year)
      {cout << movie.title;
        cout << " (" << movie.year << ")\n";
      }
}

如何让我的程序正常运行?*如果重要的话,在我添加用户名和密码以及 if 语句的代码之前程序运行正常

下面发布的是原始版本(运行的那个)

// array of structures
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
#include <stdio.h>
#include <cctype>
using namespace std;

#define NUM_MOVIES 6
//structure
struct movies_iit{
    string title;
    int year;
} films [NUM_MOVIES];
//global variables
char title [20], y, n;
int year;
string search;
//function 1
void sort_on_title(movies_iit films[], int n)
{
//Local struct variable used to swap records
movies_iit temp;    

for(int i=0; i<n-1; i++)
    {
for(int i=0; i<n-1; i++)
        {
/*If s[i].title is later in alphabet than
s[i+1].title, swap the two records*/
        if(films[i].title>films[i+1].title)
            {
                temp = films[i];
                films[i] = films[i+1];
                films[i+1] = temp;
            }  
        }
    }
}
//end function 1
//function query1 prototype
void query1 (movies_iit movie);
//function query2 prototype
void query2 (movies_iit movie);
//function 2 prototype
void printmovie (movies_iit movie);
//beginning of main
int main ()
{
//login
//username: user
//password: word
string mystr;
int n;
char response;
string c[4];
string name;
for (n=0; n<NUM_MOVIES; n++)
{
    cout << "Enter title: ";
    getline (cin,films[n].title);
    cout << "Enter year: ";
    getline (cin,mystr);
    stringstream(mystr) >> films[n].year;
}
//sort records, function 1 call
sort_on_title(films, NUM_MOVIES);
cout << "\nYou have entered these movies:\n";
for (n=0; n<NUM_MOVIES; n++) 
    printmovie (films[n]);  //function 2 call
//Query 1
cout << "Perform an alphabetical search? (y/n)" << endl;
cin >> response;

if (response == 'y')
    {cout << "Please enter title" << endl;
    cin >> title;
    for (n=0; n<NUM_MOVIES; n++)
        {query1 (films[n]);
            response == n;
        }
    }
else if (response == 'n')
    cout << "\n" << endl;
else
    cout << "invalid entry" << endl;
//Query 2
cout << "Perform a chronological search? (y/n)" << endl;
cin >> response;
//greater than
if (response == 'y')
{   cout << "greater than what year?" << endl;
    cin >> year;
    for (n=0; n<NUM_MOVIES; n++)
        {   query2 (films[n]);
        }
}
else if (response == 'n')
    cout << "Thank you, goodbye." << endl;
else
    cout << "invalid entry" << endl;
system("pause");
return 0;
}
//end of  main
//function 2 definition
void printmovie (movies_iit movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}
//function query1 defintion
void query1 (movies_iit movie)
{
  if (movie.title == title)
      {cout << " >> " << movie.title;
  cout << " (" << movie.year << ")\n";}
  else
      {cout << movie.title;
  cout << " (" << movie.year << ")\n";}
}
//function query2 definition
vvoid query2 (movies_iit movie)
    {
  if (movie.year >= year)
      {cout << movie.title;
        cout << " (" << movie.year << ")\n";
      }
}
4

2 回答 2

2

您正在stringchar *. 您不需要一个数组string来存储密码,只需像使用名称一样读取它。如果您想读取getch用于该*事物的密码,请读入 achar[9]然后转换为string(仅 1 个字符串)。

顺便说一句,c[9]不存在,你想用它做什么?

顺便说一句 2:如果您将 9 个字符读入 achar[9]并且您没有将最后一个位置设置为0,那么当您尝试使用任何与字符串相关的函数时会发生不好的事情(更不用说"password"长度为 8 因此它无法匹配) .

于 2013-11-15T01:33:04.563 回答
0

问题在于这一行,以及您使用相同约定的所有行,但不理解它:

string c[9];

上面的意思不是“最多 9 个字符的字符串”,它的意思是“一个由 9 个字符串组成的数组,其字符数与您输入的字符数一样多”。在循环:

for (int i=0;i<9;i++){
   c[i] = getch();
}

您在每个字符串中输入 1 个字符(而不是在一个字符串中输入 9 个字符)。然后,当您尝试c[9]与“密码”进行比较时,程序会尝试访问c[9]不存在的元素。

于 2013-11-15T01:40:38.703 回答