i am working on just a side project. nothing to do with class so you arent doing my homework for me. i just need help. i am using getline(cin, phone) to get an account/phone number. i would like to make it to where if the string length is not 10 characters, then it prompts the user to re-enter or press q to quit. i know how to do the else part with:
else (phone=="q")
{cout%26lt;%26lt;" blah blah blah";}
i just am not sure how i should go about doing this part:
cout%26lt;%26lt;"Enter 10-digit telephone/rental account # with no hyphens, or scan member rental card: ";
cin%26gt;%26gt;phone;
cout%26lt;%26lt;endl;
if (phone!= ..................this is where i dont know what direction to go....
(stringlength(phone)!=10)) ????
just not too sure as u can prpbably tell... i am only in my 3rd/4th week of c++ so i only know very basic crap. i am just doing this on the side to try and stay one of the more proficient students.
anyone who comes to C++ in yahoo answers is probably familiar with me by now. hahahahaha
C++ Help?!?! I need help using the stringlength function in an if statement...?
just use strlen() function to get the length.
char phone[20];
phone[0] = 0;//null it out
//your code
if (strlen(phone) != 10)
//your code
yes, you could use int, but i think a string would be much better.
Reply:yeah, use an int for telephone numbers. I'm new to C too, but I was able to understand this. It's easy to get a length for ints
do
int num, times;
while(num%26gt;0)
{
num=num%10;
times++;
}
if(time==10)...
Reply:try strlen
Reply:Put all that code in a little while loop like this:
bool Done = false;
while ( ! Done ) {
/* your code goes here */
/* When the length test fails, put out an error message and do NOT set Done = true */
/* Set Done = true only when all tests succeed */
}
Because you'll often want to handle input errors more gracefully than C++ input does, it's not generally a good idea to take user input in any form other than strings.
Reply:If you have a C++ string, you can get its length with s.length(). See http://cppreference.com/cppstring/length...
%26gt; "should the account number be an INT instead of STRING?"
You can do that if you want.
Just note that:
For making a robust program, it's probably better to take the input into a string and then attempt to convert it to a number. Or if you use an int, have code that deals with crap data like random character symbols and what not.
edible flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment