无法摆脱循环 [英] can't get out of loop

查看:80
本文介绍了无法摆脱循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上有两个问题。如果我输入gcgc,它应该在计算G和C时给我4,但它给我3代替。此外,程序不会自行停止(我必须按控制C才能退出)。我的代码出了什么问题?



我的代码的全部内容是搜索DNA序列并找到序列中G和C的数量。如果用户输入的不是核苷酸序列,那么会出现错误,告诉您输入正确的序列。如果有人能为我找到一种方法来搜索CG的数量(C直接跟随G),那也会很棒!



I actually have 2 problems. If i enter gcgc, it should give me 4 when counting G's and C's but it gives me 3 instead. Also, the program doesn't stop by itself (I have to press control C to get out). What is wrong with my code?

The whole point of my codes are to search through a DNA sequence and find the number of G's and C's in the sequence. If the user inputs other than the nucleotide sequence, then there will be an error telling you to put the correct sequence. If anybody can find a way for me to also search the number of CG (C directly followed by G), that would be great too!

#include <iostream>
#include <algorithm> 


using namespace std;


int main() {

char input = ' ';
 int count = 0;
char newinput = ' ';
 int newcount = 0;

 cout << "Enter the DNA strand:";
 cin >> input;


while (input != '\n')
 {cin.get(input);
   switch (toupper(input))
{
case 'C': 
    count++;
    break;
    case 'G':
    count++;
    break;}}
 cout << "There are " << count << " C's and G's in this DNA strand."<< endl;


while (input != '\n')
 {cin.get(input);
   switch (toupper(input))
 {
case 'B':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin>>newinput;
cin.clear();
cin.ignore('\n');

  break;
case 'D':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'E':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'F':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'H':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'I':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'J':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'K':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'L':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'M':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'N':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'O':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'P':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'Q':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'R':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'S':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'U':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'V':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'W':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'X':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'Y':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
case 'Z':
    cout << "Invalid sequence! Please enter the correct nucleotide sequence:"; 
cin.clear();
  cin.ignore('\n');
  break;
}
}


while (newinput != '\n') 
 { cin.get(newinput);
   switch (toupper(newinput))
  { case 'C': 
    newcount++;
    break;
    case 'G':
    newcount++;
    break;
}
}

 cout << "There are " << newcount << " C's and G's in this DNA strand." << endl;



return 0;
}

推荐答案

1。数量为3:

您在while循环之前读取了第一个字符,但不计算它。第一次执行循环时,读取第二个字符,计数为1,然后读取第三个字符,计数到2,然后读取最后一个字符,并计数到3.



2.无限循环:

你告诉cin忽略'\ n'。您如何期望您的变量输入能够持有'\ n'?



3。额外建议:

a)除上述建议外,不要使用开关,不要默认!如果用户输入3会发生什么?如果他进入'ñ'或'ä'会怎样?在第二个while循环中检查不可感知的输入是很好的,但是你必须检查所有可能的输入值! 默认为你做这件事。



b)每行输入一个字符序列非常繁琐。为什么不让用户输入整个字符串作为一个输入? GCGGG \ n代替G\\\
C\ nGG \ nGG \ nn?



c)程序逻辑:
根据您的描述,您的程序会检查输入。但是,检查是在第二个循环中实现的,输入是您正在处理的输入的独立输入。你的progeram中的执行顺序与你想要的算法不符,也不是真的合理,因为即使输入了正确的输入,它也会被丢弃。



d)格式化:

请尝试格式化代码,使得同一嵌套级别的行从同一列开始。它将大大提高可读性。



同时将关闭'}放在一个单独的行上。每行一行!这样可以更容易地发现代码块的结束。



P.S。:

我没有评论你的第三个循环。由于与上述3.c)相关的原因,这完全是荒谬的。如果你无法在程序中正确表达嵌套的控制流,那么SA是对的:那不是程序。
1. count of 3:
You read the first character before the while loop, but don't count it. The first time you do the loop, you read the second character, count to 1, then you read the third character, count to two, then read the last character, and count to 3.

2. endless loop:
You tell cin to ignore '\n'. How do you expect your variable input will ever be able to hold '\n'?

3. Bonus advice:
a) Apart from the above advice, never use switch without default! What happens if the user enters '3'? What happens if he enters 'ñ' or 'ä'? It's good that you check for insensible input in the second while loop, but you've got to check all possible input values! default does that for you.

b) Entering a sequence of characters at one character per line is quite tedious. Why don't you let the user enter the entire string as one input? "GCGGG\n" instead of "G\nC\nG\nG\nG\n"?

c) program logic:
According to your description your program checks the input. However the checking is implemented in a second loop, on input that is independend of the input that you are processing. The order of execution in your progeram does not correspond to your intended algorithm, nor is it really sensible, since even if you do enter correct input, it will be discarded.

d) formatting:
Please try formatting your code in such a way that lines on the same level of nesting start at the same column. It will greatly increase readability.

Also put closing '}'s on a separate line. One line for each! That makes it easier to spot the end of a code block.

P.S.:
I didn't comment on your third loop. It's completely nonsensical for reasons related to 3.c) above. If you can't properly express the nested flow of control in a program, then SA is right: that is not a program.


这篇关于无法摆脱循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆