d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Question
Add Reply New Topic New Poll
Member
Posts: 976
Joined: Apr 5 2010
Gold: 1,080.50
May 19 2012 08:02am
Code


#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

void genderTrue()
{
   cout << "You selected male!";

}

int main()
{
   char FirstName[30], LastName[30], FileName[20], checkGender[7];

   bool Gender;

/*
   cout << "Hello there!\n";
   cout << "This is the start of your journey.\n";
   system("PAUSE");

   system("CLS");

   cout << "Please enter your characters first name: ";
   cin >> FirstName;

   cout << "Now his or her second name: ";
   cin >> LastName;

   system("CLS");
*/

   cout << "Please enter your character's gender with male or female: ";
   cin >> checkGender;

       if (checkGender == "male") {
          Gender = true;
       }
           else if (checkGender == "female") {
               Gender = false;
           }

   if (Gender == true) {
       cout << "You selected male!\n";
   }
       else if (Gender == false) {
           cout << "You selected female!\n";
       }

   system("PAUSE");
}


Preface: First hour of programming. First error and I know there is an easier way to do this gender check type of thing and it is probably horrid and you also probably hate me for using system library.
Purpose: At the moment, I just want it to check gender, then it spits out whether you confirmed it or not, but no matter if you type male or female, it will also present with "You selected male!"

I'm looking for the correction to my current code and a way to do this better.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
May 19 2012 12:03pm
what i would do instead is either store m or f in the character array and then check
Code
if checkgender == m
printf(youre male)
else if (checkgender == f)
printf(youre female)


and then put it in a while loop so that you are garateed the correct answer.

Code
while checkgender != m || checkgender != f
printf(please enter your gender)
if checkgender == m
printf(youre male)
else if (checkgender == f)
printf(youre female)
end while


that would eliminate the function you have and 2 if statements.

if you insist that you want the full word in there you will have to use a function that converts the inputed text into FULL CAPS or FULL LOWERCASE to compare them incase they input maLe or FEMale

here is a quick function i wrote that works to change a character array into full uppercase (just pass its pointer to the function).

Code
//usage
//
//char array[7];
//cin >> array >>
//alpganum2upper(array)

void alphanum2upper(char *str)
{
   int i = strlen(str);                            //length of the string
   while (i-->0)                                   //while the length is greater then zero deincrement
   {
       if (*str >= (int)'a' && *str <= (int)'z')  //if the pointer to the current character of the array is greater then (int)a
       {                                          //(:ps all that does is turn 'a' into a decimal number look it up on a ascii chart:) and str is less then (int)z
           *str-=32;                              //if current char is between those lowercase values subtract 32 from it putting the character in the
       }                                          //ascii range for the equivalent upper case letter
   str++;                                         //increament the string pointer to point to the next letter in the string array
   }
}


edit:: you could edit the function to accept a string length as an variable so you wouldnt have to include strings.h if you wanted.

This post was edited by AbDuCt on May 19 2012 12:11pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll