Using strings and if statements in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • W051
    New Member
    • Mar 2012
    • 5

    Using strings and if statements in C++

    The problem with this code is that there are no errors showing, but when i compile the program, the if else statements do not carry out as they should do. Both if statements show the same answer, for example the second if statement. If I type Y or N then I get the same thing, 'You will now choose an event'. How do i get rid of this problem? Is char supposed to be used or string?
    Code:
    #include <iostream>
    #include <iomanip>
    #include<stdlib.h>
    class start
    {
    public :
    	void getusertype ();
    	void registeruser ();
    	start();	
    protected :
    	char consumer_name[20],
    		 consumer_address[30],
    		 consumer_email[30];
    		char user[8];
    };
    start::start()
    {
    	 char terminator;
    	 cout <<"Are you the venue manager, consumer or ticket agent?";
    	 cin.get(user,8);
    	 cin.get(terminator);
    	
    }
    void start::getusertype ()
    {
    	char terminator;
    	if(user[8])
    	{
    		cout <<"You have now entered the system." <<endl <<endl;
    		cin.get(terminator);
    		
    	}
    	else
    	{
    		cout <<"You can only enter this part of the system if you are a consumer.";
    	}
    	
    }
    void start::registeruser()
    {
    	char option[1];
    	cout <<"Are you registered? (Enter Y/N)";
    	cin.get(option,1);
    	if (option=="N")
    	{	char terminator;
    		cout <<"Please enter your registration details" <<endl <<endl;
    		cout << " Enter your full name: " <<endl;
    		cin.get (consumer_name,20);
    		cin.get(terminator);
    		cout <<"Enter your address: " <<endl;
    		cin.get(consumer_address,30);
    		cin.get(terminator);
    		cout <<"Enter your email address: " <<endl;
    		cin.get(consumer_email,30);
    		cin.get(terminator);
    	}
    	else
    	{
    		cout <<"You will now choose an event.";
    	}
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
      if(user[8])
    etc...
    There is no user[8]. user is an 8 element array so the last element is user[7]. You are looking at the byte after the end of your array.

    This code:

    Code:
    char option[1];
         cout <<"Are you registered? (Enter Y/N)";
          cin.get(option,1);
          if (option=="N")
          {  
    etc...
    In C/C++ the name of an array s the address of element 0 of the array. So here you are comparing the address of option with the address of the literal. Since they are two things they are in different memory locations so the addresses will always be unequal.

    Note that "N" is a 2 element array - you need a \0 to make it a string. I expect here you meant 'N', which is a single char. If you actually meant "N", then read up on strcmp and then most likely option will need to be a bigger array.

    Since you are using C++, I would not use any of the C-string stuff but would use C++ string objects instead.

    Comment

    Working...