2015-10-27 247 views
0

我很新的C++和我的任務,他們要我創建獲取用戶輸入,我已經在這裏做了一個功能:(班級列表是一個數組)do-while循環C++

public: 
    void userInput() { 

     string enterAgain; 

     do { 

     cout << "Enter the students name: " << name; 
     cin >> name; 

     cout << "Enter the number of classes the student is enrolled in: " << numClasses; 
     cin >> numClasses; 

     for (int i = 0; i < numClasses; i++) { 
      cout << "Enter the class list: " << (i+1) << classList; 
      cin >> classList; 
      i++; 
     } 

     cout << "Would you like to enter again (y for yes): " << enterAgain; 
     cin >> enterAgain; 

     } while (enterAgain == "Y" || enterAgain == "y"); 

    } 

當我運行它,它會要求用戶爲學生名字後面的他們正在上課的數,但是當它要求用戶輸入類列表會顯示這樣的事情:

Enter the class list: 0x7fff536d1b78 

但除此之外,它不會讓我輸入任何內容。我搜索了幾個小時試圖糾正這個問題,我希望有人能指出我在糾正這個問題的正確方向。謝謝!

+1

'<< classList'打印陣列'classList'的地址。你爲什麼要打印?如果'classList'是一個數組,則需要將值放在**索引**處。 – CrakC

+0

我仍然完全理解如何使用數組,但指令是「輸入來自用戶的所有值的函數,包括類名稱列表」。 – mur7ay

+0

是的,它就是這樣做的。我相信數組'classList'的內容是他們說**類名**的指令。 – CrakC

回答

1
public: 
    void userInput() { 

     string enterAgain; 

     do { 

     cout << "Enter the students name: " << name; 
     cin >> name; 

     cout << "Enter the number of classes the student is enrolled in: " << numClasses; 
     cin >> numClasses; 

     for (int i = 0; i < numClasses; i++) { 
      cout << "Enter the class list: " << (i+1) << classList; 
      cin >> classList; 
      i++; 
     } 

     cout << "Would you like to enter again (y for yes): " << enterAgain; 
     cin >> enterAgain; 

     } while (enterAgain == "Y" || enterAgain == "y"); 

    } 

應爲─

public: 
    void userInput() { 

     string enterAgain; 

     do { 

     cout << "Enter the students name: " << endl; 
     cin >> name; 

     cout << "Enter the number of classes the student is enrolled in: " << endl; 
     cin >> numClasses; 

     for (int i = 0; i < numClasses; i++) { 
      cout << "Enter the class list: " << (i+1) << endl; 
      cin >> classList[i]; 
     } 

     cout << "Would you like to enter again (y for yes): " << endl; 
     cin >> enterAgain; 

     } while (enterAgain == "Y" || enterAgain == "y"); 

    } 
+0

非常感謝! – mur7ay