Hi, i am trying to make a BMI calulator and here is my code:

#include<iostream>
#include<sstream>
#include<fstream.h>
using namespace std;
int main (char argc)
{
    double ff1;
    double ii1;
    double ww1;
    for(;;)
    {
    system("cls");
    cout << "Enter feet." << endl;
    string f1;
    cin >> f1;
    stringstream(f1) >> ff1;
    if(ff1 >! -999999999) // I wanted to use if(stringstream(f1).fail()) but that didnt work
    {
    
     if(f1 == "u")
     {
              fstream feet;
              feet.open("feet.txt");
              feet >> ff1;
              
     }
     
     if(f1 == "uc")
     {
              fstream feet1;
              feet1.open("feet.txt");
              cout << "Enter new default feet." << endl;
              double f2;
              cin >> f2;
              feet1 << f2;
              ff1 = f2;
     }
    }
    else
    {
              stringstream(f1) >> ff1;
    }
    cout << ff1 << endl;
           
    
    cout << "Enter inches." << endl;
    string i1;
    cin >> i1;
    stringstream(i1) >> ii1;
    if(ii1 >! -999999999)
    {
    
     if(f1 == "u")
     {
              fstream inches;
              inches.open("inches.txt");
              inches >> ii1;
     }
     
     if(f1 == "uc")
     {
              fstream inches1;
              inches1.open("inches.txt");
              cout << "Enter new default inches." << endl;
              double i2;
              cin >> i2;
              inches1 << i2;
              ii1 = i2;
     }
    }
    else
    {
              stringstream(i1) >> ii1;
    }
    
    cout << "Enter weight." << endl;
    string w1;
    cin >> w1;
    stringstream(w1) >> ww1;
    if(ww1 >! -999999999)
    {
    
     if(f1 == "u")
     {
              fstream weight;
              weight.open("weight.txt");
              weight >> ii1;
     }
     
     if(f1 == "uc")
     {
              fstream weight1;
              weight1.open("weight.txt");
              cout << "Enter new default weight." << endl;
              double w2;
              cin >> w2;
              weight1 << w2;
              ww1 = w2;
     }
    }
    else
    {
              stringstream(w1) >> ww1;
    }
    
    ff1 = ff1 * 12;
    ff1 = ff1 + ii1;
    ff1 = ff1 * ff1;
    
    ww1 = ww1 * 703;
    
    double bmi = ww1 / ff1;
    
    cout << "Your BMI is " <<bmi<< endl;
    system("pause");
}
}

Whenever i enter u or uc as the feet, inches, or weight, the program doesnt run that loop. Also, i dont think that the program is reading the text files. Any solutions?

Edited 5 Years Ago by thecoolman5: n/a

You can try debugging the program. Put different segments of the program into different functions and call them from the main() function. That way, you can know what segment the problem is coming from. For the debugging, you can output the values of the different variables to screen so that you can see which variable has a wrong value that might be affecting the program.

I havent gone through the program though.

cout is the easiest debugging tool you have.

You suspect the file is not being read? After the open, display the status of the open -- did it work? After each read, display the data read -- is it correct?

You suspect u and uc don't work? Display values before and after IF's to see if they worked properly. Display messages to follow the flow of the program.

In other words, follow the program by outputting key data and positional statements.

What exactly does this do? if(ff1 >! -999999999)

I enter u and it stringstreams that. I don't know why but the number comes out very large and if the number isnt greater than -999999999 then run the default numbers. When i enter u, its supposed to use the default number in the text file. And uc changes that default number.I'll add functions and see if it works. Does anyone know how to detect if an inputed string is not a number?

I enter u and it stringstreams that. I don't know why but the number comes out very large and if the number isnt greater than -999999999 then run the default numbers. When i enter u, its supposed to use the default number in the text file. And uc changes that default number.I'll add functions and see if it works. Does anyone know how to detect if an inputed string is not a number?

Sorry could you explain that in better English? Basically I'm wondering what: "greater than" "logical not" means.

ok, i took as much time as i could and i found all the problems and got rid of them. Heres the code if you guys want to try:

#include<iostream>
#include<sstream>
#include<fstream.h>
using namespace std;
int main (char argc)
{
    double ff1;
    double ii1;
    double ww1;
    for(;;)
    {
    system("cls");
    
    cout << "Enter the letter u and it will use the default number." << endl;
    cout << "Enter the letters uc and you can change the default number." << endl;
    cout << "You might want to set the default number on the first time you run the program." << endl;
    cout << "Enter feet." << endl;
    string f1;
    cin >> f1;
    
    if(f1 == "u")
    {
          ifstream feet;
          feet.open("feet.txt");
          feet >> ff1;
          
              
    }
    else
    {
        stringstream(f1) >> ff1;
    }
     
    if(f1 == "uc")
    {
          ofstream feet1;
          feet1.open("feet.txt");
          cout << "Enter new default feet." << endl;
          double f2;
          cin >> f2;
          feet1 << f2;
          ff1 = f2;
    }
    else
    {
              stringstream(f1) >> ff1;
    }
           
    
    cout << "Enter inches." << endl;
    string i1;
    cin >> i1;
    if(i1 == "u")
    {
              ifstream inches;
              inches.open("inches.txt");
              inches >> ii1;
     }
     else
     {
         stringstream(i1) >> ii1;
     }
     
     if(i1 == "uc")
     {
              ofstream inches1;
              inches1.open("inches.txt");
              cout << "Enter new default inches." << endl;
              double i2;
              cin >> i2;
              inches1 << i2;
              ii1 = i2;
     }
    else
    {
              stringstream(i1) >> ii1;
    }
    
    cout << "Enter weight." << endl;
    string w1;
    cin >> w1;
     if(w1 == "u")
     {
              ifstream weight;
              weight.open("weight.txt");
              weight >> ww1;
     }
     else
     {
              stringstream(w1) >> ww1;
     }
     
     if(w1 == "uc")
     {
              ofstream weight1;
              weight1.open("weight.txt");
              cout << "Enter new default weight." << endl;
              double w2;
              cin >> w2;
              weight1 << w2;
              ww1 = w2;
     }
    else
    {
              stringstream(w1) >> ww1;
    }
    
    ff1 = ff1 * 12;
    ff1 = ff1 + ii1;
    ff1 = ff1 * ff1;
    
    ww1 = ww1 * 703;
    
    double bmi = ww1 / ff1;
    
    cout << "Your BMI is " <<bmi<< endl;
    if(bmi < 18.5)
    {
           cout << "You are underweight." << endl;
    }
    else if(bmi > 18.5)
    if(bmi < 25)
    {
         cout << "You are healthy." << endl;
    }
    else if(bmi > 25)
    if(bmi < 30)
    {
         cout << "You are overweight." << endl;
    }
    else if(bmi > 30)
    {
         cout << "You are obese." << endl;
    }
    system("pause");
}
}
This question has already been answered. Start a new discussion instead.