2014-09-28 138 views
0

我很難試圖讓這個程序循環,如果用戶輸入'y'他們想繼續。我是非常新的編程方式,所以任何幫助非常感謝。我認爲最好的做法是在main中添加一個do/while,如果他們想要在代碼的最後繼續添加用戶,但我很快意識到,除非我調用用於用戶輸入的以前方法並輸出。這就是問題出現的地方。C++ do/while循環和調用函數?

再次感謝您的幫助!

/* 

• Ask the user if they want to enter the data again (y/n). 
• If ’n’, then the program ends, otherwise it should clear the student class object and 
repeat the loop (ask the user to enter new data...). 

*/ 
#include <iostream> 
#include <cstdlib> 
#include <string> 

using namespace std; 

class Student { 
    public: 
    Student(); 
    ~Student(); 
    void InputData();  // Input all data from user 
    void OutputData();  // Output class list to console 
    void ResetClasses();  // Reset class list 
    Student& operator =(const Student& rightSide); // Assignment operator 

private: 
    string name; 
    int numClasses; 
    string *classList; 
}; 


//array intialized to NULL 
Student::Student() { 
    numClasses = 0; 
    classList = NULL; 
    name = ""; 
} 

//Frees up any memory of array 
Student::~Student() { 

if(classList != NULL) { 
    delete [] classList; 
} 
} 

// This method deletes the class list 
// ====================== 
void Student::ResetClasses() { 
    if(classList != NULL) { 
     delete [ ] classList; 
     classList = NULL; 
} 
numClasses = 0; 
} 



//inputs all data from user (i.e. number of classes) 
//using an array to store classes 
void Student::InputData() { 
int i; 

// Resets the class list in case the method 
// was called again and array wasn't cleared 
ResetClasses(); 

cout << "Enter student name." << endl; 
getline(cin, name); 
cout << "Enter number of classes." << endl; 
cin >> numClasses; 
cin.ignore(2,'\n'); // Discard extra newline 
if (numClasses > 0) { 
    //array to hold number of classes 
    classList = new string[numClasses]; 
    // Loops through # of classes, inputting name of each into array 
    for (i=0; i<numClasses; i++) { 
     cout << "Enter name of class " << (i+1) << endl; 
     getline(cin, classList[i]); 
    } 
} 
cout << endl; 
} 

// This method outputs the data entered by the user. 
void Student::OutputData() { 

int i; 
cout << "Name: " << name << endl; 
cout << "Number of classes: " << numClasses << endl; 
for (i=0; i<numClasses; i++) { 
    cout << " Class " << (i+1) << ":" << classList[i] << endl; 
} 
cout << endl; 
} 


/*This method copies a new classlist to target of assignment. If the operator isn't overloaded  there would be two references to the same class list.*/ 
Student& Student::operator =(const Student& rightSide) { 

int i; 
// Erases the list of classes 
ResetClasses(); 
name = rightSide.name; 
numClasses = rightSide.numClasses; 

// Copies the list of classes 
if (numClasses > 0) { 
    classList = new string[numClasses]; 
    for (i=0; i<numClasses; i++) { 
     classList[i] = rightSide.classList[i]; 
    } 
} 
return *this; 
} 

//main function 
int main() { 
    char choice; 
do { 
// Test our code with two student classes 
Student s1, s2; 

s1.InputData();  // Input data for student 1 
cout << "Student 1's data:" << endl; 
s1.OutputData();  // Output data for student 1 

cout << endl; 

s2 = s1; 
cout << "Student 2's data after assignment from student 1:" << endl; 
s2.OutputData();  // Should output same data as for student 1 

s1.ResetClasses(); 
cout << "Student 1's data after reset:" << endl; 
s1.OutputData();  // Should have no classes 

cout << "Student 2's data, should still have original classes:" << endl; 
s2.OutputData();  // Should still have original classes 

cout << endl; 
cout << "Would you like to continue? y/n" << endl; 
cin >> choice; 
if(choice == 'y') { 

    void InputData();  // Input all data from user 
    void OutputData();  // Output class list to console 
    void ResetClasses();  // Reset class list 
} 
} while(choice == 'y'); 

return 0; 
} 
+0

如果您製作classlist'vector ',那麼您不再需要刪除它或創建一個賦值運算符或複製構造函數(您忘記了)。它只是工作。 – 2014-09-29 00:54:01

回答

0

剛剛擺脫的

if(choice == 'y') { 

    void InputData();  // Input all data from user 
    void OutputData();  // Output class list to console 
    void ResetClasses();  // Reset class list 
} 

因爲變量s1s2是內部的,而DO循環,他們會在每次迭代重建。 (構造函數將在定義處被調用,析構函數將在循環的大括號處被調用,然後再測試choice == 'y'並重復)。

您遇到的另一個問題是您的標準輸入不處於與再次調用s1.InputData()兼容的狀態。由於您只是使用提取操作符>>來讀取choice,所以解析停止在第一個空白處,並且緩衝區中至少存在(至少)一個換行符。當Student::InputData調用getline時,它會發現該換行符仍在緩衝區中,而不是等待其他輸入。

這與您在閱讀numClasses後使用cin.ignore的原因相同。你會想在這裏做同樣的事情。

+0

這些不是「變數」;他們是函數聲明。 OP可能打算從全局範圍調用具有相同名稱的函數,而不是重新聲明類似的本地函數。我沒有看到刪除整個條件塊會實現什麼。 – 2014-09-28 23:53:15

+0

是的,這將無濟於事,因爲它會重新運行程序而不再詢問用戶輸入。我試着用不同的方式調用輸入和輸出函數,但沒有運氣。任何提示或想法? – NoobCoderChick 2014-09-29 00:14:53

+0

@LightnessRacesinOrbit:請把你的評論帶走。那些不是變數,但我從未說過他們是。我說變量在循環裏面。那麼,他們是。您的評論完全錯過了這一觀點。如果你看不到刪除條件塊會做什麼......你甚至看過問題中的代碼嗎? – 2014-09-29 00:20:43