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.

Recommended Answers

All 8 Replies

Post down your code please.

commented: nice signature. +22

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 :

#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;
		}
	}
	
}

a way you can do this would be to have a void check function inside a while loop in your main function.

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.

a way you can do this would be to have a void check function inside a while loop in your main function.

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.

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)

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.

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;
}

on this line

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.

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.

Thanks so much !!! That really helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.