如果语句和星号密码破坏我的程序

if statement & asterisk password breaking my program

本文关键字:我的 程序 密码 语句 如果      更新时间:2023-10-16

以下代码的目的是建立一个伪电影数据库,并能够通过两个查询搜索它。 程序本身一直工作,直到我也尝试放置用户名和密码(密码显示在屏幕上的星号)。 为了使用用户名和密码,我设置了一个if语句,使得if(用户名=="用户"&&密码=="单词")它将显示"你好,用户"。 而不是预期的输出,我遇到了

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";
      }
}

您正在将stringchar *的操作混合在一起。您不需要一堆string来存储密码,只需像使用名称一样读取它即可。如果要使用 getch 读取密码*,请读入char[9],然后转换为 string(仅 1 个字符串)。

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

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

问题出在这一行,以及你使用相同约定的所有行,而不理解它:

string c[9];
以上并不意味着"最多 9 个字符的字符串

",它意味着"一个包含 9 个字符串的数组,其中包含您输入的字符数"。在循环中:

for (int i=0;i<9;i++){
   c[i] = getch();
}
您在每个字符串中输入

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

相关文章: