can't get out of loop
-
Hi, 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?
#include #include 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(); -
Hi, 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?
#include #include 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();