C++;输入到结构:_getch() 在C++中,我试图输入电影的名字和年份,并将它们存储在数据库/结构中。在我要求输入标题和年份之前。我已使用凭据让用户登录。在本例中,用户名为“rusty”,密码为“rusty”
我遇到的问题是,在验证凭据后,将跳过要输入到数据库/结构中的第一个电影标题。我相信这与我使用C++;输入到结构:_getch() 在C++中,我试图输入电影的名字和年份,并将它们存储在数据库/结构中。在我要求输入标题和年份之前。我已使用凭据让用户登录。在本例中,用户名为“rusty”,密码为“rusty”,c++,database,getch,C++,Database,Getch,我遇到的问题是,在验证凭据后,将跳过要输入到数据库/结构中的第一个电影标题。我相信这与我使用\u getch函数有关,但我不能完全确定 我的代码如下。我的输出如下所示: Please enter your username rusty Please enter your password Access granted! Welcome rusty Enter title: Enter year: (input movie year) Enter title: (input mov
\u getch
函数有关,但我不能完全确定
我的代码如下。我的输出如下所示:
Please enter your username
rusty
Please enter your password
Access granted! Welcome rusty
Enter title: Enter year: (input movie year)
Enter title: (input movie title)
Enter year: (input movie year)
Enter title: (input movie title)
....
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
//function prototype
int username_and_pass();
#define NUM_MOVIES 6
struct movies_list{
string title;
int year;
}films[NUM_MOVIES];
// prototype with function declaration
void sort_on_title(movies_list films[], int n)
{
movies_list temp;
for (int i = 0; i < n - 1; i++)
{
if (films[i].title>films[i + 1].title)
{
temp = films[i];
films[i] = films[i + 1];
films[i + 1] = temp;
}
}
}// end of sort_on_title function
void printmovie(movies_list movie)
{
cout << movie.title;
cout << " (" << movie.year << ") \n";
}
void search_on_title(movies_list films[], int n, string title)
{
bool flag = false;
for (n = 0; n < NUM_MOVIES; n++)
{
if (films[n].title == title)
{
cout << "Title: " << films[n].title << endl;
cout << "Year of Release: " << films[n].year << endl;
cout << "\n";
flag = true;
}
}
if (flag == false)
cout << "Search on title not found!" << endl;
}// end of search_on_title function
void search_on_year(movies_list films[], int n, int year)
{
bool flag = false; // check on existence of record
for (n = 0; n < NUM_MOVIES; n++)
{
if (films[n].year == year) // display if true
{
cout << "Title: " << films[n].title << endl;
cout << "Year of Release: " << films[n].year << endl;
cout << "\n";
flag = true;
}
}
if (flag = false)
cout << "Search on title not found!" << endl;
}// end of search_on_title function
int menu()
{
int choice;
cout << " " << endl;
cout << "Enter 1 to search on titles " << endl;
cout << "Enter 2 to search on years " << endl;
cin >> choice;
cout << "\n";
return choice;
}// end of menu function
int username_and_pass()
{
string uName;
string password;
int value;
char ch;
cout << "Please enter your username\n";//"rusty"
cin >> uName;
cout << "Please enter your password\n";//"rusty"
ch = _getch();
while (ch != 13)//As long as the user doesn't press Enter
{//(enter is ASCII code 13) continue reading keystrokes from the screen.
password.push_back(ch);
cout << '*';
ch = _getch();
}
if (uName == "rusty" && password == "rusty")
{
cout << "\n\nAccess granted! Welcome " << uName << "\n\n" << endl;
value = 1
}
else
{
cout << "Invalid credentials" << endl;
value = 0;
}
return value;
}// end of username_and_pass function
int main()
{
string mystr;
int n;
string response;
int value = 0;
do
{
value = username_and_pass();
} while (value==0);
if(value==1)
{
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;
}
//sorts records
sort_on_title(films, NUM_MOVIES);
cout << "\nYou have entered these movies:\n";
for (n = 0; n < NUM_MOVIES; n++)
printmovie(films[n]);
//menu
int choice = 0;
choice = menu();
if (choice == 1)
{
string searchTerm;
cout << "What is the movie you want to search for? " << endl;
cin >> searchTerm;
cout << " " << endl;
search_on_title(films, NUM_MOVIES, searchTerm);
}
else
{
int searchTermYear;
cout << "What is the year you want to search for? " << endl;
cin >> searchTermYear;
cout << " " << endl;
search_on_year(films, NUM_MOVIES, searchTermYear);
}
cout << "Would you like to query the database again? (Y/N)" << endl;
cin >> response;
if (response == "Y" || "y")
{
choice = menu();
if (choice == 1)
{
string searchTerm;
cout << "What is the movie you want to search for? " << endl;
cin >> searchTerm;
cout << " " << endl;
search_on_title(films, NUM_MOVIES, searchTerm);
}
else
{
int searchTermYear;
cout << "What is the year you want to search for? " << endl;
cin >> searchTermYear;
cout << " " << endl;
search_on_year(films, NUM_MOVIES, searchTermYear);
}
}
}
return 0;
}
请输入您的用户名
生锈的
请输入您的密码
允许访问!欢迎拉斯蒂
输入标题:输入年份:(输入电影年)
输入标题:(输入电影标题)
输入年份:(输入电影年)
输入标题:(输入电影标题)
....
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
int username_和_pass();
#定义NUM_电影6
结构电影列表{
字符串标题;
国际年;
}电影[NUM_MOVIES];
//带有函数声明的原型
标题上的无效排序(电影列表电影[],int n)
{
电影(临时名单),;
对于(int i=0;i电影[i+1]。标题)
{
温度=胶片[i];
菲林[i]=菲林[i+1];
薄膜[i+1]=温度;
}
}
}//标题函数上的排序结束
作废打印电影(电影列表电影)
{
cout我不确定是什么原因导致了这个问题。但是您可以尝试使用cin.clear()。请在请求输入标题之前放置它
cin.clear();
cout << "Enter title: ";
getline(cin, films[n].title);
cout << "Enter year: ";
getline(cin, mystr);
stringstream(mystr) >> films[n].year;
cin.clear();
古特电影[n].年;
刷新输入缓冲区的所有内容
请转到以下链接以刷新输入缓冲区的内容
谢谢!我把fseek(stdin,0,SEEK_END);
放在cout后面