2016-07-25 30 views
-2
#include <iostream> 
#include <time.h> 
#include <stdlib.h> 

using namespace std; 



int main() 
{ 
    int password; 

    system("cls"); 
    cout <<"Login"<< endl; 
    cout <<"Enter password to continue"<< endl; 
    cin >> password; 
    cin.ignore().get(); 

      if(password == 1111) 
      { 

       system("cls"); 
       cout <<"Access Granted"<< endl; 
       system("PAUSE"); 
       system("cls"); 
       return main(); 
      } 
      //Want to make if(password == 1111) return to main(), but start where it left off 
      //I want it to start at cout <<"Files:'<< endl; 
        cout <<"Files:"<< endl; 
        cout <<"\n E \n N \n G \n S \n"<< endl; 
        cout <<"Choose a file"<< endl; 
        string file; 
        cin >> file; 
        cin.ignore().get(); 

           if(file == "E" || file == "e") 
           { 
             system("cls"); 
             cout <<"E:"<< endl; 
             cout <<"Age:"<< endl; 
             cout <<"Grade:"<< endl; 
             cout <<"Eye color:"<< endl; 
             cout <<"Hair color:"<< endl; 
             system("Pause");  
           } 

           else if(file == "N" || file == "n") 
           { 
             system("cls"); 
             cout <<"N:"<< endl; 
             cout <<"Age:"<< endl; 
             cout <<"Grade:"<< endl; 
             cout <<"Eye color:"<< endl; 
             cout <<"Hair color:"<< endl; 
             system("Pause"); 

           }   

           else if(file == "G" || file == "g") 
           { 
             system("cls"); 
             cout <<"G:"<< endl; 
             cout <<"Age:"<< endl; 
             cout <<"Eye color:"<< endl; 
             cout <<"Hair color:"<< endl; 
             system("Pause");   
           }   

           else if(file == "S" || file == "s") 
           { 
             system("cls"); 
             cout <<"S:"<< endl; 
             cout <<"Age:"<< endl; 
             cout <<"Eye color:"<< endl; 
             cout <<"Hair color:"<< endl; 
             system("Pause");   
           }   
      else 
      { 
       system("cls"); 
       cout<<"Access Denied!"<< endl; 
       system("PAUSE"); 
       return 0;  
      } 

    return 0; 
} 

無法確定如何繼續主要功能所在的位置。我一直在教自己如何編寫代碼,所以我不知道這是否可能。閱讀我留下的評論在代碼中更好地理解我在說什麼。 THXC++要返回到哪裏停止工作

+0

*想讓如果(密碼== 1111)返回主(),但是開始離開的地方 我希望它在開始的cout <<「文件:」 << endl; *現在,代碼將完成如果您刪除調用'return main()'。此外,不要從'main'或您定義的任何函數中調用'main' 。標準不允許。 –

+0

if(password == 1111)//做你的東西,否則繼續下去的路徑。你不需要做任何extra.remove遞歸調用main()。這是不合適的 – basav

+0

順便說一句,你的縮進是怎麼回事?這個程序中的縮進並不是'不符合其實際結構。 – immibis

回答

0

如果您刪除此行:

return main(); 

然後電腦將繼續在它們的排列順序執行指令 - 特別是,下一個會是cout <<"Files:"<< endl;

0

要控制程序的流程,您有多個選項。

如果您多次處理事務,則有forwhile循環。如果你想停止循環並離開,請使用break。如果您想跳到下一次迭代,請使用continue

如果您只想跳到程序中的不同位置,可以使用goto,但這是一種代碼異味。避免它通常會更好,因爲它往往會影響可讀性。

如果你想做一個他可以中斷的任務,你可以使用一個void function。如果您希望該功能停止正在進行的操作並繼續使用main,請使用return


然而,在具體的例子,好像你不太瞭解的if聲明是如何工作的。 如果if的條件爲真,它將執行花括號中的任何內容({ ... })。它會在右大括號後自動繼續執行。 您不需要明確返回到主。

你究竟在那裏做了什麼(錯誤)是寫一個遞歸函數(一個自我調用,直到滿足條件)。

0

您似乎對C++程序中的程序流程有明顯的誤解。 return main()將重新調用main,所以用戶將再次看到「login」提示。當對main的調用已結束時,因爲您使用了關鍵字return,它將退出先前的main調用。這些都不是你想要的。 C++標準也明確禁止從另一個函數調用main。

我還注意到,在縮進功能的長塊之後,您有一個else語句,看起來像是其他的到if (password == 1111)

縮進對C++編譯器沒有任何意義,它純粹是爲了人類的可讀性。

我想你正在努力實現更多的東西是這樣的:

if (password == 1111) 
{ 
    std::cout << "Access granted\n"; 
} 
else 
{ 
    std::cout << "Access denied\n"; 
    return 0; 
} 

std::cout << "Files:\n"; 

這裏,如果用戶鍵入1111,則執行的代碼的第一個複合塊,並結束後繼續執行if/else塊,即下一條指令是打印文件。

如果用戶鍵入其他內容,則執行else塊。它以return 0結束,它退出該函數,並返回值0給調用者。

#include <iostream> 
#include <string> 

int main() 
{ 
    std::cout << "Login:\n"; 
    std::string password; 
    std::getline(std::cin, password); 

    if (password == "1111") 
     std::cout << "That's correct.\n"; 
    else { 
     std::cout << "Access denied.\n"; 
     return 0; 
    } 

    std::cout << "Files:\n"; 

    // your code here 
} 

演示:http://ideone.com/DSYAG4

+0

非常感謝。被困了幾天。 –

+0

@NathanTurner如果有幫助,請接受(點擊對號)和/或加註答案。 – kfsone