2017-02-27 1 views
-1

Accelerated C++의 연습 문제를 해결하고 while 루프에서 문자열을 읽는 데 문제가 있습니다. 이 코드는 읽는 사람의 이름이 어느 정도 선택적인 것으로 보입니다. 왜 이것이 끊어지고있는 아이디어가 있습니까? 나는 그것을 디버깅하기 위해 Xcode에 들어갔다. 단지 성명이 읽혀지지 않은 것 같다.cin 전체 문자열을 읽지 않습니다.

코드의 아이디어는 학생 이름을 입력하고 중간 학년, 최종 시험 최종 점수를 계산하기 위해 일련의 숙제 점수를받습니다. 그런 다음 이름 옆에 최종 표시가있는 알파벳 순서로 이름을 반환하며 서식이 지정되어 최종 표시가 단일 열에 표시됩니다. (주)

GradeFinder.ccp

#include <algorithm> 
#include <iomanip> 
#include <ios> 
#include <iostream> 
#include <stdexcept> 
#include <string> 
#include <vector> 
#include "grade.cpp" 


using std::cin; 
using std::cout; 
using std::endl; 
using std::domain_error; 
using std::max; 
using std::setprecision; 
using std::sort; 
using std::streamsize; 
using std::string; 
using std::vector; 

int main() 
{ 
    vector<Student_info> students; 
    Student_info record; 
    string::size_type maxlen=0; 

    while (read(cin,record)) 
    { 
    maxlen=max(maxlen,record.name.size()); 
    students.push_back(record); 
    } 

    sort(students.begin(),students.end(),compare); 

    for (vector<Student_info>::size_type i=0; i!=students.size();++i) 
    { 
    cout<<students[i].name 
     <<string(maxlen+1-students[i].name.size(),' '); 

     try 
     { 
     double final_grade=grade(students[i]); 
     streamsize prec=cout.precision(); 
     cout<<setprecision(3)<<final_grade 
      <<setprecision(prec); 
      } 
     catch (domain_error e) 
     { 
      cout<<e.what(); 
     } 
     cout<<endl; 
    } 
return 0; 
} 

'Student_info.h'

#ifndef GUARD_Student_info 
#define GUARD_Student_info 

// Student_info.h header file 
#include <iostream> 
#include <string> 
#include <vector> 

struct Student_info { 
    std::string name; 
    double midterm, final; 
    std::vector<double> homework; 
}; 

bool compare(const Student_info&, const Student_info&); 
std::istream& read(std::istream&, Student_info&); 
std::istream& read_hw(std::istream&, std::vector<double>&); 

#endif // GUARD_Student_info 

Student_info.cpp

#include "Student_info.h" 

using std::istream; 
using std::vector; 

bool compare(const Student_info& x, const Student_info& y) 
{ 
    return x.name<y.name; 
} 

istream& read(istream& is, Student_info& s) 
{ 
    is >> s.name >> s.midterm >> s.exam; 
    read_hw(is,s.homework); 
    return is; 
} 

istream& read_hw(istream& in, vector<double>& hw) 
{ 
    if (in) 
    { 
     hw.clear(); 

     double x; 
     while (in>>x) 
     hw.push_back(x);  
    in.clear(); 

    } 
    return in; 
}  

제 (이름과 관련된 마크의 커맨드 라인 입력을 판독한다) 다음은 명령 행 입력의 예입니다. 흥미롭게도 이름 페넬로페는 처음 이름을 입력하지 않고 조니가 항상 괜찮을 때 문제를 일으 킵니다.

Chapter4 Alex$ ./GradeFinder 
Johnny 90 90 90 90 90 
Frederick 80 80 80 80 80 
Penelope 85 85 85 85 85 
Polly 95 95 95 95 95 95 
Johnny 90 
lope  85 
olly  95 
rederick 80 

Chapter4 Alex$ ./GradeFinder 
Penelope 85 85 85 85 85 
Frederick 80 80 80 80 80 80 
Polly 95 95 95 95 95 95 
Johnny 90 90 90 90 90 90 
Johnny 90 
Penelope 85 
olly  95 
rederick 80 
+2

당신은 모든 독서를 위해서'std :: istream :: operator >>'를 사용하고 있습니다. 'operator >>'는 줄 바꿈을 무시하고 공백을 만났을 때 문자열 읽기를 멈 춥니 다. 또한 시험 또는 숙제 점수가 포함되지 않은 몇 줄에서 읽기가 실패합니다. 따라서 당신은 당신이 기대하는 것을 항상 읽지는 않습니다. 먼저'std :: getline()'을 사용하여 전체 텍스트 행을'std :: string'으로 읽어 들이고'std :: istreamstream'을 사용하여 필요에 따라 그 행에서 개별 값을 읽는 것을 고려하십시오. –

+0

프로그램 동작을 이해하는 데 필요한 파일 인 student_info.h가 포함되도록 편집되었습니다. 전체 소스는 https://github.com/acarabott/accelerated-cplusplus/tree/master/04에 있습니다. – Squirrel

+0

[mcve] 만 포함하도록 질문을 수정 해보십시오. 실제로 2 개의 기능 만있는 것처럼 보입니다. istream에서 읽기 때문에 코드의 수를 줄여야합니다. – zett42

답변

0

이 문제를 보인다는 libc++을 사용하여 특정 문자를 삼키는 것이다 (A, B, C, D, E, F, I, N, P, X)가 이름의 시작에서 발생할 때 .

컴파일을 libstdc++으로 전환하면 더 이상 이름에서 문자를 제거하지 않습니다. 그러나 나는 여전히 libc++을 사용하면서이 문제를 해결하는 방법을 모르겠습니다.

동일한 문제에 대한 답변은 here을 참조하십시오. Mac 관련 문제 일 것 같습니다.

관련 문제