Saturday, May 22, 2010

Cin not working (C++)?

When I input a number, nothing happens. Heres my code:





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


using namespace std;





int main()


{


float nValue,


rValue,


newGuess,


guess,


checkNum;





cout %26lt;%26lt; "Enter a positive number (or any letter to quit): ";


cin %26gt;%26gt; nValue;





checkNum = 1.0;


while (!((checkNum %26gt;= -0.0001) %26amp;%26amp; (checkNum %26lt;= 0.0001)))


{


guess = nValue/2.000;


rValue = nValue/guess;


newGuess = (guess+rValue)/2.000;


checkNum = (newGuess-guess)/guess;


}





cout %26lt;%26lt; "\n\nHere is newGuess: " %26lt;%26lt; newGuess;


cout %26lt;%26lt; "\n\nHere is checkNum: " %26lt;%26lt; checkNum;

Cin not working (C++)?
And when you say "nothing happens" you should be more specific. Does the program end? Does it freeze? I can't see it ending without saying anything, but I can see it going into an infinite loop. Also did you mean to put that closing bracket below the couts? Try adding a watch, or a break point and step thru it.
Reply:Cin is working fine in your code....i am not sure what you are trying to accomplish but from what i can see i notice that in your "while" statement your "checkNum" line:





checkNum = (newGuess-guess)/guess;





This line if say you input "2" will never exit the while loop (inifinite loop)...And so your problem is what you are trying to accomplish in your while statement....more specifically that "checkNum" line...





If i new what you were accomplishing here i could fix it but alas i cant at this time until you add more details for me.

baseball cards

No comments:

Post a Comment