-2

I came into this problem where gets does not work inside condition statement. Can we not use gets() function in this way? Is it even taking input or not as it is not displaying output. Help me how to fix this issue.

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

class Hospital
{
    char bloodGroup[100];
    char gender;
    public:
        void setData();//1  Use this to set data on the record 
        void showData();//3 This is to show the whole data which is recorded within the file given from setData() 
};
//----------------------------------------
void Hospital::setData()//1
{    
    
    cout<<"\n\n\t\t\xB3\xB2=\xB2=\xB2-\xB3 HOSPITAL MANAGEMENT SYSTEM  \xB3\xB2=\xB2=\xB2-\xB3\n\n"<<endl;
    cout<<"\n\t\t\t*****************************************\n";
    cout<<"\t\t\t\t ENTER THE DETAILS ";
    cout<<"\n\t\t\t*****************************************\n\n";
    fflush(stdin);
        cout<<"\n\t\t Enter your gender (M|F) :-  ";
        cin>>gender;
        again:
        int flag=0;
        cout<<"\n\t\t Enter patient Blood group :-"; 
        cin>>bloodGroup;
        if((strcmp(bloodGroup,"A+")==0)||(strcmp(bloodGroup,"a+")==0)||(strcmp(bloodGroup,"A-")==0)||(strcmp(bloodGroup,"a-")==0)||
        (strcmp(bloodGroup,"B+")==0)||(strcmp(bloodGroup,"b+")==0)||(strcmp(bloodGroup,"B-")==0)||(strcmp(bloodGroup,"b-")==0)||
        (strcmp(bloodGroup,"O+")==0)||(strcmp(bloodGroup,"o+")==0)||(strcmp(bloodGroup,"O-")==0)||(strcmp(bloodGroup,"o-")==0)||
        (strcmp(bloodGroup,"AB+")==0)||(strcmp(bloodGroup,"ab+")==0)||(strcmp(bloodGroup,"AB-")==0)||(strcmp(bloodGroup,"ab-")==0))
        {
            flag=1;
            gets(bloodGroup);
        }
        if(flag==0)
        {
            cout<<"\n\t\t Wrong Entry...Enter a valid Blood Group..Try Again..\n";
            goto again;
        }
        system("cls");
        Hospital::showData();
        
}

void Hospital ::showData()//3
{
    cout<<" Gender  : "<<gender<<endl;
    cout<<" Blood group : "<<bloodGroup<<endl; 
}

int main(){
    Hospital a1;
    a1.setData();
    return 0;
}

The output is like this i am able to give input but showData() function is not displaying output so i wonder if it is even taking my inputs:

  Gender  : F
  Blood group :


                                         
  • from https://en.cppreference.com/w/cpp/io/c/gets: Reads stdin into given character string until a newline character is found or end-of-file occurs. Is that what you're trying to do? You already read in bloodgroup. – stefaanv Aug 04 '22 at 16:04
  • 4
    `gets` is old and has a really poor design and a ton of security issues. Please don't ever use it in new code (and try to remove it from old code). – Jesper Juhl Aug 04 '22 at 16:04
  • 1
    Why are you using `strcmp` in *C++* code? That's a C API. – Jesper Juhl Aug 04 '22 at 16:12
  • 1
    [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714). `gets` has been remove from C and C++ standards for more than a decade. Upgrade yourself – phuclv Aug 04 '22 at 16:14
  • 2
    `goto again;` -- Please use structured looping constructs, such as `while` and `do-while`. Putting `goto` in code will guarantee less people will help you, even if the `goto` looks "innocent enough". – PaulMcKenzie Aug 04 '22 at 16:16
  • This doesn't address the question, but `flag` is not a meaningful name. Change it to something that describes what it's used for. `bool valid_group = false;`, for example. But in the code in the question it's not needed at all. Change `if(flag == 0)` to `else` and remove every mention of `flag`. – Pete Becker Aug 04 '22 at 16:25
  • So, I spent roughly 30 minutes to formulate an answer only to get this question locked before I can submit. You can ready my answer [here](https://gist.github.com/HiImJulien/bb5b133e4a72ea3cd44405e41d036d86). As to why it doesn't work as well as some pointers on how you can improve your code. – Julian Kirsch Aug 04 '22 at 16:45

1 Answers1

0

You really should be using get with a string, and also in this scenario I don't see why its even necessary. If you delete the line with get(), your code works as intended.

absolutezero
  • 134
  • 3