以下代码的目的是设置伪电影数据库,并能够使用两个查询进行搜索。程序本身一直有效,直到我还试图将用户名和密码放到位(密码显示在屏幕上用星号)。为了使用用户名和密码,我设置了一个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";
}
}
答案 0 :(得分:2)
您正在将string
与char *
的操作混合在一起。您不需要string
数组来存储密码,只需像读取名称一样阅读它。如果您想使用getch
来*
读取密码,请先阅读char[9]
,然后转换为string
(仅限1个字符串)。
BTW,c[9]
不存在,你想用它做什么?
顺便说一句2:如果你将9个字符读入char[9]
并且你没有将最后一个位置设置为0
,当你尝试使用任何与字符串相关的函数时会发生不好的事情(不是提到"password"
的长度为8,因此无法匹配。)
答案 1 :(得分:0)
问题出在这一行,以及你使用相同约定的所有行,而不理解它:
string c[9];
以上并不意味着“最多9个字符的字符串”,它表示“包含与您输入的字符数相同的9个字符串的数组”。在循环中:
for (int i=0;i<9;i++){
c[i] = getch();
}
在每个字符串中输入1个字符(一个字符串中不是9个字符)。然后,当您尝试将c[9]
与“密码”进行比较时,程序会尝试访问不存在的c[9]
元素。