validation

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Re: validation

Postby omnius » Thu Dec 25, 2003 5:51 pm

Midnightuk wrote:I have a defined a char of size 6

How can i validate the input so if a user enters 7 or more characters then an invalid size is shown and it will not override os memory.

Code: Select all
char product[6];

cout<<"enter product: ";
cin>>prod;


One way would be to use the std::string class, which overcomes many of the limitations of using character arrays for strings.

You can limit the input to your char array with:

cin >> setw(6) >> prod;
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm

or u can use this

Postby rockmanian » Fri Dec 26, 2003 2:52 am

Try this.

cin.get(6, product);

My advice is that after you get this input to write the following:

cin.get(); //this u use to clear the input stream.
++ me!
rockmanian
 
Posts: 50
Joined: Fri Dec 26, 2003 2:16 am
Location: Earth

Re: or u can use this

Postby omnius » Fri Dec 26, 2003 3:38 am

rockmanian wrote:My advice is that after you get this input to write the following:

cin.get(); //this u use to clear the input stream.

That will block waiting for input with an Enter key press rather than clear the input stream directly. You can use cin.ignore() to clear the input stream - a quick search through the forums should find examples of how to do so.
omnius
 
Posts: 496
Joined: Wed Sep 24, 2003 12:03 pm


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 1 guest