如果语句和星号密码破坏我的程序
if statement & asterisk password breaking my program
以下代码的目的是建立一个伪电影数据库,并能够通过两个查询搜索它。 程序本身一直工作,直到我也尝试放置用户名和密码(密码显示在屏幕上的星号)。 为了使用用户名和密码,我设置了一个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";
}
}
您正在将string
与char *
的操作混合在一起。您不需要一堆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]
元素。
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- 为什么我的 c++ 程序检查不是初始化的变量?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- C++ - 为什么我的程序在再次调用函数后关闭?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 我需要如何更改我的程序以使用打开/关闭原则?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较