473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Using strings and if statements in C++

5
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?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include<stdlib.h>
  4. class start
  5. {
  6. public :
  7.     void getusertype ();
  8.     void registeruser ();
  9.     start();    
  10. protected :
  11.     char consumer_name[20],
  12.          consumer_address[30],
  13.          consumer_email[30];
  14.         char user[8];
  15. };
  16. start::start()
  17. {
  18.      char terminator;
  19.      cout <<"Are you the venue manager, consumer or ticket agent?";
  20.      cin.get(user,8);
  21.      cin.get(terminator);
  22.  
  23. }
  24. void start::getusertype ()
  25. {
  26.     char terminator;
  27.     if(user[8])
  28.     {
  29.         cout <<"You have now entered the system." <<endl <<endl;
  30.         cin.get(terminator);
  31.  
  32.     }
  33.     else
  34.     {
  35.         cout <<"You can only enter this part of the system if you are a consumer.";
  36.     }
  37.  
  38. }
  39. void start::registeruser()
  40. {
  41.     char option[1];
  42.     cout <<"Are you registered? (Enter Y/N)";
  43.     cin.get(option,1);
  44.     if (option=="N")
  45.     {    char terminator;
  46.         cout <<"Please enter your registration details" <<endl <<endl;
  47.         cout << " Enter your full name: " <<endl;
  48.         cin.get (consumer_name,20);
  49.         cin.get(terminator);
  50.         cout <<"Enter your address: " <<endl;
  51.         cin.get(consumer_address,30);
  52.         cin.get(terminator);
  53.         cout <<"Enter your email address: " <<endl;
  54.         cin.get(consumer_email,30);
  55.         cin.get(terminator);
  56.     }
  57.     else
  58.     {
  59.         cout <<"You will now choose an event.";
  60.     }
  61. }
  62.  
May 3 '12 #1

✓ answered by weaknessforcats

This code:

Expand|Select|Wrap|Line Numbers
  1.   if(user[8])
  2. 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:

Expand|Select|Wrap|Line Numbers
  1. char option[1];
  2.      cout <<"Are you registered? (Enter Y/N)";
  3.       cin.get(option,1);
  4.       if (option=="N")
  5.       {  
  6. 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.

1 2735
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1.   if(user[8])
  2. 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:

Expand|Select|Wrap|Line Numbers
  1. char option[1];
  2.      cout <<"Are you registered? (Enter Y/N)";
  3.       cin.get(option,1);
  4.       if (option=="N")
  5.       {  
  6. 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.
May 4 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: jstreet10 | last post by:
How would i use function statements instead of loops(for) in this code? i have tried some, but not working. # this program is suppose to read fm a file, computes the average and reads fm a...
4
by: PM | last post by:
Hello, I am a beginner to VB.NET. Here is my question: I currently have my SQL INSERT and UPDATE functions in my form. I'm trying to clean up the code on my form, and use modules for database...
0
by: Daniel | last post by:
Does system.xml have any way to transofrm data with an xswl style sheet using strings like MSXML2 does? how to convert this to use System.XML so i do not depend on MSXML2 interop? static...
0
by: mmiller | last post by:
Does anyone know of a Add-In for VS.NET 2005 that optimizes the using statements in a code file like Resharper does (basically order them, remove un-need ones, etc.). Resharper for VS.NET 2005 is...
3
by: Wolfgang Meister | last post by:
Is there a way to let VisualStudio insert automatically (after pressing e.g. F7) missing "Using" statements at the top ? Or is there a menu which suggests inserts for using directives ? How...
2
by: Pugi! | last post by:
It is by accident that I noticed that I forgot to use mysql_real_escape_string in part of my webapp. I tested input with following text : Hélène 51°56'12'' http://www.mysite.org/folder 3 functions...
2
by: captainmorgan | last post by:
I have a simple query, which consists of a few project dates, which in return need to be charged based on completion. That said, I wanted to create a CHARGE field which would calculate the total...
1
by: bimeldip | last post by:
Hi, I have managed to create codes to display data from the database in a html page.I have gone on to create a page to allow users to manipulate the table via a html page. For instance users will be...
0
by: bimeldip | last post by:
Hi, I have managed to create codes to display data from the database in a html page.I have gone on to create a page to allow users to manipulate the table via a html page. For instance users will be...
11
by: Botang | last post by:
Create a chessboard program by using while statements and you can only use TWO cout statments.. (using C++) One cout is cout<< whiteRow << endl; THe other cout is cout <<blackRow << endl; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.