Click here to Skip to main content
15,888,802 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: What`s a palette in the parsing a bitmap context Pin
phil.o10-Mar-20 19:27
professionalphil.o10-Mar-20 19:27 
GeneralRe: What`s a palette in the parsing a bitmap context Pin
Calin Negru10-Mar-20 21:06
Calin Negru10-Mar-20 21:06 
GeneralRe: What`s a palette in the parsing a bitmap context Pin
phil.o10-Mar-20 21:11
professionalphil.o10-Mar-20 21:11 
GeneralRe: What`s a palette in the parsing a bitmap context Pin
Calin Negru10-Mar-20 21:30
Calin Negru10-Mar-20 21:30 
GeneralRe: What`s a palette in the parsing a bitmap context Pin
phil.o10-Mar-20 21:46
professionalphil.o10-Mar-20 21:46 
GeneralRe: What`s a palette in the parsing a bitmap context Pin
Calin Negru11-Mar-20 7:32
Calin Negru11-Mar-20 7:32 
GeneralRe: What`s a palette in the parsing a bitmap context Pin
leon de boer12-Mar-20 5:15
leon de boer12-Mar-20 5:15 
QuestionHaving issues with the cin.getline() function in C++ Pin
Member 147670008-Mar-20 14:29
Member 147670008-Mar-20 14:29 
for a short test in my class, we had to write code about pigs that allowed the user to enter information about a set of pigs (e.g. the pigs' names, breed, weight, gender, etc.). We were to create a class called pigs that would be able to hold the pigs' information. There was nothing wrong with the class itself, the problem I'm having is actually in the main function.
cout << "Enter the pig gender (\'m\'/\'f\')(if not press \'enter\' for none): ";
cin.getline(pigGender, 10);
if(pigGender != "\n")
{
    while(pigGender != "m" && pigGender != "f")
    {
        cout << "Invalid gender, please enter a valid gender m or f(if not press \'Enter\'): ";
        cin.getline(pigGender, 10);
        if(pigGender != "\n")
        {
            //cout << "if branch, pigGender:" << pigGender;
            gender = pigGender[0];
        }
        else
        {
            //cout << "else branch, pigGender:" << pigGender;
            gender = ' ';
            break;
        }
    }
    pigs[i].SetPigGender(gender);
}

The piece of code above, shows how I'm trying to obtain the pig's gender from the user. I made it so that if the user doesn't know the gender, they can just hit enter to move on (as I did with other features that worked fine). The problem is that when you hit enter, the pigGender string doesn't seem to take the newline character. Also, I understand how the getline function works (takes in a newline and stops), hence I believe when you hit enter, the getline will take in the newline and stop, but here it does not seem to take it in at all. I also made it so that if the user enters a wrong letter (not m or f), then they can still hit enter if they do not know. The commented out codes are checks I implemented to see exactly what was in pigGender. When I enter a charcter that is not a newline it will go to the if branch and dislpay my error message, however, when I hit only enter, it still goes through that if branch instead of the else branch. the checks i commented out show me which branch I am in and the charcter in the pigGender string, and when I hit enter for the newline, there seems to be nothing in the pigGender string. What seems even more bizarre, is that the following code is very similar to what I did above (I actually copied and pasted it and made some changes to make the one above) and it works perfectly.
cout << "Enter the pig weight(if not press \'Enter\'): ";
cin.getline(weight, 10);
if(weight != "\n")
{
    float pigWeight = atof(weight);
    while(pigWeight < 0)
    {
        cout << endl;
        cout << "Invalid weight, please enter a valid weight(if not press \'Enter\'): ";
        cin.getline(weight, 10);
        if(weight != "\n")
        {
            pigWeight = atof(weight);
        }
        else
        {
            pigWeight = 0;
        }
    }
    pigs[i].SetPigWeight(pigWeight);
}

Is there something I'm just not seeing here, because I have been looking at this code since yesterday and have tried a bunch of stuff to fix it but can't. Of course, I do know of many other ways to tackle this problem (like comparing string size instead), but I started with this and its really bugging me that it does not work, so I just want to figure it out just so I can know. If you can help me understand, please respond, I really want to figure this thing out, thank you!
AnswerRe: Having issues with the cin.getline() function in C++ Pin
k50548-Mar-20 15:37
mvek50548-Mar-20 15:37 
GeneralRe: Having issues with the cin.getline() function in C++ Pin
Member 147670009-Mar-20 10:55
Member 147670009-Mar-20 10:55 
GeneralRe: Having issues with the cin.getline() function in C++ Pin
Member 147670009-Mar-20 11:01
Member 147670009-Mar-20 11:01 
GeneralRe: Having issues with the cin.getline() function in C++ Pin
k50549-Mar-20 12:12
mvek50549-Mar-20 12:12 
GeneralRe: Having issues with the cin.getline() function in C++ Pin
Member 1476700010-Mar-20 8:49
Member 1476700010-Mar-20 8:49 
GeneralRe: Having issues with the cin.getline() function in C++ Pin
k505410-Mar-20 9:04
mvek505410-Mar-20 9:04 
AnswerRe: Having issues with the cin.getline() function in C++ Pin
Stefan_Lang8-Mar-20 22:26
Stefan_Lang8-Mar-20 22:26 
GeneralRe: Having issues with the cin.getline() function in C++ Pin
k50549-Mar-20 12:29
mvek50549-Mar-20 12:29 
GeneralRe: Having issues with the cin.getline() function in C++ Pin
Stefan_Lang9-Mar-20 21:41
Stefan_Lang9-Mar-20 21:41 
Generala not fun question: how to deploy a utility program written in MFC and C++ Pin
Southmountain8-Mar-20 12:56
Southmountain8-Mar-20 12:56 
GeneralRe: a not fun question: how to deploy a utility program written in MFC and C++ Pin
Jon McKee8-Mar-20 13:01
professionalJon McKee8-Mar-20 13:01 
GeneralRe: a not fun question: how to deploy a utility program written in MFC and C++ Pin
Southmountain8-Mar-20 16:06
Southmountain8-Mar-20 16:06 
GeneralRe: a not fun question: how to deploy a utility program written in MFC and C++ Pin
Maximilien8-Mar-20 15:38
Maximilien8-Mar-20 15:38 
GeneralRe: a not fun question: how to deploy a utility program written in MFC and C++ Pin
Southmountain8-Mar-20 16:03
Southmountain8-Mar-20 16:03 
GeneralRe: a not fun question: how to deploy a utility program written in MFC and C++ Pin
Shao Voon Wong8-Mar-20 15:51
mvaShao Voon Wong8-Mar-20 15:51 
GeneralRe: a not fun question: how to deploy a utility program written in MFC and C++ Pin
Southmountain8-Mar-20 16:04
Southmountain8-Mar-20 16:04 
GeneralRe: a not fun question: how to deploy a utility program written in MFC and C++ Pin
Shao Voon Wong8-Mar-20 16:09
mvaShao Voon Wong8-Mar-20 16:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.