강의로 돌아가기
권건우

floating point exception (core dumped)에러 도와주세요 ㅠ

문제를 풀어서 맞긴했는데 입력 방식을 더 간단하게 바꾸고 싶어서 stringstream을 이용해서 다시 짜봤습니다.
그런데 테스트 케이스에서는 오류가 없지만 문제 채점을 해보면 밑의 string code라는 변수에 코드를 넣을때, 가끔 코드값이 들어가지않아 code.size()가 0이 되서 줄 55 부분에서 songtime에 0이 들어가 이 에러가 뜨는것 같습니다.
입력방식에 문제가 있는 건가요? 한참 고민해봤는데 도저히 모르겠네요.. 도와주세요 ㅠ

작성중인 코드―solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <string.h>
#include <vector>
#include <sstream>
#include <iostream>

using namespace std;

void convertCode(string &code)
{
    while (!(code.find("A#") == string::npos &&
        code.find("C#") == string::npos &&
        code.find("D#") == string::npos &&
        code.find("F#") == string::npos &&
        code.find("G#") == string::npos))
    {
        if (code.find("A#") != string::npos) { code.replace(code.find("A#"), 2, "H"); }
        else if (code.find("C#") != string::npos) { code.replace(code.find("C#"), 2, "I"); }
        else if (code.find("D#") != string::npos) { code.replace(code.find("D#"), 2, "J"); }
        else if (code.find("F#") != string::npos) { code.replace(code.find("F#"), 2, "K"); }
        else if (code.find("G#") != string::npos) { code.replace(code.find("G#"), 2, "L"); };
    }
}

string solution(string m, vector<string> musicinfos) {
    string answer = "";
    int beforePlaytime = 0;
    convertCode(m);

    for (auto& music : musicinfos)
    {
        stringstream ss(music);
        char comma, colon;
        int starthour, endhour;
        int startminute, endminute;
        string songnameAndCode;
        string songname;
        string code;

        //03:00,03:30,FOO,CC#B
        //시간 받기
        ss >> starthour >> colon >> startminute >> comma >> endhour >> colon >> endminute >> comma >> songnameAndCode;
        ss.str(songnameAndCode); ss.clear();

        getline(ss, songname, ','); getline(ss, code, '\0'); ss.str(""); ss.clear();

        int playtime = (60 * (endhour - starthour))
            + (endminute - startminute);

        //code에 있는 #을 치환   
        //A# -> H, C# -> I, D# -> J, F# -> K, G# ->L 
        convertCode(code);
        int songtime = (int)code.size();
        string listenCode;

        //오류지점으로 보임
        for (int i = 0; i < playtime; i++)
            listenCode += code[i % songtime];

        //확인
        string::size_type check;
        check = listenCode.find(m);

        //문자열이 존재하고 전에 넣었던 answer에 비해 노래제목이 긴 노래 넣기
        if (check != string::npos)
            if (playtime > beforePlaytime)
            {
                answer = songname;
                beforePlaytime = playtime;
            }
    }

    if (answer.size() == 0)
        answer = "(None)";

    return answer;
}
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다.