Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. can't get out of loop

can't get out of loop

Scheduled Pinned Locked Moved Managed C++/CLI
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10189463
    wrote on last edited by
    #1

    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();

    L 1 Reply Last reply
    0
    • U User 10189463

      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();

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could simplify this by removing all those redundant case blocks and replaing them with a single default:. You could then use your debugger to find out why it does not break out of the loop.

      Use the best guess

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups