string and integer validating

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 6
Reputation: willywonka is an unknown quantity at this point 
Solved Threads: 1
willywonka's Avatar
willywonka willywonka is offline Offline
Newbie Poster

string and integer validating

 
0
  #1
Sep 19th, 2009
I have to write a program for my assignment that does a quote.
It has to have a void function that validates a string and and integer entered. Other wise I would have solved the problem with a simple menu and integers.

The entries has to be one of five cake choices and the amount needed.
In this case, chocolate, carrot, coffee, fruit, custard.
It has to keep prompting the user until one of these options are entered.

It has to have a do{....}while loop.

I tried a switch statement as an option in the loop but it tells me the about parameters being to long.
Was I on the right track?

Can you accumulate information in a void function to be used in the main function without using global variables???

Please just help me with an idea of how to compound all this string options and to use them as a condition in the do...while loop. Even a totally unrelated example will be appreciated. I will appreciate ANY pointer in the right direction.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,970
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: string and integer validating

 
1
  #2
Sep 19th, 2009
Post down your code please.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: string and integer validating

 
0
  #3
Sep 19th, 2009
the cctype header has a function called isdigits(...). You can use that
to validate whether a string contains integer. There are also isspace(..)
among other things in that library. Check out the library , here
Here is an example :
  1.  
  2. #include<iostream>
  3. #include<cctype>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string phoneNumber = "777777777";
  10.  
  11. bool pass = true;
  12.  
  13. for(int i = 0; i < phoneNumber.size(); i++)
  14. {
  15. if( isdigit(phoneNumber[i]) )
  16. pass = true;
  17. else
  18. {
  19. pass = false;
  20. break;
  21. }
  22. }
  23.  
  24. }
Last edited by firstPerson; Sep 19th, 2009 at 1:45 pm.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: string and integer validating

 
0
  #4
Sep 19th, 2009
a way you can do this would be to have a void check function inside a while loop in your main function.
  1. void CheckInput(char[80], bool*);
  2.  
  3. int main()
  4. {
  5. char[80] cake;
  6. bool good = false;
  7. while (!good)
  8. {
  9. cout << "please enter a cake: "
  10. cin >> cake;
  11. CheckInput(cake, &good);
  12. }
  13. //...
  14. }
  15.  
  16. void CkeckInput(char[80] cake, bool* good)
  17. {
  18. // run a switch checking cake agiants the valid choices
  19. // if the check succeds then set good to true otherwies do nothing
  20. }

im not sure if you are using pointers though so if you are not this solution wont work.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,970
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: string and integer validating

 
0
  #5
Sep 21st, 2009
Originally Posted by NathanOliver View Post
a way you can do this would be to have a void check function inside a while loop in your main function.
  1. void CheckInput(char[80], bool*);
  2.  
  3. int main()
  4. {
  5. char[80] cake;
  6. bool good = false;
  7. while (!good)
  8. {
  9. cout << "please enter a cake: "
  10. cin >> cake;
  11. CheckInput(cake, &good);
  12. }
  13. //...
  14. }
  15.  
  16. void CkeckInput(char[80] cake, bool* good)
  17. {
  18. // run a switch checking cake agiants the valid choices
  19. // if the check succeds then set good to true otherwies do nothing
  20. }

im not sure if you are using pointers though so if you are not this solution wont work.
Since cake is a character array, the following instruction will allow an array overflow (in case there are more than 80 characters entered):
cin >> cake; ,
you can prevent this by for example using
cin.getline(cake, 80);
or cin >> setw(80) >> cake; (but for this one you'll need to include the iomanip-header)
Last edited by tux4life; Sep 21st, 2009 at 4:57 pm. Reason: add something crucial
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: string and integer validating

 
0
  #6
Sep 22nd, 2009
yeah that is true tux. i normally use getline but this was a really quick and dirty example. i would hope the O.P. would use good programing techniques in his code.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: Vani3863 is an unknown quantity at this point 
Solved Threads: 0
Vani3863's Avatar
Vani3863 Vani3863 is offline Offline
Newbie Poster

Re: string and integer validating

 
0
  #7
Sep 30th, 2009
Hi Guys,

I'm working on the same assignment, and this question is really confusing me.

I get that we have to use a void function with two parameters (one string and one int) but I cannot get the while condition in the do..while loop to execute.

This is what I've managed so far but I know I'm making lots of mistakes.

The text in Red was given to us in the assignment and we have to work out the void function. This is only step one of the question and its frustrating cos I have nothing else to work with.

Does this make sense to anyone else???

Thanks for letting me rant, I needed to or I would be killing someone right now

PROGRAM:
//Assignment 2 - Question 5
#include <iostream>
#include <string>
using namespace std;

// void inputAndValidate function.

void inputAndValidate(string & sort, int & number)
{
do
{
cout << "Enter the type of cake ordered " << endl;
cout << "(chocolate, carrot, custard, fruit or coffee): ";
cin >> sort;

cout << "Enter the number of cakes ordered: ";
cin >> number;
}
while ((sort != 'chocolate') && (sort != 'carrot') && (sort != 'custard') && (sort != 'fruit') && (sort != 'coffee')
}
int main( )
{
string sort;
int number;

inputAndValidate(sort, number);

cout << number << " " << sort << " cake(s). " << endl;

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: string and integer validating

 
0
  #8
Sep 30th, 2009
on this line
  1. while ((sort != 'chocolate') && (sort != 'carrot') && (sort != 'custard') && (sort != 'fruit') && (sort != 'coffee')
you are using single quotes for the words. you need to use double quotes.
  1. while ((sort != "chocolate") && (sort != "carrot") && (sort != "custard") && (sort != "fruit") && (sort != "coffee")

also since this thread is solved i would suggest starting a new one if you have any other problems.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 4
Reputation: Vani3863 is an unknown quantity at this point 
Solved Threads: 0
Vani3863's Avatar
Vani3863 Vani3863 is offline Offline
Newbie Poster

Re: string and integer validating

 
0
  #9
Oct 1st, 2009
Thanks so much !!! That really helped.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 604 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC