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

  • Context: Comp Sci 
  • Thread starter Thread starter Joe_K
  • Start date Start date
  • Tags Tags
    C++ Function
Click For Summary

Discussion Overview

The discussion revolves around implementing a string input validation loop in C++ for a function named Menu(). Participants are exploring how to correctly accept user input as a string, validate it against specific acceptable values, and handle invalid input through repeated prompts.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the requirements for the Menu() function, including the need to return a valid string input from the user.
  • Another participant questions the use of a pointer in the function signature, suggesting that passing a string by reference could be more appropriate.
  • A different participant proposes a pseudo code approach where the loop continues until a valid string is entered, indicating that the loop condition could be based on the validity of the string.
  • Concerns are raised about the function signature as specified by the professor, with some participants questioning whether it was a mistake to require no arguments.
  • One participant critiques the initial code provided, pointing out issues such as undeclared variables, missing braces in the if statement, and incorrect comparisons between string and integer types.

Areas of Agreement / Disagreement

Participants express differing views on the function signature and the approach to input validation. There is no consensus on whether the professor's requirement for no arguments is a mistake, and multiple suggestions for implementing the validation loop are presented without agreement on a single correct method.

Contextual Notes

Limitations include unclear assumptions about variable declarations and the handling of string comparisons. The discussion also reflects a lack of clarity on the expected behavior of the Menu() function in relation to user input.

Who May Find This Useful

Students learning about C++ programming, particularly those working on input validation and function implementation in a homework context.

Joe_K
Messages
32
Reaction score
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")

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

}}
 
Physics news on Phys.org
Why not Menu(&string)?
 
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
 
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?
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K