English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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<<" blah blah blah";}

i just am not sure how i should go about doing this part:

cout<<"Enter 10-digit telephone/rental account # with no hyphens, or scan member rental card: ";
cin>>phone;
cout< 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

2007-03-24 16:48:38 · 5 answers · asked by eyefixguitars 1 in Computers & Internet Programming & Design

when i was rereading my post i thought of an idea that helped earlier when i was working on the part where i enter the SKU for each movie to be rented.... should the account number be an INT instead of STRING? i think it would make life a little easier and i could get rid of a few lines of code and replace with a short if statement. am i thinking correctly on that part??

2007-03-24 16:50:37 · update #1

i appreciate the help, but you all are confusing me. this is all i need to do.......

cout<<"Enter 5-digit movie sku to be rented or type 0 and press enter to finish:\n";
cin>>sku;
cout<

cin.slength(sku) <<<<<<<<<<<<< how do i type this part?


thats all i want to know. i cannot get the syntax right, and my book from class is over 1300 pages. i just want to find the stringlegth of sku

2007-03-25 10:22:54 · update #2

5 answers

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.

2007-03-25 02:24:25 · answer #1 · answered by justme 7 · 0 0

If you have a C++ string, you can get its length with s.length(). See http://cppreference.com/cppstring/length.html

> "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.

2007-03-25 00:41:18 · answer #2 · answered by csanon 6 · 0 0

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.

2007-03-25 00:10:22 · answer #3 · answered by The Phlebob 7 · 0 0

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>0)
{
num=num%10;
times++;
}
if(time==10)...

2007-03-24 23:54:24 · answer #4 · answered by Anonymous · 0 1

try strlen

2007-03-25 02:16:38 · answer #5 · answered by dezrtluver 2 · 0 0

fedest.com, questions and answers