if statement problem?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pdring
    New Member
    • Oct 2007
    • 15

    if statement problem?

    Hi,
    I wonder if somebody can see my error?
    I would assume that these if statements would be valid only if 'y' or 'Y' is entered, and if any other character is entered the default string declared for the variables would be displayed, however this is not the case! I have looked over this section of code so many times and can not see my error.
    any input would be greatly appreciated.

    Code:
    void main(void)
    {
       char alarmTime [30]= "no alarm call required";
       char answer, answer2;
       char thisFirstName[30];
       char thisSecondName[30];
       char thisCarReg [30] = "no vehicle registered";
       guest newguest;
       clrscr();
    
       cout << "Enter new guests first name: ";
       gets(thisFirstName);
       cout << "Enter new guests sir name: ";
       gets(thisSecondName);
    
       cout << "does the guest have a car? y or n: ";
       cin >> answer;
    
       if   (answer == 'y'|| 'Y')
            {
            cout << "Enter new guests car registration number ";
            gets(thisCarReg);
            }
    
       cout << "would the guest like an alarm call?  y or n: ";
       cin >> answer2;
    
    
       if   (answer2 == 'y'|| 'Y')
            {
            cout << "Please enter the time of the alarm call: ";
            cin >> alarmTime;
            }
       
    
       newguest.setGuestDetails(thisFirstName,thisSecondName, thisCarReg);
       newguest.setAlarm(alarmTime);
    
       cout << endl <<"Guest Name: "<< newguest.getFirstName()<< " " << newguest.getSecondName() << endl;
       cout <<"Vehicle Registration: "<<newguest.getCarReg()<< endl;
       cout <<"Alarm Call: " << newguest.getAlarm();
    
       
    
       getchar();
    
    }
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    you have to restate the answer == part of the if, so instead of this

    if (answer == 'y'|| 'Y')

    use this

    if (answer == 'y'|| answer == 'Y')

    Reason being, that the || doesn't work the way you think it does. When you do it the way you currently have it your saying "if answer is 'y' or if the value of 'Y' is non zero" - the compiler needs you to be more explicit, its just one of those things you have to get used to.

    Comment

    • pdring
      New Member
      • Oct 2007
      • 15

      #3
      THANKS !!
      now you have explaned it it does seem obvious, such is the way with these things, they are easy only when you know the answer.

      Comment

      • compman9902
        New Member
        • Mar 2007
        • 105

        #4
        Originally posted by pdring
        THANKS !!
        now you have explaned it it does seem obvious, such is the way with these things, they are easy only when you know the answer.
        In the future, you shouldn't try to cut corners with your code in C++. I think that only works in Visual Basic. <--Joke
        Have fun with your programming though.

        Comment

        Working...