简体   繁体   English

从文件中读取字符串

[英]Reading string from file

I've been stuck on this for a while. 我已经坚持了一段时间。 I'm trying to read a string from a file. 我正在尝试从文件中读取字符串。 The string contains notes (A, G, C, B, etc.) which may be followed by certain values. 该字符串包含注释(A,G,C,B等),后面可能跟有某些值。 For example, when a note is followed by '~', the note has added 20 to its duration (A~~ adds 40 to the preset duration). 例如,当音符后跟“〜”时,该音符的持续时间增加了20(A ~~将预设持续时间增加了40)。 Other possible inputs: A(4) should change the frequency by +4. 其他可能的输入:A(4)应将频率更改+4。 B# should change the frequency by +20. B#应将频率更改+20。

The input would look something like this: B(4)~F#(5)~~~Aa 输入看起来像这样:B(4)〜F#(5)~~~ Aa

I started by doing the following but I can't seem to figure out how to increment frequency with each additional '~' after a note. 我从执行以下操作开始,但是我似乎无法弄清楚在注释后如何以每增加一个“〜”来增加频率。

string a;
for (int i = 0; i < a.length(); a++)
{
    if(a.at(i) == 'C')
    {
        frequency = 440;
        duration = 10;

        if(a.at(i++) == '~')
        {
            frequency += 20;
        }
    }
Values(frequency, duration)

}

The point in your loop that checks for a '~' is only reached when a.at(i) == C, and only tests i++. 仅当a.at(i)== C且仅测试i ++时才到达循环中检查“〜”的点。 add within the conditional for == '~' a loop: 在=='〜'的条件内添加一个循环:

if(a.at(i++) == '~'){
    while(a.at(i) == '~'){
        frequency += 20;
        i++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM