[질문]c++ 이 코드가 왜 무한루프에 빠지는지 모르겠습니다.

redpig의 이미지

#include <iostream> 
using namespace::std; 
const int NAME_LEN=20; 
 
/*************** account class ***************/ 
class account { 
    int id;                            //계좌번호 
    int balance;                       //잔액 
    char* name;                        //이름 
public: 
    account() {} 
    account(int id, char* name, int balance); 
    account(const account& acc); 
    ~account(); 
 
    int getID() const;                 //계좌번호 조회 
    int getBalance() const;            //잔액 조회 
    void addMoney(int val);            //입금 
    void minMoney(int val);            //출금 
    const char* getName() const; 
    void showAllData(); 
}; 
 
account::account(int id, char* name, int balance) { 
    this->id=id; 
    this->balance=balance; 
    this->name=new char[strlen(name)+1]; 
    strcpy(this->name, name); 
} 
account::account(const account& acc) { 
    this->id=acc.id; 
    this->balance=acc.balance; 
    this->name=new char[strlen(acc.name)+1]; 
    strcpy(this->name, acc.name); 
} 
account::~account() { 
    delete []name; 
} 
 
int account::getID() const { 
    return id; 
} 
int account::getBalance() const { 
    return balance; 
} 
void account::addMoney(int val) { 
    balance+=val; 
} 
void account::minMoney(int val) { 
    balance-=val; 
} 
const char* account::getName() const { 
    return name; 
} 
void account::showAllData() { 
    cout<<"계좌ID: "<<id<<endl; 
    cout<<"이름: "<<name<<endl; 
    cout<<"잔액: "<<balance<<endl; 
} 
 
/*************** accManager class ***************/ 
class accManager { 
    account* pArray[100];              //account 저장을 위한 배열 
    int index;                         //저장된 account 수 
public: 
    accManager(); 
    void printMenu(); 
    void makeAccount();                //계좌 개설을 위한 함수 
    void deposit();                    //입금 
    void withdraw();                   //출금 
    void inquire();                    //잔액 조회 
}; 
 
accManager::accManager() { 
    index=0; 
}; 
 
void accManager::printMenu() { 
    cout<<"\n---Menu--------"<<endl; 
    cout<<"1. 계좌 개설"<<endl; 
    cout<<"2. 입금"<<endl; 
    cout<<"3. 출금"<<endl; 
    cout<<"4. 잔액 조회"<<endl; 
    cout<<"5. 프로그램 종료"<<endl; 
} 
 
void accManager::makeAccount() { 
    int id; 
    char name[NAME_LEN]; 
    int balance; 
 
    cout<<"계좌 개설--------"<<endl; 
	getchar();
    cout<<"계좌ID: "; 
    cin>>id; 
    cout<<"이름: "; 
    cin>>name; 
    cout<<"입금액: "; 
    cin>>balance; 
 
    pArray[index++]=new account(id, name, balance); 
} 
 
void accManager::deposit() {           //입금 
    int money; 
    int id;                            //찾고자 하는 ID 
 
    cout<<"계좌 ID: "; 
    cin>>id; 
    cout<<"입금액: "; 
    cin>>money; 
 
    for(int i=0; i<index; i++) { 
        if(pArray[i]->getID()==id) { 
            pArray[i]->addMoney(money); 
            cout<<"입금 완료"<<endl; 
            return; 
        } 
    } 
    cout<<"유효하지 않은 ID입니다."<<endl; 
} 
 
void accManager::withdraw() {          //출금 
    int money; 
    int id; 
 
    cout<<"계좌ID: "; 
    cin>>id; 
    cout<<"출금액: "; 
    cin>>money; 
 
    for(int i=0; i<index; i++) { 
        if(pArray[i]->getID()==id) { 
            if(pArray[i]->getBalance()<money) { 
                cout<<"잔액 부족"<<endl; 
                return; 
            } 
 
            pArray[i]->minMoney(money); 
            cout<<"출금완료"<<endl; 
            return; 
        } 
    } 
    cout<<"유효하지 않은 ID입니다."<<endl; 
} 
 
void accManager::inquire() {           //전체 잔액조회 
    for(int i=0; i<index; i++) { 
        pArray[i]->showAllData(); 
    } 
} 
 
/*************** main function ***************/ 
enum {MAKE=1, DEPOSIT, WITHDRAW, INQUIRE, EXIT}; 
 
int main(void) { 
    int choice; 
    accManager manager; 
 
    while(1) { 
        manager.printMenu(); 
        cout<<"선택: "; 
        cin>>choice; 
 
        switch(choice) { 
            case MAKE: 
                manager.makeAccount(); 
                break; 
            case DEPOSIT: 
                manager.deposit(); 
                break; 
            case WITHDRAW: 
                manager.withdraw(); 
                break; 
            case INQUIRE: 
                manager.inquire(); 
                break; 
            case EXIT: 
                return 0; 
            default: 
                cout<<"Illegal selection.."<<endl; 
                break; 
        } 
    } 
    return 0; 
}

Quote:

책을 보면서 c++을 공부하는 중인데요.
위 코드를 컴파일해서 실행하면 main() 함수에서 manager.makeAccount();를 호출하고
void accManager::makeAccount() 함수에서 cin>>id; 여기서 아이디를 입력하고 엔터를 치면
cin>>name; 여기서 이름을 입력받도록 대기해야 되는데 그렇지않고 그냥 무한루프에 빠져버리네요.

혹시 잘못 코딩했나 싶어서 책 소스를 그대로 가져와서 해봐도 마찬가지입니다.
도데체 뭐가 문제일까요?

블루스크린의 이미지

코드를 올리실때는 < c o d e > < / c o d e > 블럭 안에 넣어주세요 - 스페이스는 빼고 입력하세요
---------------------------------------------------------------------------------------------------------
이 댓글(comment)의 수정 및 삭제를 위해 이 글에 답글(reply)를 달지말고 원 글에 댓글(comment)로 달아주세요

-------------------------------------------------------------------------------
이 댓글(comment)의 수정 및 삭제를 위해 이 글에 답글(reply)을 쓰지 말아 주십시요.
의견이 있으시면 원 글에 댓글(comment)로 써 주세요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.