2012-03-06 6 views
0

여기에서 제 문제를 이해하지 못합니다. 진영과 숫자의 가치를 얻는 것에 대한 나의 목록은 옳은 것처럼 보인다. 하지만, 내 출력에 문제가 있습니다. 저 숫자와 문자가 없음은에있어 어떤 진영에 표시 할 수 있습니다문자를 std :: cout 번호로 인쇄합니다.

을 번호를 인쇄하는 문이 하나처럼, 맨 아래에 있습니다. 여기

cout << "Your first Card is " 
    << numberBoxListHead_ptr->getNumber() 
    << factionHead_ptr->getLetter() 
    << endl; 

전체 코드입니다 :

#include <iostream> 
#include <conio.h> 
using namespace std; 

//The Class where the List depends on it. 
class NumberBox 
{ 
private: 

    int number; 
    char letter; 

public: 

    NumberBox *next_ptr; 
    void setNumber(int number) 
    { 
     this->number = number; 
    } 

    void setLetter(char letter) 
    { 
     this->letter = letter; 
    } 


    int getNumber() 
    { 
     return number; 
    } 

    char getLetter() 
    { 
     return letter; 
    } 
}; 

int main() 
{ 
    int number[4]; 
    char faction[4]; 

    NumberBox *factionHead_ptr = new NumberBox(); 
    NumberBox *factionBody1_ptr = new NumberBox(); 
    NumberBox *factionBody2_ptr = new NumberBox(); 
    NumberBox *factionBody3_ptr = new NumberBox(); 
    NumberBox *factionTail_ptr = new NumberBox(); 

    NumberBox *numberBoxListHead_ptr = new NumberBox(); 
    NumberBox *numberBoxListBody1_ptr = new NumberBox(); 
    NumberBox *numberBoxListBody2_ptr = new NumberBox(); 
    NumberBox *numberBoxListBody3_ptr = new NumberBox(); 
    NumberBox *numberBoxListTail_ptr = new NumberBox(); 

    cout << "Please give the number for the first Card" << endl; 
    cin >> number[0]; 
    if(number[0] > 0 && number[0] < 13) 
    { 
     numberBoxListHead_ptr->setNumber(number[0]); /* <--- Setting for the first input, which is the number */ 
    } 
    else 
    { 
     cout << " Invalid " << endl; 
     exit(1); 
    } 
    cout << "Please Give the faction for the first Card" << endl; 
    cin >> faction[0]; 
    if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd') 
    { 
     if(faction[0] == 's') 
     { 
      factionHead_ptr->setLetter('s'); 
     } 
     else if(faction[0] == 'c') 
     { 
      factionHead_ptr->setLetter('c'); 
     } 
     else if(faction[0] == 'h') 
     { 
      factionHead_ptr->setLetter('h'); 
     } 
     else if(faction[0] == 'd') 
     { 
      factionHead_ptr->setLetter('d'); 
     } 
    } 
    else 
    { 
     cout << "Invalid" << endl; 
     exit(1); 
    } 


    cout << "Please give the number for the Second Card" << endl; 
    cin >> number[1]; 
    if(number[1] > 0 && number[1] < 13) 
    { 
     numberBoxListBody1_ptr->setNumber(number[1]); /* <--- Setting for the second input, which is the number */ 
    } 
    else 
    { 
     cout << " Invalid " << endl; 
     exit(1); 
    } 
    cout << "Please Give the faction for the second Card" << endl; 
    cin >> faction[1]; 
    if(faction[1] == 's' || faction[1] == 'c' || faction [1] == 'h' || faction [1] == 'd') 
    { 
     if(faction[1] == 's') 
     { 
      factionHead_ptr->setLetter('s'); 
     } 
     else if(faction[1] == 'c') 
     { 
      factionHead_ptr->setLetter('c'); 
     } 
     else if(faction[1] == 'h') 
     { 
      factionHead_ptr->setLetter('h'); 
     } 
     else if(faction[1] == 'd') 
     { 
      factionHead_ptr->setLetter('d'); 
     } 
    } 
    else 
    { 
     cout << "Invalid" << endl; 
     exit(1); 
    } 
    cout << "Please give the number for the Third Card" << endl; 
    cin >> number[2]; 
    if(number[2] > 0 && number[2] < 13) 
    { 
     numberBoxListBody2_ptr->setNumber(number[2]); /* <--- Setting for the third input, which is the number */ 
    } 
    else 
    { 
     cout << " Invalid " << endl; 
     exit(1); 
    } 
    cout << "Please Give the faction for the Third Card" << endl; 
    cin >> faction[2]; 
    if(faction[2] == 's' || faction[2] == 'c' || faction [2] == 'h' || faction [2] == 'd') 
    { 
     if(faction[2] == 's') 
     { 
      factionHead_ptr->setLetter('s'); 
     } 
     else if(faction[2] == 'c') 
     { 
      factionHead_ptr->setLetter('c'); 
     } 
     else if(faction[2] == 'h') 
     { 
      factionHead_ptr->setLetter('h'); 
     } 
     else if(faction[2] == 'd') 
     { 
      factionHead_ptr->setLetter('d'); 
     } 
    } 
    else 
    { 
     cout << "Invalid" << endl; 
     exit(1); 
    } 
    cout << "Please give the number for the fourth Card" << endl; 
    cin >> number[3]; 
    if(number[3] > 0 && number[3] < 13) 
    { 
     numberBoxListBody3_ptr->setNumber(number[3]); /* <--- Setting for the fourth input, which is the number */ 
    } 
    else 
    { 
     cout << " Invalid " << endl; 
     exit(1); 
    } 
    cout << "Please Give the faction for the fourth Card" << endl; 
    cin >> faction[3]; 
    if(faction[3] == 's' || faction[3] == 'c' || faction [3] == 'h' || faction [3] == 'd') 
    { 
     if(faction[3] == 's') 
     { 
      factionHead_ptr->setLetter('s'); 
     } 
     else if(faction[3] == 'c') 
     { 
      factionHead_ptr->setLetter('c'); 
     } 
     else if(faction[3] == 'h') 
     { 
      factionHead_ptr->setLetter('h'); 
     } 
     else if(faction[3] == 'd') 
     { 
      factionHead_ptr->setLetter('d'); 
     } 
    } 
    else 
    { 
     cout << "Invalid" << endl; 
     exit(1); 
    } 
    cout << "Please give the number for the Fifth Card" << endl; 
    cin >> number[4]; 
    if(number[4] > 0 && number[4] < 13) 
    { 
     numberBoxListTail_ptr->setNumber(number[4]); /* <--- Setting for the fifth input, which is the number */ 
    } 
    else 
    { 
     cout << " Invalid " << endl; 
     exit(1); 
    } 

    cout << "Please Give the faction for the fifth Card" << endl; 
    cin >> faction[4]; 
    if(faction[4] == 's' || faction[4] == 'c' || faction [4] == 'h' || faction [4] == 'd') 
    { 
     if(faction[4] == 's') 
     { 
      factionHead_ptr->setLetter('s'); 
     } 
     else if(faction[4] == 'c') 
     { 
      factionHead_ptr->setLetter('c'); 
     } 
     else if(faction[4] == 'h') 
     { 
      factionHead_ptr->setLetter('h'); 
     } 
     else if(faction[4] == 'd') 
     { 
      factionHead_ptr->setLetter('d'); 
     } 
    } 
    else 
    { 
     cout << "Invalid" << endl; 
     exit(1); 
    } 

    //This would be the list for the value of the card. 

    numberBoxListHead_ptr->next_ptr = numberBoxListBody1_ptr; /* <--- From Head to Body1 */ 
    numberBoxListBody1_ptr->next_ptr = numberBoxListBody2_ptr; /* <--- From Body1 to Body2 */ 
    numberBoxListBody2_ptr->next_ptr = numberBoxListBody3_ptr; /* <--- From Body2 to Body3 */ 
    numberBoxListBody3_ptr->next_ptr = numberBoxListTail_ptr; /* <--- From Body3 to BodyTail */ 

    //This Would be the list for the faction of the card. 

    factionHead_ptr->next_ptr = factionBody1_ptr; /* <--- From Head to Body1 */ 
    factionBody1_ptr->next_ptr = factionBody2_ptr; /* <--- From Body1 to Body2 */ 
    factionBody2_ptr->next_ptr = factionBody3_ptr; /* <--- From Body2 to Body3 */ 
    factionBody3_ptr->next_ptr = factionTail_ptr; /* <--- From Body3 to BodyTail */ 

    int counter; 
    for(counter = 0; counter < 4; counter++) 
    { 
     if(counter == 0) 
     { 
      cout << "Your first Card is " << numberBoxListHead_ptr->getNumber() << factionHead_ptr->getLetter() << endl; 
     } 
     if(counter == 1) 
     { 
      cout << "Your Second Card is " << numberBoxListBody1_ptr->getNumber() << factionBody1_ptr->getLetter() << endl; 
     } 
     if(counter == 2) 
     { 
      cout << "Your third Card is " << numberBoxListBody2_ptr->getNumber() << factionBody2_ptr->getLetter() << endl; 
     } 
     if(counter == 3) 
     { 
      cout << "Your fourth Card is " << numberBoxListBody3_ptr->getNumber() << factionBody3_ptr->getLetter() << endl; 
     } 
     if(counter == 4) 
     { 
      cout << "Your fifth Card is " << numberBoxListTail_ptr->getNumber() << factionTail_ptr->getLetter() << endl; 
     } 
    } 
    getch(); 
} 

답변

3
int getLetter() 

이이 경우 잘못된 것입니다. letter이 char 인 경우 문자로 인쇄하려면 char을 반환해야합니다.

char getLetter() { ... } 

당신이 int을 반환하는 경우, std::cout이 카드 순위의 경우와 마찬가지로이 수치 인쇄됩니다. char을 반환하면 숫자가 아닌 해당 문자가 인쇄됩니다.

는 (귀하의 코드. 심각한 리팩토링 작업을 필요로하는 소송을 입력 처리 기능을 확인하고 순위, 그 기능. 그런 다음 네 개의 카드를 읽을 수있는 루프를 수행하는 방법을 알아낼 다시 사용합니다.)

+0

오, 고마워, 나는 눈치 채지 못했다. – JuanDelCarlos

+0

마지막으로 한 가지만 남았습니다. 다섯 번째 카드가 편지를 보여주지 않을 때까지 두 번째. – JuanDelCarlos

+1

스스로 디버깅 할 필요가 있습니다. 코드 자체가 너무 많이 반복되고 다른 것들에 대한 이름이 붙은 변수가있는 경우가 많습니다. 그러니 스스로 고쳐야합니다. 하지만 코드를 살펴보고 factionBodyX_ptr 변수의 순위 또는 슈트를 설정하는지 여부를 확인하십시오. – Mat

관련 문제