C++ 클래스에 대한이 할당을 얻으려고합니다. do/while 루프가 제대로 작동하지 않는 문제가있어서 누군가가 줄을 추가 할 것을 제안했습니다. cin.ignore (2, '\ n'); 학생이 입력 한 학생 이름을 입력 할 수있는 InputData 함수에서. 그것은 효과가 있었고 do/while은 현재 작동 중입니다. 그러나, 나는 100 % 확실하지 않아 어떻게 cin.ignore (2, '\ n'); 작품과 나는 사용자가 입력 한 "이름"의 처음 두 문자가 버려지고있는 곳에서 처음으로 문제가 발생합니다. 2를 0으로 변경하면 이름의 처음 두 문자는 잘리지 않지만 사용자가 'y'를 입력하면 계속 진행하고 첫 번째 질문은 건너 뜁니다. "학생의 이름 입력 ".C++가 입력 버퍼 문제에서 나머지 줄 바꿈을 삭제합니다.
도움을 주시면 대단히 감사하겠습니다 !!
참고 자료로, 저는 일반적으로 프로그래밍에 초보자입니다. 특히 C++입니다. 제발 웃어주세요.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Student {
public:
Student();
~Student();
// Input all info of user
void InputData();
// Output class list
void OutputData();
// Reset class list
void ResetClasses();
Student& operator =(const Student& rightSide);
private:
string name;
string stuName;
int numbClasses;
string *classList;
};//end student class
//Initialize variables to empty and array to NULL
Student::Student() {
numbClasses = 0;
classList = NULL;
name = "";
}//end variable initialization
//Frees up any memory allocated to array.
Student::~Student() {
if (classList != NULL) {
delete [ ] classList;
}//end if
}//end free memory
//Delete the class list
void Student::ResetClasses() {
if (classList != NULL) {
delete [] classList;
classList = NULL;
}//end if block
numbClasses = 0;
}//end reset classes
여기서 cin.ignore (2, '\ n'); 계십니까이/루프가있는 동안
// Inputs info from user.
void Student::InputData() {
int i;
// Reset the class list in case method is called again and array isn't cleared
ResetClasses();
cout << "Enter student name." << endl;
//Discards the leftover newline from input buffer
cin.ignore(2,'\n');
getline(cin, name);
cout << "Enter number of classes." << endl;
cin >> numbClasses;
//Discards the leftover newline from input buffer
cin.ignore(2,'\n');
if (numbClasses > 0) {
// Construct array big enough to hold # of classes
classList = new string[numbClasses];
// Loop through the # classes, input name of each one into array
for (i = 0; i < numbClasses; i++) {
cout << "Enter name of class " << (i+1) << endl;
getline(cin, classList[i]);
}//end for loop
}//end if block
cout << endl;
}//end input data
출력 데이터
//Output info entered by user.
void Student::OutputData() {
int i;
cout << "Name: " << name << endl;
cout << "Number of classes: " << numbClasses << endl;
for (i=0; i<numbClasses; i++) {
cout << " Class " << (i+1) << ":" << classList[i] << endl;
}//end for loop
cout << endl;
}//end Output data
//overload this operator so there aren't two references to same class list.
Student& Student::operator =(const Student& rightSide) {
int i;
// Erase list of classes
ResetClasses();
name = rightSide.name;
numbClasses = rightSide.numbClasses;
// Copy the list of classes
if (numbClasses > 0) {
classList = new string[numbClasses];
for (i=0; i<numbClasses; i++) {
classList[i] = rightSide.classList[i];
}//end for loop
}//end if block
return *this;
}//end overload
홈페이지를 위치하고 있습니다.
// main function
int main() {
char choice;
//Do/While loop to ask user if they'd like to continue or end program.
do {
// Test code with two student classes
Student s1, s2;
// Input for s1
s1.InputData();
cout << "Student 1's data:" << endl;
// Output for s1
s1.OutputData();
cout << endl;
s2 = s1;
cout << "Student 2's info after assignment from student 1:" << endl;
// Should output same info as student 1
s2.OutputData();
s1.ResetClasses();
cout << "Student 1's info after the reset:" << endl;
// Should have no classes
s1.OutputData();
cout << "Student 2's info, should still have original classes:" << endl;
// Should still have original classes
s2.OutputData();
cout << endl;
cout << "Would you like to continue? y/n" << endl;
cin >> choice;
} while(choice == 'y'); //end do/while
return 0;
}//end main
HTTP : //www.cplusplus를 .com/doc/tutorial/control/다음에 MCVE를 사용하십시오. 부디. – Chantola
MCVE는 귀하를 위해서입니다. 항상 getline (cin, ...)과 같이 마스터하려고하는 기술을 사용하는 가장 작고 간단한 프로그램을 작성하여 새 기능 *을 독립적으로 개발하려고 노력해야합니다. 일단 완벽하게 작동하면 더 큰 것으로 통합 할 수 있습니다. – Beta
크기와 문자를 지정하는 대신'std :: cin.ignore();'를 사용하면됩니다. – Brandon