2017-02-15 38 views
1

在我的C++類中,我們的任務是在這個代碼中構建不同的方面。我目前收到2個錯誤,並且卡在我不知道自己做錯了什麼的地方。該程序需要一個私人汽車或字符串的名稱和一個私人整數輸入遊戲檢查可分性3,5,和兩個3 & 5.我要在課堂上使用get函數和put函數輸入值並輸出它們。我基本上已經想出了該程序,但它不會編譯,我真的不確定爲什麼。這裏是我的代碼:使用類,私有,公共,構造函數,函數,整數和字符串進一步學習的程序

#include <iostream> 
#include <iomanip> 
using namespace std; 
using std::istream; 

// declare the max size the username input can be 
const int MAX = 14; 

enum FIZZBUZZ { ABORT = 0, FIZZBUZZ, FIZZ, BUZZ }; 


class CFizzbuzz        // Class definition at global scope 
{ 
    // make sure our constructor, destructor, plus member functions are 
    // all public and available from outside of the class. 
public: 

    CFizzbuzz() {}    // Default constructor definition 
    ~CFizzbuzz() {}    // Default destructor definition 


           // function members that are public 

           // get the user's name and their value from the console and 
           // store those results into the member variables. 
    void getFizzbuzz() 
    { 
     cout << "Please enter your name: " << endl; 
     cin >> m_myName; 

     cout << "Please enter your number for the FizzBuzz game: " << endl; 
     cin >> m_myNum; 
    } 

    // return the user's number type entered 
    int putFizzBuzz() 
    { 
     return m_myNum; 
    } 

    char* getName() 
    { 
     return m_myName; 
    } 

    // logic to check to see if the user's number is 0, fizz, buzz, or  fizzbuz 
    int getRecord(int num) 
    { 
     if (num == 0) 
     { 
      return ABORT; 
     } 
     else if (num % 5 == 0 && num % 3 == 0) // fizzbuzz number 
     { 
      return FIZZBUZZ; 
     } 
     else if (num % 5 == 0) // buzz number 
     { 
      return BUZZ; 
     } 
     else if (num % 3 == 0) // fizz number 
     { 
      return FIZZ; 
     } 
     else 
      return num; 
    } 

    // private data members only available inside the class 
private: 
    int  m_myNum; 
    char m_myName[MAX]; 
}; 


int main() 
{ 
    CFizzbuzz myClass; 


    cout << "Welcome to my Fizzbuzz game, you are to guess the location of a " 
     << "number which if is divisible by 5 and 3 you will win with " 
     << "the output of Fizzbuzz. " << endl; 
    cout << "Please enter an integer value between 0 and 3 " 
     << "representing the row location of the number for the game, " 
     << "then press the Enter key: " << endl; 

    for (;;) 
    { 
     myClass.getFizzbuzz(); 

     int num = myClass.putFizzBuzz(); 
     switch (myClass.getRecord(num)) 
     { 
     case ABORT: 
      cout << myClass.getName() << "\nThank you for playing\n"; 
      system("PAUSE"); 
      return 0; // exit program 

     case FIZZ: 
      cout << "Sorry, " << myClass.getName() << ", number is a Fizz, please try again.\n"; 
      break; 

     case BUZZ: 
      cout << "Sorry, " << myClass.getName() << ", number is a Buzz, please try again.\n"; 
      break; 

     case FIZZBUZZ: 
      cout << "You win you got FizzBuzz!!!" << endl; 
      break; 

     default: 
      cout << "Sorry, " << myClass.getName() << ", number is a not a Fizz, Buzz, or Fizzbuzz\nPlease try again.\n"; 
      break; 
     } 
    } 
} 

這些錯誤我越來越:

LNK2019,LNK1120

+0

你能提供更具體的錯誤嗎?請提供描述您的錯誤的行,而不是_LNK2019_和_LNK1120_ –

+0

另外您還說您收到9個錯誤。如果是這樣的話,那麼你的其他7個錯誤是什麼? –

+0

只有兩個錯誤。我以前有過9次。錯誤讀取:無法解析的外部符號_WinMain @ 16在函數「int_cdecl invoke_main(void)」(?invoke_main @@ YAHXZ)中引用 – phoenixCoder

回答

0

根據您在評論中提到的錯誤(Unresolved external symbol [email protected])我會說,你創建了一個Win32項目(一個GUI項目)在Visual Studio中,但您的代碼旨在成爲控制檯應用程序。

您需要將項目的類型從Win32應用程序更改爲控制檯應用程序,方法是重新創建它,或者在項目設置中將子系統從Windows更改爲控制檯。參見後者以下鏈接瞭解更多信息:

https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx

+0

修復它。它似乎並不想結束,而是一直在玩 – phoenixCoder

0

我將可疑否則返回NUM的。當你輸入1或2時會發生什麼?顯然,模數並不提供嘶嘶聲或嗡嗡聲,但是根據你的枚舉值,函數getRecord()返回它的值。我將有一個無枚舉值設置爲-1,以表明它既不是一個嘶嘶聲或嗡嗡聲。

要記住一個枚舉值的事情是它編譯時解析爲一個實際的數字。所以,當你輸入1,並且模數不能證明是一個嘶嘶聲,嗡嗡聲,或者一個混亂的聲音,並且你返回1時,即使情況並非如此(雙關意圖),開關盒語句也會解決。

只要您評論說它不能按預期工作,請輸入更多詳細信息。

相關問題