Reputation: 33
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.";
}
}
Upvotes: 0
Views: 12928
Reputation: 168886
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";
}
}
Upvotes: 0
Reputation: 1519
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).
Upvotes: 0
Reputation: 60047
You are using 'C' strings - that end of the day are just pointers. So you need to use the function strcmp
- but std::string
s will save the hassle.
Upvotes: 1
Reputation: 32428
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 ==
.
Upvotes: 3
Reputation: 613612
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.
Upvotes: 2
Reputation: 131
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.
Upvotes: 3