|
-
June 26th, 2011, 05:50 PM
#1
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;
-
June 26th, 2011, 06:47 PM
#2
Re: infinate loop
You may want to look up what type of argument isdigit takes.
-
June 26th, 2011, 08:43 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|