Offistart - Virtual Offices, Office Space, Business Support Services
*Home>>>Telephone Answering

C++ Help?!?! I need help using the stringlength function in an if statement...?


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

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??

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.

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

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.

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

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

try strlen

Tags
  Business Services   Business Address   Call Forwarding   Call Handling   Answering Service   Telephone Answering   Mail Forwarding   Virtual Address   Virtual Assistant   Virtual Business   Virtual Offices
Related information
  • Write the spelling word that fits each clue.?

    1) biography 2) physician 3) politician 4) physical 5) biology 6) economy 7) democracy 8) astronomy 9) thermos 10) symphony 11) athlete 12) microscope 13) atmosphere 14) toxic 15) syn...

  • Dream Interpretation?

    daughter downstairs, washing dishes = female task son watching television = male task two men in black = stranger, insecure tied up = restricted. It may seem like an insecure dream. However,...

  • The g/f for 9 years has left me, should I ring or tex just to say that I miss her or wait until she calls?P2?

    I know that you love her and there is no one else for you right now but she doesn't want the relationship. How can you just hang around until she falls apart with someone else? Why would y...

  • How can I get out of high interest/car payment part 2?

    That's a very long story. Your an adult, you made a bad decision. I'm not sure why you think the loan company has screwed you over. You borrowed money, you knew what the payments would b...

  • HELP! how do i get a faster internet than dial up?

    Yes. I have a Linksys brand router. It's a great brand and router.One of the best in the market for it's price and speed. If you want more information of it: Price, information, and model...

  • Them damn bushs...the more you know the more they suck?

    Yawn that was long boring and by the way not a question.

    ...
  • Probability of event occuring? is there a formula?

    well yaybob's answer was pretty much all theres to it, but you said that each has a unique probability, so u cant really represent each person with a single pronumeral, since they have unique ...

  • DO anybody know these two jamicans songs, the lyrics are like this.?

    Try this website, ...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster