0

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?

#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.";
    }
}
6
  • 11
    Don't use character arrays. Use std::string. Everything will be easier. Commented May 3, 2012 at 22:24
  • 3
    Comparison of char*s for "string equality" is done with strcmp only. But as Rob says, std::string will be so much more convenient it's a shame not to use it. Commented May 3, 2012 at 22:27
  • where do i call std::string ? i get incompatible message in the if statement Commented May 3, 2012 at 22:31
  • "where do i call std::string?" You don't. Right now you find a good reference and read up on the topic. Surely you have a C++ book. Commented May 3, 2012 at 22:32
  • 2
    stackoverflow.com/questions/388242/… Commented May 3, 2012 at 22:33

6 Answers 6

3

If you really want to use char [] rather than std::string (which has an operator== and clearly acts more like what you expect) you'll need to edit your if statement to use the old C-way of doing string comparisons. Something like:

if ( strcmp(option, "N") == 0)
{
  // ...
}

Or since you are comparing only one character you could just do:

if ( *option == 'N' )
{
  // ...
}

This dereferences the pointer to the first character in that char [] which is a primitive type and so can be compared directly with ==.

Sign up to request clarification or add additional context in comments.

Comments

3

char option[1]; ... if (option=="N")

The 'if' statement compares the address of the option array with address of a constant string; they will never be equal.

Writing in C style, you could write something like if (strcmp(option, "N") == 0) or several other things. But it would be better to get used to using std::string in C++ code; and it's more direct. If option was a std::string, your 'if' statement would have been correct.

Comments

2

Take, for example

if (option=="N")

This compares option, decayed to a pointer, to the address of the literal "N". That will always return false. To compare the contents of C strings you need to use strcmp.

Do yourself an enormous favour and use std::string instead of C strings.

Comments

1

You are using 'C' strings - that end of the day are just pointers. So you need to use the function strcmp - but std::strings will save the hassle.

4 Comments

Arrays are not pointers. They decay into pointers. The distinction should be made clear.
@chris - I was trying to keep it simple.
I misread that as "that are just pointless." Which is nearly true. :)
I'm not sure I've ever ussed a c-string, but I'm glad to know at the end of the day they're just pointes ;-)
0

To understand why this doesn't work, you need to know a few things about how pointers work.

What is a pointer?
A pointer is a type that points to a value. So when you declare a variable with a type char:
char foo; char* bar = &foo;
You are reserving a chunk of memory to store a character. The variable bar points to the variable foo. You can get the value stored in foo by using it directly or dereferencing the variable bar (*bar).

When you declare a variable with a char pointer type:
char* foo
You are reserving a chunk of memory to store a chunk of memory to store a character.

But I never made pointer! In C/C++, an array can be implicitly converted into a pointer. This is not what actually happens with the code you wrote, but you can think of it like this:
char optionValue;
char* option = &optionValue;

Why does my if statement not work?
When you compare pointers, you compare the chunk of memory you are pointing to with the other pointer's chunk of memory. So == will only return true if the two pointers point to the same chunk of memory. In your code, this is impossible: the constant "N" will never be the same as the pointer that points to the chunk of memory the compiler created for you.

What you need to do is compare the content of the chunks of memory (by using strlen as suggested by other people) or changing the type of the variable (strings are very common, so there is a std::string type to deal with them).

6 Comments

"In C/C++, an array is a different syntax for a pointer." -- No, it's not. Stop telling people that.
While I agree, the point is an array is a memory address. The fact that there is protection against serious goofups built into the language is beside the point.
No, an array is not a memory address. An array is a contiguous block of elements of a particular type.
Fine then, can be treated as a memory address.
It would be much better if you just stated the actual relationship between arrays and pointers, which is this. "An array can be implicitly converted to a pointer to its first element." -- Otherwise people end up writing code like this and wonder why it doesn't work.
|
0

How do i get rid of this problem?

Stop using character arrays.

where do i call std::string ?

Like this:

#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

class start
{
public :
    void getusertype ();
    void registeruser ();
    start(); 
protected :
    std::string consumer_name;
    std::string consumer_address;
    std::string consumer_email;
    std::string user;
};

start::start()
{
    std::cout <<"Are you the venue manager, consumer or ticket agent?";
    std::getline(std::cin, user);

}
void start::getusertype ()
{
    if(user == "consumer")
    {
        std::cout <<"You have now entered the system.\n\n";
    }
    else
    {
        std::cout <<"You can only enter this part of the system if you are a consumer.\n";
    }

}
void start::registeruser()
{
    std::string option;
    std::cout <<"Are you registered? (Enter Y/N)";
    std::cin >> option;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    if (option=="N")
    { 
        std::cout <<"Please enter your registration details\n\n";
        std::cout << " Enter your full name: \n";
        std::getline(std::cin, consumer_name);
        std::cout <<"Enter your address: \n";
        std::getline(std::cin, consumer_address);
        std::cout <<"Enter your email address: \n";
        std::getline(std::cin, consumer_email);
    }
    else
    {
        std::cout <<"You will now choose an event.\n";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.