這是我寫過的第一個C++程序,我很難理解操作數必須放入的順序。這是爲了類,但看起來我不應該使用作業標籤。對不起,如果我做錯了。一個程序,用戶輸入一個字符串,程序統計字母的實例
這是我輸入
// Get DNA string
string st;
cout << "Enter the DNA sequence to be analysed: ";
cin >> st;
這似乎是工作不錯,但我想我會包括它櫃面這是我做錯了。
這是我到目前爲止檢查輸入是否是C,T,A或G. 它運行程序並簡單地打印「請輸入一個有效的sequnce1,請輸入一個有效的序列2,。 ..等。我知道我在做一些非常愚蠢的,我只是不明白。
// Check that the sequence is all C, T, A, G
while (i <= st.size()){
if (st[i] != 'c' && st[i] != 'C' && st[i] != 'g' && st[i] != 'G' && st[i] != 't' && st[i] != 'T' && st[i] != 'a' && st[i] != 'A');
cout << "Please enter a valid sequence" <<
i++;
else if (st[i] == c,C,G,t,T,a,A)
i++;
我計劃的第二部分是計算序列中C和G的數量
for (i < st.size() ; i++ ;);
for (loop <= st.size() ; loop++;)
if (st[loop] == 'c')
{
count_c++;
}
else if (st[loop] == C)
{
count_c++;
}
else if (st[loop] == g)
{
count_g++;
}
else if (st[loop] == G);
{
count_g++;
}
cout << "Number of instances of C = " << count_c;
cout << "Number of instances of G = " << count_g;
它看起來好像不循環,它會計算其中一個字母1秒。我如何讓它循環?我似乎無法投入endl;任何地方都不會收到錯誤,儘管我知道我會在某個地方需要它。
任何幫助或提示指向我在正確的方向將不勝感激 - 我一直在這個代碼工作兩天(這是承認尷尬)。
編輯:
我的序列檢驗看起來像現在這樣:
while (i < st.size()) {
if (st[i] != c && st[i] != C && st[i] != g && st[i] !=G && st[i] !=t && st[i] !=T && st[i] !=a && st[i] != A)
cout << "Please enter a valid sequence" << "\n" << "\n";
i++;
}
和我的櫃檯看起來是這樣的:
// Count the number of Cs and Gs
count_c = 0;
count_g = 0;
for (i = 0; i < st.size() ; i++) {
if ((st[i] == 'c') || (st[i] == 'C'))
count_c++;
else if ((st[i] == 'g')|| (st[i] == 'G'));
count_g++;
}
cout << "Number of instances of C = " << count_c;
cout << "Number of instances of G = " << count_g;
你的for循環失蹤的第一部分。 – chris
這段代碼不會編譯,即使它做到了,它也不會做你聲稱它的事情 –
我並不是說它做任何事情!我在尋求幫助。現在它正在爲我編譯 - 但是它重複了我的gs,我會在一分鐘內編輯我的文章。 – stringgy