-1

My program runs the first For loop correctly then skips the Cin's on the 2nd and 3rd cycle. Then when the loop is finished it goes on to calculate the BMI of the first index [0] and does this correctly and gives the right answer but then nothing for the index's 1 and [2] because no information was inputted because the cin's were skipped.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

struct Patient
{
  double height;
  double weight;
  int age;
  bool isMale;
};


int main()
{
Patient Patients[3];

for (int i = 0; i < 3; i++) {
    cout << "Patient "<< i << " Height: ";
    cin >> Patients[i].height;
    cout << "Patient " << i << " Weight: ";
    cin >> Patients[i].weight;
    cout << "Patient " << i << " Age: ";
    cin >> Patients[i].age;
    cout << "Is Patient " << i << " Male True or False: ";
    cin >> Patients[i].isMale;
    cout << endl << endl;

   }

    cout << endl << endl;

    for (int i = 0; i < 3; i++) {
        float BMI = Patients[i].weight / (Patients[i].height *Patients[i].height);
        cout << "Patient " << i << " Has A BMI of: " << BMI << endl << endl;
      }

return 0;
 }

This is the console where you can see after the first loop all the cin's are skipped but the first loop was correctly stored as it couted the BMI of the first index:

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 1
    Everything looks fine based on the code you posted. Have you tried stepping through your code with a debugger to see what is going on? – Andy Feb 03 '17 at 20:27
  • 3
    related/dupe: http://stackoverflow.com/questions/26203441/cin-and-boolean-input – NathanOliver Feb 03 '17 at 20:28
  • 1
    Show the input and output. Sometimes the type that you enter can cause problems if it's incorrect. Also try printing out the cin status bits. Also run the debugger as suggested. – sbail95 Feb 03 '17 at 20:29
  • @Andy Yes, it correctly takes all the cin's on the first cycle and stores them in index [0], but when it iterates through [1] and [2] theres no prompt for user input so nothing is stored – Elliot Lee Feb 03 '17 at 20:35
  • Please take screenshots instead of taking photos of your screen. To take screenshots on Windows, you can press the Print Screen key and then paste into Paint. – Donald Duck Feb 03 '17 at 21:08
  • Or even better copy the text directly from the console. – HolyBlackCat Feb 03 '17 at 21:15
  • From what I understand (someone correct me if I'm wrong): When you try to read into the `bool`, `cin` will look for either 0 or 1. If you're entering in "true" or "false" the input will fail and leave `cin` in a fail state. The rest of the input operations are then ignored. If you enter in 0 or 1 for the gender then it should work. Note that you can test `cin` to see if a read operation succeeded or not, and you can use `cin.clear()` to reset the state. – Greg Kikola Feb 03 '17 at 21:18
  • Enter `1` or `0` instead of `true` or `false`. – Raindrop7 Feb 03 '17 at 21:50

2 Answers2

1

You can fix your program in two ways.

  1. Just input "0" or "1" in the male/female question instead of "true" or "false".
  2. Change this line and continue to input "true" or "false":

    cin >> boolalpha >> Patients[i].isMale;
    

Sources:

Cin and Boolean input

http://www.cplusplus.com/reference/ios/boolalpha/

Community
  • 1
  • 1
1

You see that you are having an error at the end of the loop. You can see from iterations 2 and 3 that cin is not behaving the same way each time. There are a couple of error state flags that come from ios that will help you see what's going wrong here. See iso::good for details. If you add those checks:

for (int i = 0; i < 3; i++) {
cout << "Patient " << i << " Height: ";
cin >> Patients[i].height;
cout << "Patient " << i << " Weight: ";
cin >> Patients[i].weight;
cout << "Patient " << i << " Age: ";
cin >> Patients[i].age;
cout << "Is Patient " << i << " Male True or False: ";
cin >> Patients[i].isMale;
cout << cin.good() << '\n';
cout << cin.eof() << '\n';
cout << cin.fail() << '\n';
cout << cin.bad() << '\n';
cout << endl << endl;
}

What you will see if that cin is no longer good, it is not eof it is fail, and it is not bad. While the fail bit is set, cin will not work. Hence you see the result. Looking at the chart in the link you see this:

Logical error on i/o operation

You were preforming an i/o operation of inserting "true" into a bool. The word true is probably stored as a character array or string, not a boolean. How should cin convert this to a boolean? You need to trap your input and convert it into a bool or switch to use an input that can be explicitly converted into a bool.

For example:

cout << "Is Patient " << i << " Male? (1 for Male, 0 for Female):";
cin >> Patients[i].isMale;

In this case the cin recognizes 1 and 0 as integers and can convert an integer into a boolean. 0 is false, everything else is true. Another option is to let the library do it and use boolalpha. You can read about it here.

This shows a larger issue. What happens if I write "two point five" as the answer to height? In this case we can assume some intelligence on the part of the user, but thinking about things like this will help write more robust code in the future.

Matt
  • 2,554
  • 2
  • 24
  • 45