C++ do/while loop and calling functions?

烂漫一生 提交于 2019-12-13 07:32:16

问题


I am having trouble trying to get this program to loop if user enter 'y' they'd like to continue. I'm super new to programming by the way so any help is greatly appreciated. I figured the best was to add a do/while in main and as k the user if they'd like to continue at the end of the code but, I quickly realized that wouldn't work unless I called the previous methods for user input and output. That's where the issue is arising.

Thanks again for any help!

/* 

 • 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;
}

回答1:


Just get rid of

if(choice == 'y') {

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

Because the variables s1 and s2 are inside the do-while loop, they'll be recreated on each iteration. (The constructor will be called at the definition, and the destructor will be called at the closing brace of the loop, before it tests choice == 'y' and repeats).

The other problem you run into is that your standard input isn't in a state compatible with calling s1.InputData() again. Because you just used the >> extraction operator to read choice, parsing stopped at the first whitespace, and there is (at least) a newline leftover in the buffer. When Student::InputData calls getline, it will find that newline still in the buffer and not wait for additional input.

This is the same reason you used cin.ignore after reading numClasses. You'll want to do the same here.



来源:https://stackoverflow.com/questions/26090734/c-do-while-loop-and-calling-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!