Me again, need help.

This is a discussion on Me again, need help. within the C++ Programming forums, part of the General Programming Boards category; Well I saw another post on this site of a guy making something that will tell him a list of ...

Me again, need help.

Well I saw another post on this site of a guy making something that will tell him a list of phone numbers when he put in the correct password. I'm trying to edit it so after putting in a correct password it asks you for a name, then you type the name and it lists the number. The password works fine, but when I type in a name it closes the program. This is my first time using Chars and I dunno what the heck I'm doing.

You are reading into a C-Style string incorrectly. You should use cin >> number; instead of cin >> number[10];. Of course, if you are trying to read in a name, why is the variable called number? In addition, if you want to read in a full name (with spaces) you cannot use >>, you must use getline.

You are also comparing strings incorrectly. Nameone and Nametwo are just single characters instead of a string of characters. You cannot compare a single character to a string of characters (even though technically your current code only compares single characters, I don't think that's what you meant).

My advice is to use the C++ string class in <string>. It makes reading and comparing strings easier.

#include <string> makes it so they can type names instead of just one letter or number, i think that is why your program was closing also set Nameone to = "steve" (i just made up a random name) since "steve" is a string the program will recognize it, i hope that isn't confusing i also added it so it would loop so the program wont close after finding only one number

whoohoo i finally helped someone and wasn't asking for help myself this is a big acheivement for me lol i hope this helped you

In the following, string is the name of an STL class. string objects, usually called strings, consist of a C style string and functions to manipulate the C style string. For example, the functions allow you to resize C style strings automatically. Most, if not all of the functions in cstring which can manipulate C style strings have equivalents in the string class, and there are other functions as well.

loop, as declared below is just the name of a string object.

string number,Nameone = "steve", Nametwo = "betty", loop;

>Also what exactly does the cin.ignore() command

ignore() is a member of the istream class. It has two parameters. The first indicates how many characters to ignore, and the second indicates a delimiting char. The first parameter defaults to to one and the second to newline char. ignore() looks at the input buffer and will remove and discard (ignore) as many char as indicated by first parameter unless a delimiting char has been encountered first, in which case removal from the input buffer stops after removal of the delimiting char. There may be more esoteric details beyond this, too.

Comparing floats/doubles for equality is generally a bad idea. The reason is, due to internal representation, you can get slight differences. Granted, in this case, "1337" should have the same bit pattern as "1337" (obviously), an int (or unsigned int) would be a better choice.
An example of where this is likely to go wrong:
The password is: 1.000045
The user types: 1.0000449
They show up identical...*
Here is some info on how floats are represented: http://www.math.grin.edu/~stone/cour...EEE-reals.html

Code:

cin>>number[10];

This reads in a character, but it has an off-by-one error. The available array addresses are &number[0] through &number[9].

That said, I would take everyone's advice about using std::string.

Cheers

*Example made up to demonstrate the type of error that can occur. It won't necessarily happen with these numbers.