CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: infinate loop

  1. #1
    Join Date
    Jun 2011
    Posts
    5

    infinate loop

    stuck in an infinite loop don't know what im doing wrong.


    #include <iostream> // have to have for output

    #include <string>

    #include <iomanip>

    #include <limits>

    #include <sstream>

    using namespace std;
    int main() // the main function
    {
    float length; // stores lengthint=0
    float width;// stores width
    float depth; //stores depth
    float gallon; //stores the answer in gallons
    cout<< "welcome to the Fish Tank calculator"" " <<'\n';//welcome text
    do
    {
    cout << "enter the length of your fish tank in inches, numbers only" <<'\n';
    cin >> length;
    }

    while (!isdigit(length));
    cout << "Not a number " << endl;

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: infinate loop

    You may want to look up what type of argument isdigit takes.

  3. #3
    Join Date
    May 2000
    Location
    Armenia
    Posts
    201

    Re: infinate loop

    You should give integer code of the character from ANSII character set to isdigit function. But in your code you gave the integer value (length is implicitly cast from float to int).
    You can define your length variable as a string and check first input character with isdigit function:

    Code:
    std::string  length;
    ...
    do
    {
    	cout << "enter the length of your fish tank in inches, numbers only" <<'\n';
    	cin >> length;
    }
    while (!isdigit(length[0]));
    ...
    isdigit can check only 1 character at a time.
    If you want to check ALL characters of length string to be digits, you need to check all of them with isdigit function one by one.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured