Program enters Infinite loop; need help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Program enters Infinite loop; need help

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Angry Program enters Infinite loop; need help

    Okay, my problem is that when people enter a letter when asked for what they know (either Radius, Diameter, or Circumference; it needs to be only a number) the program enters an infinite loop. I need to have it only allow numerical entry, and deny character input for that value. How can I make it do that? For reference, the in statement for Case 1 is Radius; Case 2 is Diameter; Case 3 is Circumference.

    Below is my .cpp file.

    I don't know too many things about C++ because I have only been learning it for 1 month. Any help would be appreciated
    __________________________________________________________________________

    /*
    Name: Circle Radius/Circumference/Diameter Calculator
    Copyright: 2011
    Author: Matthew Mirarchi
    Date: 27/10/11 17:58
    Description: Version 1.0.4 - Will solve for Radius/Circumference/Diameter of
    a circle only given one.
    */
    #include <iostream> // Needed for output statements
    #include <stdio.h> // Needed for pause command system -> ("pause"); executes M$-DOS' pause command
    #include <string.h>
    #include <windows.h> // Used for string commands
    #include <iomanip>
    #include <limits> //Needed for command used to prevent users from entering a letter for choice
    using namespace std;

    int main()
    {
    long double Radius;
    long double Diameter;
    long double Circumference;
    const long double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286;

    {
    int choice;
    bool ProgramOn = true; //Start condition for program to run
    while (ProgramOn != false) // While = false, program will run
    {

    cout << setw(70) << "*************************************************************\n";
    cout << setw(70) << "************* Enter Number of Desired Choice ****************\n";
    cout << setw(70) << "*************************************************************\n";
    cout << setw(70) << "* *\n";
    cout << setw(70) << "* 1 - Calculate Diameter and Circumference if Radius known. *\n";
    cout << setw(70) << "* 2 - Calculate Circumference and Radius if Diameter known. *\n";
    cout << setw(70) << "* 3 - Calculate Diameter and Radius if Circumference known. *\n";
    cout << setw(70) << "* 4 - Help/Info. *\n";
    cout << setw(70) << "* 5 - Exit. *\n";
    cout << setw(70) << "* *\n";
    cout << setw(70) << "*************************************************************\n";
    cout << endl;
    cout << setw(50) << " Enter your choice and press return: ";
    cin >> choice;
    cin.ignore(2, '\n'); // User enters menu choice

    cout << endl;

    cout << "\n";

    switch (choice) //switch for menu start
    {

    case 1:
    cout << "This choice calculates the diameter and circumference of a circle if a radius is known.\n" << endl;
    cout << "What is the radius?";
    cout << endl;
    cin >> Radius; //user input radius
    cout << endl;
    //calculations
    Diameter = Radius * 2;
    Circumference = Diameter * pi;
    //output
    cout << setprecision(10) << "The circumference of the circle is " << Circumference
    << " and \n the diameter of the circle is " << Diameter << endl;
    cout << endl;
    cout << endl;
    break;

    case 2:
    cout << "This choice calculates the radius and circumference of a circle if the diameter is known.\n" << endl;
    cout << "What is the diameter?";
    cout << endl;
    cin >> Diameter;
    cout << endl;
    //calculations
    Radius = Diameter / 2;
    Circumference = Diameter * pi;
    //output
    cout << setprecision(10) << "The circumference of the circle is " << Circumference
    << " and \n the radius of the circle is " << Radius << endl;
    cout << endl;
    cout << endl;
    break;

    case 3:
    cout << "This choice calculates the diameter and radius of a circle if the circumference is known.\n" << endl;
    cout << "What is the circumference?";
    cout << endl;
    cin >> Circumference;
    cout << endl;
    //calculations
    Diameter = Circumference / pi;
    Radius = Diameter / 2;
    //output
    cout << setprecision(10) << "The Radius of the circle is " << Radius
    << " and \n the diameter of the circle is " << Diameter << endl;
    cout << endl;
    cout << endl;

    break;

    case 4:
    //help file/about
    cout << endl;
    cout << "Will solve for an unknown Radius, Diameter, and Circumference of a circle. \n";
    cout << "Dev-C++ 4.9.9.2 was used to write and compile this. \n"; //Writes Text
    cout << " \n"; //Creates a line break
    cout << "This was written by Matthew Mirarchi. \n";
    cout << " \n";
    break;

    case 5:
    // end program
    cout << "End of Program.\n";
    ProgramOn = false;
    break;
    default:
    //Prevents user from entering any number except 1,2,3,4,5
    if (choice == 6||7||8||9||10)
    {

    goto insrtgoto;
    }
    else{

    while (!(cin >> choice))
    {
    insrtgoto:
    cout << "Incorrect character entered." << endl;
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << endl;
    break;
    }
    break;
    }
    }

    }
    return 0;
    }
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Program enters Infinite loop; need help

    If you are trying to read to an int, but the user enters a character instead, the stream (cin) enters a failure state. All future operations will fail until (a) the character is successfully read, and (b) cin.clear() is called to clear the error.

    It is in general much easier to do your reading into a char or std::string, and then convert to an integer afterwords.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Program enters Infinite loop; need help

    1) Please use code tags when posting code. The code you posted is practically unreadable.

    [code] Your code goes here [/code]

    2) Explain what this is supposed to do:
    Code:
    if (choice == 6||7||8||9||10)
    I bet it isn't doing what you think it's supposed to do.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center