943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1027
  • C++ RSS
Sep 19th, 2009
0

string and integer validating

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
willywonka is offline Offline
6 posts
since Sep 2009
Sep 19th, 2009
1

Re: string and integer validating

Post down your code please.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Sep 19th, 2009
0

Re: string and integer validating

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 :
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 19th, 2009
0

Re: string and integer validating

a way you can do this would be to have a void check function inside a while loop in your main function.
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
Sep 21st, 2009
0

Re: string and integer validating

a way you can do this would be to have a void check function inside a while loop in your main function.
c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Sep 22nd, 2009
0

Re: string and integer validating

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.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
Sep 30th, 2009
0

Re: string and integer validating

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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vani3863 is offline Offline
6 posts
since Sep 2009
Sep 30th, 2009
0

Re: string and integer validating

on this line
c++ Syntax (Toggle Plain Text)
  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.
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
Oct 1st, 2009
0

Re: string and integer validating

Thanks so much !!! That really helped.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vani3863 is offline Offline
6 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Homework Help: Recursive Multiplication
Next Thread in C++ Forum Timeline: ambiguity in a code in c/c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC