| | |
string and integer validating
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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.
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 :
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)
#include<iostream> #include<cctype> using namespace std; int main() { string phoneNumber = "777777777"; bool pass = true; for(int i = 0; i < phoneNumber.size(); i++) { if( isdigit(phoneNumber[i]) ) pass = true; else { pass = false; break; } } }
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
a way you can do this would be to have a void check function inside a while loop in your main function.
im not sure if you are using pointers though so if you are not this solution wont work.
c++ Syntax (Toggle Plain Text)
void CheckInput(char[80], bool*); int main() { char[80] cake; bool good = false; while (!good) { cout << "please enter a cake: " cin >> cake; CheckInput(cake, &good); } //... } void CkeckInput(char[80] cake, bool* good) { // run a switch checking cake agiants the valid choices // if the check succeds then set good to true otherwies do nothing }
im not sure if you are using pointers though so if you are not this solution wont work.
if you write
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
•
•
•
•
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)
void CheckInput(char[80], bool*); int main() { char[80] cake; bool good = false; while (!good) { cout << "please enter a cake: " cin >> cake; CheckInput(cake, &good); } //... } void CkeckInput(char[80] cake, bool* good) { // run a switch checking cake agiants the valid choices // if the check succeds then set good to true otherwies do nothing }
im not sure if you are using pointers though so if you are not this solution wont work.
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."
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
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
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;
}
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;
}
on this line
you are using single quotes for the words. you need to use double quotes.
also since this thread is solved i would suggest starting a new one if you have any other problems.
c++ Syntax (Toggle Plain Text)
while ((sort != 'chocolate') && (sort != 'carrot') && (sort != 'custard') && (sort != 'fruit') && (sort != 'coffee')
c++ Syntax (Toggle Plain Text)
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
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
![]() |
Similar Threads
- c++ very basic, convert a string to an integer (C++)
- Converting String to Integer (C)
- How to add string to an integer variable... (C++)
- string to integer OR float (C++)
- to convert string to integer value (Legacy and Other Languages)
- String to integer conversion (C)
- Convert a string to an integer (PHP)
- Converting String to Integer help (C++)
- String to integer to ascii (C)
Other Threads in the C++ Forum
- Previous Thread: Homework Help: Recursive Multiplication
- Next Thread: ambiguity in a code in c/c++
Views: 604 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






