Trouble with User Input & Error Checks

In summary, the code is asking for four different inputs during each iteration of the for loop, regardless of whether the user entered an invalid command or file name. This makes the "Try again." messages misleading and there are no prompts to guide the user.
  • #1
magnifik
360
0
i'm having trouble with user input & error checks. after the first iteration, if there is an error, the correct error message shows up. but for the next iterations, the error messages for the opening of the input and output files show along with the correct one. idk if that made any sense...

screen should look like:
e power input.txt output.txt

with an error it looks like:
a power input.txt output.txt
Command not allowed. Try again.

e p0wer input.txt output.txt
Key invalid. Try again.
Can't open file input.txt. Try again. // this should not show
Can't open file output.txt. Try again. // this should not show

Code:
int main(){
	char choice;
	string key;
	char inputFile[500];
	char outputFile[500];
	for(;;){
		cin >> choice;
		if (choice != 'e' && choice != 'd')
			cout << "Command not allowed. Try again." << endl;
		cin >> key;
		for (int i = 0; i < key.length(); i++){
			if(!isalpha(key[i]))
				cout << "Key invalid. Try again." << endl;
		}
		cin >> inputFile;
		inFile.open(inputFile);
		if (!inFile)
			cout << "Can't open file " << inputFile << ". Try again." << endl;
		cin >> outputFile;
		outFile.open(outputFile);
		if (!outFile)
			cout << "Can't open file " << outputFile << ". Try again." << endl;
	}
}
 
Physics news on Phys.org
  • #2
Think about what happens during each iteration of your for loop. You are asking for four different inputs regardless of whether the user typed a wrong command or invalid file name or whatever. For this reason, the "Try again." requests when the user enters erroneous input are very misleading, because the user doesn't get a chance to re-enter the bad input in that iteration of the for loop.

Besides this, there are no prompts to guide the poor user to what he should be entering.
 

What is user input?

User input refers to any information or data that is entered by a user into a computer or electronic system. This can include text, numbers, or other types of data.

Why is user input important?

User input is important because it allows users to interact with a computer or electronic system and provide the necessary information for it to perform a desired function or task.

What is an error check?

An error check is a process of validating user input to ensure that it is correct, complete, and appropriate for the intended use. This helps to prevent errors and ensure the accuracy and reliability of the system.

What are some common types of user input errors?

Some common types of user input errors include missing or incorrect data, invalid formats, and input that is outside of the expected range or values.

How can error checks be implemented in a system?

Error checks can be implemented by using various techniques such as data validation, data verification, and error handling. These techniques can be incorporated into the design and coding of the system to ensure that user input is accurately and appropriately processed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
Replies
10
Views
904
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
817
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top