I've been racking my brain on this for hours. The goal of the program is to estimate the value of pi using three different formulas. My arithmetic is fine, I've tested it, and if it is wrong, I can correct that easy. The problem is that my if statements don't seem to be working for me. The program will run with an input file. The file would contain a letter (a, b, or c) and then a number to decide how many terms to use. I have my if statements as if a; if b; and if c.. However every letter i use seems to be recognized as an 'a' . so it's not even using my if statements for b and c. any ideas? I think it's a formatting issue.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{

  double da = 1; //denominator for Aa                                           
  double db = 1; //denominator for Bb                                           
  double dc = 2; //denominator for Cc                                           
  double terms; //# of terms to sum in the series                               
  double sum = 0; //sum of the terms of the series                              
  char series; //char value to determine which series to use                    
  double pi; //estimation of pi                                                 
  double neg = -1; //determine whether value is negative or positive in series  
  double n = -1; //numerator value                                              

  cout << endl;
  cout << setw(9) << left << "SERIES" << setw(7) << right << "# TERMS" <<
    setw(15) << "ESTIMATE" << endl;

  cin >> series;
  while (!cin.eof())
  {
    cin >> terms;
    bool A,a = true;
     if (series == 'A' || 'a')
     {
       for (int i=0; i<terms; i++)
       {
       n = n*neg;
       sum = sum+(n/da);
       da = da+2;
       }
       pi = sum*4;
     cout << fixed << showpoint << setprecision(10);
     cout << pi << endl;
     }
else //series != a or A                                                    
     {
       bool B,b = true;
       if (series == 'B' || 'b')
       {
         for (int i=1; i<=terms; i++)
         {
         n = n*neg;
         db = db*db;
         sum = sum+(n/db);
         db = sqrt(db);
         db = db+1;
         }
         pi = sqrt(12*sum);
       cout << pi << endl;
       }
       else //series != b or B                                                  
       {
         bool c,C = true;
     if (series == 'C' || 'c')
     {
for (int i=0; i<terms; i++)
       {
       n = n*neg;
       dc = dc*dc;
       sum = sum+(n/dc);
       dc = sqrt(dc);
       dc = dc+2;
       }
       pi = sqrt(sum*24);
     cout << pi << endl;
     }
       }
     }
 cin >> series;

  }
}

Try it like this:

if (series == 'A' || series == 'a')

and do the same with the conditionals of b and c

Try it like this:

if (series == 'A' || series == 'a')

and do the same with the conditionals of b and c

So that pretty much solved what I was asking for. But I have come across another problem. My b statements are coming up as -nan. I am new to programming so I don't know what that means.

My b statements are coming up as -nan.

what do you mean by this? what does it print out.
I tried to run the code and it worked fine on my end

And my arithmetic for my b statement is correct, I checked it in a test file and got the correct results. It's just coming up as nan when I use it in this program.

what do you mean by this? what does it print out.
I tried to run the code and it worked fine on my end

I mean when I run the program with my input file of test values.
It's supposed to display numbers. some do, but the b statements display nan..

It's just coming up as nan when I use it in this program

weird it always prints out a numeric value in my end?...
what's the value of term that you input?

I mean when I run the program with my input file of test values.
It's supposed to display numbers. some do, but the b statements display nan..

try do debug it check the values (print them out so you can see) of the variables used when it passes the b conditional to see which causes the problem

weird it always prints out a numeric value in my end?...
what's the value of term that you input?

Okay, I think it has to do with my looping or inputfile. If I regular ./a.out then type in b 7, then I will get the correct value. But when I use ./a.out < inputfile
then it displays the b 7 as nan.

Input File:

a 3
c 10
b    25
b 7

try do debug it check the values (print them out so you can see) of the variables used when it passes the b conditional to see which causes the problem

Sorry, I don't know how to do that.. I want to say it might have something to do with the order my cin statements are taking values. The b conditional might be taking a letter as a number to output nan. Apparently nan means not a number.

My guess is, it is most probably the way it reads the file
print out those variable's value to see if it does take a character so you can see when and how it happens

My guess is, it is most probably the way it reads the file
print out those variable's value to see if it does take a character so you can see when and how it happens

Okay I threw in a print statement inside the b conditional. it displayed the correct numbers..i don't know what I'm doing wrong.

did you try it with every variable present there?

did you try it with every variable present there?

After messing with this, I found a lot of crazy problems.. First of all, if I type in a, I'll get one answer, if I use A, i'll get the wrong answer.

I'm going to rewrite the entire thing and see if it fixes anything. Because of the char types, do you think I need to use bool?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.