gpt4 book ai didi

c++ - if 语句和星号密码破坏了我的程序

转载 作者:行者123 更新时间:2023-11-28 03:04:54 26 4
gpt4 key购买 nike

以下代码的目的是建立一个伪电影数据库,并能够通过两个查询来搜索它。该程序本身一直有效,直到我还尝试输入用户名和密码(屏幕上显示的密码带有星号)。为了使用用户名和密码,我设置了一个 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";
}
}

最佳答案

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

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

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

关于c++ - if 语句和星号密码破坏了我的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19991879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com