By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Program goes into infinite loop

P: 3
first thanks for every one who helps me...
guys..I have to do program that count the value of Pi.
where

pi= 4( 1-(1/3)+(1/5)-(1/7)+((-1)^n/(2n+1))

I did the program but I have problems ...actually I want to learn c++ and this is the frist time for me with this program

this is my code that I did

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <math.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. double n,p;
  10. char ans;
  11. do
  12. {
  13. cout<< " plz enter the value of N \n ";
  14. cin>> n;
  15. p = pow(-1,n)/(2*n + 1);
  16. cout<<" The Value Of Pi is " << "  " << p <<endl;
  17. }
  18. while
  19.  
  20. cout<<" do u want to continue "; <<endl;
  21.  if
  22.  ((ans='y') || (ans='Y'));
  23. cin>>ans;
  24.  
  25.  
  26. getch();
  27. return(0);
  28. }
I have also problem with while loop ... I did try to solve the problem but I could not ...
Mar 11 '07 #1
Share this Question
Share on Google+
5 Replies


P: 93
first thanks for every one who helps me...
guys..I have to do program that count the value of Pi.
where

pi= 4( 1-(1/3)+(1/5)-(1/7)+((-1)^n/(2n+1))

I did the program but I have problems ...actually I want to learn c++ and this is the frist time for me with this program

this is my code that I did

#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;

int main()
{

double n,p;
char ans;
do
{
cout<< " plz enter the value of N \n ";
cin>> n;
p = pow(-1,n)/(2*n + 1);
cout<<" The Value Of Pi is " << " " << p <<endl;
}
while

cout<<" do u want to continue "; <<endl;
if
((ans='y') || (ans='Y'));
cin>>ans;


getch();
return(0);
}

I have also problem with while loop ... I did try to solve the problem but I could not ...


You seemed to have made an error. the coode is wrong i think.
How about this.

Expand|Select|Wrap|Line Numbers
  1. #the headers.
  2.  
  3. int main()
  4. {
  5.  
  6. do
  7. {
  8.     // Pi calculations here
  9.  
  10.      cout<<" The Value Of Pi is " << "  " << total <<endl;
  11.  
  12.      cout<<" do u want to continue "; <<endl;
  13.      cin>>ans;
  14.      if ((ans='y') || (ans='Y'))
  15.         continue;
  16.      else
  17.          return 0;
  18.  
  19. } while(true);
  20.  
  21. return 0;
  22. }
Mar 11 '07 #2

P: 3
thank you my friend ... but I still have the problem .... the program does not work ..
Mar 12 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
thank you my friend ... but I still have the problem .... the program does not work ..
What's the problem? Did that change anything - and if so, what?
Mar 12 '07 #4

P: 3
ok .. I will tell you what the problem is exactly...

I can run the program but in the last part when the user finish using the program he must be shown message if he wants to continue or not...

actuallt the message can be shown but when I click (N) means no
the program gives me uninfinite loooooooooooooop..
and I can't control it ...
Mar 12 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
At the end, the person who gave you your full solution put

ans='y' and ans='Y'

These are, of course, assignment operations and not comparison operations. You need to use == to do this.
Mar 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.