-
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.
Code:
#include <iostream>
using namespace std;
int main()
{
float password;
char number[10];
char Nameone;
char Nametwo;
cout<<"Enter the password: \n";
cin>>password;
if (password == 1337) {
cout<<"What number are you requesting?\n";
cin>>number[10];
}
else if (number[10] == Nameone)
cout<<"The number is ***-****\n";
else if (number[10] == Nametwo)
cout<<"The number is ***-****\n";
cin.ignore();
cin.get();
return 0;
}
Help would be appreciated. :)
-
you need the
cin.ignore();
right after each cin >>
-
Adding cin.ignore() will not help here.
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.
-
oh oops my bad i read like the first line made my post was kinda
busy at the time. wont let that happen again :(
-
the way i would make this program is like this
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
float password;
string number,Nameone = "steve", Nametwo = "betty", loop;
cout<<"This is my phone book continue looking up numbers?(yes or no): ";
cin>>loop;
cin.ignore();
cout<<"\nEnter the password: ";
cin>>password;
cin.ignore();
while (loop == "yes")
{
if (password == 1337) {
cout<<"\nWhat number are you requesting?\n";
cin>>number;
cin.ignore();
}
if (number == Nameone)
cout<<"\nThe number is 544-6789";
else
if (number == Nametwo)
cout<<"\nThe number is 456-4566";
cout<<"\nwould you like to find another number?(yes or no):";
cin>>loop;
cin.ignore();
}
return 0;
}
any questions?
#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
-
Ah, thank you. I need to study a bit more, I've only been at it a few days. I didn't understand half the stuff in vert's code.
-
well tell me what code you dont understand and ill explain it
-
String and loop, I don't know what those two do or how to use them. Also what exactly does the cin.ignore() command do.
-
>String and loop, I don't know what those two do or how to use them.
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.
-
Some things to note:
Code:
if (password == 1337)
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
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.
-
thanks for answering for me i was away at my girlfiends house so i didn't get a chance to post everything you said answers everything i think....