Endless loop when entering ch

This is a discussion on Endless loop when entering ch within the C++ Programming forums, part of the General Programming Boards category; Hi programmers. When I choose a character instead of integer the program goes in endless loop how can I control ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Endless loop when entering ch

    Hi programmers.
    When I choose a character instead of integer the program goes in endless loop how can I control this?
    Thanks

    Code:
    // This program displays a menu and asks the user to make a
    // selection. A do-while loop repeats the program until the
    // user selects item 4 from the menu.
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
       int choice;       // Menu choice
       int months;       // Number of months
       double charges;   // Monthly charges
       
       // Constants for membership rates
       const double ADULT = 40.0;
       const double SENIOR = 30.0;
       const double CHILD = 20.0;
    
       // Set up numeric output formatting.
       cout << fixed << showpoint << setprecision(2);
       
       do
       {
          // Display the menu.
          cout << "\n\t\tHealth Club Membership Menu\n\n";
          cout << "1. Standard Adult Membership\n";
          cout << "2. Child Membership\n";
          cout << "3. Senior Citizen Membership\n";
          cout << "4. Quit the Program\n\n";
          cout << "Enter your choice: ";
          cin >> choice;
          
          // Validate the menu selection.
          while (choice < 1 || choice > 4)
          {
             cout << "Please enter 1, 2, 3, or 4: ";
             cin >> choice;
          }
    
          // Validate and process the user's choice.
          if (choice != 4)
          {
             // Get the number of months.
             cout << "For how many months? ";
             cin >> months;
             
             // Respond to the user's menu selection.
             switch (choice)
             {
                case 1:  charges = months * ADULT;
                         break;
                case 2:  charges = months * CHILD;
                         break;
                case 3:  charges = months * SENIOR;
             }
             
             // Display the monthly charges.
             cout << "The total charges are $";
             cout << charges << endl;
          }
       } while (choice != 4);
       return 0;
    }

  2. #2
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    use a char instead of an int for the choice and test for '1', '2', '3', '4'
    or use getline and one of those converter functions, i dont remember what they were called
    here is an idea of the code to use
    Code:
    char choice[100];
    do
    {
          for(int a = 0; a < 100; a++)
                   choice[a] = ' ';
               // the rest of your code
    }
    while(choice != '4')

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    this is not a solution for the problem... thanks any way

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Hello ,

    trying using isdigit(ch) to check if the entered value is a number. then proceed to checking the bounds.
    Include ctypes.h in the header.. !!

    cheers

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Hello ,

    trying using isdigit(ch) to check if the entered value is a number. then proceed to checking the bounds.
    Include ctypes.h in the header.. !!

    cheers

  6. #6
    False Return hubris's Avatar
    Join Date
    Apr 2009
    Posts
    33
    I may be missing something completely (always a disclaimer) but what happened to cin.clear, cin.ignore(100, '\n')? But...like I said. I could be missing something.
    Are unmarried people a kind of "Global Variable"?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    I just wrote some code to try to find out how can I control to much easily. I tried to use isdigit() and cin.clear but is not working. I have been looking for solution for hours. There have to be way to control the input. When I enter character instead of a number it still goes in infinity loop.
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    
    {
    
    int num, total=0;
    
    	cout << "Enter int number";
    	cin >> num;
    
    while (num < 0)
    	{
    	cout << "Enter int number";
    	cin >> num;
    	}
    
    
    	for( int i = 0; i <= num ;i++)
    	total += i;
    	cout << total;
    
    return 0;
    }

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    try..
    Last edited by Eman; 02-21-2011 at 12:23 AM.
    You ended that sentence with a preposition...Bastard!

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    this should work:
    Code:
     while(getchar()!='\n') ;//put this year after initial initial input
     while (choice < 1 || choice > 4)
    {
          {
             cout << "Please enter 1, 2, 3, or 4: ";
             while(getchar()!='\n') ;//and here
    		 
    		 
          }
    	getchar() ;
    }
    Last edited by Eman; 02-21-2011 at 12:21 AM.
    You ended that sentence with a preposition...Bastard!

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Oh boy. First char arrays, now C code.
    OK, first, don't use char arrays.
    Secondly, don't use getchar to clear the input buffer. This cannot be guaranteed to work properly...
    The most beginner friendly way of doing this is... read into a char. Yes. A char.
    Then test with isdigit if that char is a number. If not, print an error, and redo it all.

    char c;
    cin >> c;
    if (!isdigit(c)) cout << "Please enter a number!";
    ...
    Last edited by Elysia; 02-21-2011 at 12:49 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    I usually wouldn't just post a corrected version of something that looks like an assignment, but this is a pretty trivial change. You can do comparisons with chars, so e.g. you can make choice a char and do:
    Code:
    while (choice < '1' || choice > '4')
    Basically I've just made choice into a char, made it compare with other chars and put chars into your switch statement.

    Code:
    // This program displays a menu and asks the user to make a
    // selection. A do-while loop repeats the program until the
    // user selects item 4 from the menu.
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
       char choice;       // Menu choice
       int months;       // Number of months
       double charges;   // Monthly charges
       
       // Constants for membership rates
       const double ADULT = 40.0;
       const double SENIOR = 30.0;
       const double CHILD = 20.0;
    
       // Set up numeric output formatting.
       cout << fixed << showpoint << setprecision(2);
       
       do
       {
          // Display the menu.
          cout << "\n\t\tHealth Club Membership Menu\n\n";
          cout << "1. Standard Adult Membership\n";
          cout << "2. Child Membership\n";
          cout << "3. Senior Citizen Membership\n";
          cout << "4. Quit the Program\n\n";
          cout << "Enter your choice: ";
          cin >> choice;
          
          // Validate the menu selection.
          while (choice < '1' || choice > '4')
          {
             cout << "Please enter 1, 2, 3, or 4: ";
             cin >> choice;
          }
    
          // Validate and process the user's choice.
          if (choice != '4')
          {
             // Get the number of months.
             cout << "For how many months? ";
             cin >> months;
             
             // Respond to the user's menu selection.
             switch (choice)
             {
                case '1':  charges = months * ADULT;
                         break;
                case '2':  charges = months * CHILD;
                         break;
                case '3':  charges = months * SENIOR;
             }
             
             // Display the monthly charges.
             cout << "The total charges are $";
             cout << charges << endl;
          }
       } while (choice != '4');
       return 0;
    }

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    Thanks Mozza314, this is not assignment. I am reading a book and try to teach myself (starting out with c++ from control structures trough objects) .The way of replacing all choice with '1' as ch var, I get it and it works. But what about if you want to make calculations as svetlinís example.
    I tried what Elysia suggested and it works but still assigns the var to a char. What if I want to use that variable for calculations?

  13. #13
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by sid13 View Post
    I tried what Elysia suggested and it works but still assigns the var to a char. What if I want to use that variable for calculations?
    Declare another variable.
    I never put signature, but I decided to make an exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with this endless loop
    By xkohtax in forum C Programming
    Replies: 4
    Last Post: 05-01-2009, 02:32 PM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 04:01 PM
  4. A few questions...
    By Tride in forum C Programming
    Replies: 15
    Last Post: 09-25-2003, 02:26 AM
  5. Endless loop!
    By Paninaro in forum C Programming
    Replies: 3
    Last Post: 06-23-2002, 08:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21