2014-09-28 107 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