2013-11-15 41 views
0

下面的代碼的目的是建立一個僞電影數據庫,並能夠通過它與兩個查詢搜索。該程序本身工作,直到我也試圖把用戶名和密碼到位(密碼在屏幕上顯示星號)。爲了使用用戶名和密碼,我設置了一條if語句,如果(username ==「user」& & password ==「word」)它將顯示「Hello,user」。而不是預期的產出,我遭到了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"; 
     } 
} 
+0

這將是容易得多,以幫助這一點,如果這個代碼或者編譯或格式化合理(或兩者)。 –

回答

2

stringchar *的操作。您不需要一組string只是爲了存儲密碼,只需按照名稱閱讀即可。如果你想閱讀使用getch*事情密碼,讀入char[9],然後轉換爲string(只1串)。

而且順便說一句,c[9]不存在,什麼是你想用它做?

BTW 2:如果你讀了9個字符爲char[9],你不最後的位置設置爲0,當您嘗試使用任何字符串相關功能不好的事情會發生(更不用說"password"的長度8,所以它不能匹配)。

0

問題就出在這條線,所有的線,你使用相同的約定,不理解它:

string c[9]; 

以上並不意味着「字符串,最多9個字符」,這意味着「9個字符串的數組,其中包含您輸入的字符數量」。在循環中:

for (int i=0;i<9;i++){ 
    c[i] = getch(); 
} 

在EVERY字符串中輸入1個字符(在一個字符串中不是9個字符)。然後,當您嘗試將c[9]與「密碼」進行比較時,程序將嘗試訪問不存在的c[9]元素。