Tuesday, July 28, 2009

C++ : I need help creating if statements...i think?

i need help with this unfinished program . i am in the third week of C++ so its really basic. at this point, i dont even want to worry about storing the infor in a database, i just want to make the loops or whatever. can u help???? i left the headers out


int main()


{


string lname, fname,address,streetname,phone,moviesout...


moviesout="No movies currently rented.";


cout%26lt;%26lt;"Enter 10-digit telephone number of account holder with no hyphens: ";


cin%26gt;%26gt;phone;


cout%26lt;%26lt;endl;


//Add if loop. If number is not = 10 digits, re-enter new #.


//Need to add if loop here. If phone number is already stored, bring up their information.


cout%26lt;%26lt;"Enter customer's last name: ";


cin%26gt;%26gt;lname;


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter customer's first name: ";


cin%26gt;%26gt;fname;


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter customer's address then street name: ";


cin%26gt;%26gt;address%26gt;%26gt;streetname;


cout%26lt;%26lt;endl;


cout%26lt;%26lt;fname%26lt;%26lt;" "%26lt;%26lt;lname%26lt;%26lt;"\n"%26lt;%26lt;address%26lt;%26lt;" "%26lt;%26lt;streetname%26lt;%26lt;"\n"%26lt;%26lt;phone%26lt;%26lt;"\n"%26lt;%26lt;movies...


//store to database?sorry its not spaced out-not enough room

C++ : I need help creating if statements...i think?
while(1)


{


cout%26lt;%26lt;"Enter 10-digit telephone number of account holder with no hyphens: ";


cin%26gt;%26gt;phone;


cout%26lt;%26lt;endl;


if(number == 0)


break; // I assume you also need to provide option to user to exit the program;


if(number is not 10 digit)


continue;


if(number is already in databse)


print the information ;


continue;


cout%26lt;%26lt;"Enter customer's last name: ";


cin%26gt;%26gt;lname;


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter customer's first name: ";


cin%26gt;%26gt;fname;


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter customer's address then street name: ";


cin%26gt;%26gt;address%26gt;%26gt;streetname;


cout%26lt;%26lt;endl;


cout%26lt;%26lt;fname%26lt;%26lt;" "%26lt;%26lt;lname%26lt;%26lt;"\n"%26lt;%26lt;address%26lt;%26lt;" "%26lt;%26lt;streetname%26lt;%26lt;"\n"%26lt;%26lt;phone%26lt;%26lt;"\...


//store to database?sorry its not spaced out-not enough room


}
Reply:You want something like a do..while


do


{


cout %26lt;%26lt; "enter number....";


..


..


..





} while (strlen(phone) != 10);





don't forget to include the string lib at the top
Reply:John Q has a slight problem with his suggestion. He uses strlen on a C++ string. The length of a C++ string s is s.length() or s.size().





Eyefixguitar: We won't do your homework for you, just point you in the right direction.





As John Q suggested, use a do while loop. In case you are missing reading material: http://www.cprogramming.com/tutorial/les...





A do while loop will do the trick. You want the loop to repeat only if the number is not 10 digits. Hence, your condition for the loop must be length of the string is not 10 digits. I.e. phone.length() != 10.


No comments:

Post a Comment