How Do You Correctly Implement a String Input Validation Loop in C++?

In summary, the function should take a string input from the user and return it if it is a valid choice or an error message.
  • #1
Joe_K
33
0

Homework Statement



I am writing a program which requires me to make and call a function: string Menu()

This function will display a message to the user. It will then accept the user's choice (as a string) and check it for validity - that is did the user enter a "1", "2", "3", "4", "Q", or "q"? If so, the string should be returned to the calling function (main()). If not, an error message should be displayed and the user should be given another change to make a choice - this should continue until the user enters a valid value.

Homework Equations

The Attempt at a Solution



What I am confused about is how I am supposed to take a string value from the user. Would it be correct to store this string value in a local string variable within the string Menu() function? I don't understand how I am supposed to return the string to the calling function if it is valid.

Right now, I have the function prototyped, and I have the calling statement, I just need to write the actual function code.

Would something like this be correct?

string Menu()
{
while (1)
{

if (user_value==1 || user_value==2 || user_value==3 || user_value==4 || user_value == "q" || user_value == "Q")

return user_value;
break;

else if (user_value!=1 || user_value!=2 || user_value!=3 || user_value!=4 || user_value != "q" || user_value != "Q")

cout<<"invalid choice, try again: "
cin>> userValue;

}}
 
Physics news on Phys.org
  • #2
Why not Menu(&string)?
 
  • #3
why not use the string value as the loop test?

** note: this is pseudo code not C++ code:

String menu()
{
String ans=""

while(ans=="") {
ans=askUser
if(ans not valid) ans=""
}

return ans
}

notice that the loop ends when the ans != "" ie the user entered a valid string
 
  • #4
Borek said:
Why not Menu(&string)?

I meant to type Menu(). To myself, Menu(string) would make more sense, but for some reason the professor told us to type it like that without any arguments. Is this most likely a mistake from her end, or is there some other way to take an argument?
 
  • #5
Joe_K said:
I meant to type Menu(). To myself, Menu(string) would make more sense, but for some reason the professor told us to type it like that without any arguments.
Is this most likely a mistake from her end, or is there some other way to take an argument?
I doubt very much that this is a mistake. The program requirements are that this function takes no arguments, and returns a string entered by the user to the caller (main).

I see several things wrong with your code.
1) user_value is not declared.
2) userValue is not declared. Why do you have two variables with similar names when one would suffice?
3) your if statement is missing the braces - { } - for its body. In some limited cases you can omit the braces, but it's generally a good idea to include them, even when you don't have to. In your case, this causes a syntax error that the compiler will flag.
4) user_value is likely a string variable, yet you are comparing it to integer constants.
5) your break statement will never execute, since it comes after a return statement.
 

FAQ: How Do You Correctly Implement a String Input Validation Loop in C++?

1. What is a function in C++ and how do I call it?

A function in C++ is a block of code that performs a specific task. It is defined with a name, optional parameters, and a return type. To call a function, you simply use its name followed by parentheses and any required arguments inside the parentheses.

2. How do I pass arguments to a function in C++?

Arguments can be passed to a function in C++ in two ways: by value or by reference. When passing by value, the function makes a copy of the argument's value. When passing by reference, the function receives a reference to the original argument, allowing it to modify the original value.

3. Can a function in C++ return a value?

Yes, a function in C++ can have a return type, which specifies the type of value that the function will return. The return statement is used to specify the value to be returned from the function. If the function does not have a return type, it is declared as void and does not return a value.

4. What is function overloading in C++?

Function overloading in C++ allows you to have multiple functions with the same name but different parameter types, allowing for more flexibility and reusability in your code. The compiler determines which function to call based on the number and types of arguments passed to the function.

5. How do I call a function from another function in C++?

To call a function from another function in C++, simply use its name followed by parentheses and any required arguments, just like you would when calling a function from the main function. Make sure to properly declare the function before calling it to avoid any errors.

Similar threads

Replies
2
Views
1K
Replies
14
Views
4K
Replies
12
Views
2K
Replies
2
Views
2K
Replies
6
Views
3K
Replies
10
Views
2K
Replies
3
Views
2K
Replies
7
Views
1K
Back
Top