How do you give an error for someone entering a letter

  • Thread starter Thread starter odinx
  • Start date Start date
  • Tags Tags
    Error
Click For Summary
SUMMARY

The discussion focuses on error handling in C++ when a user inputs a non-numeric value during data entry. The user attempts to implement error checking using the standard input stream, but encounters issues with repeated error messages. The solution involves using cin.clear() to reset the input stream state and cin.ignore() to flush the invalid input. This allows the program to prompt the user correctly for the next number after an invalid entry.

PREREQUISITES
  • Understanding of C++ input/output streams
  • Familiarity with error handling in C++
  • Knowledge of basic sorting algorithms
  • Experience with arrays in C++
NEXT STEPS
  • Implement cin.ignore() to flush invalid input in C++
  • Explore advanced error handling techniques in C++
  • Learn about input validation best practices in user interfaces
  • Study sorting algorithms and their implementations in C++
USEFUL FOR

C++ developers, software engineers, and anyone interested in improving user input validation in console applications.

odinx
Messages
9
Reaction score
0
Code:
 #include <iostream>
#include <cstdlib>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;

double sort(double input[], int N)
{
	double temp;
	int i, j;
	
	for (i=0; i<=8; i++)
	{
		for (j=i+1; j<=9; j++)
		{	
			if(input[i]>input[j])
			{
				temp=input[i];
				input[i]=input[j];
				input[j]=temp;
	        }
		}
	}
	return input[N];
}

int main()
{
	const int N=10;
	double x, y, sum, total, input[N];
	int i;
	sum=0;
	x=1;
   
	cout << "Enter 10 numbers: " << endl;
	for (i=0; i<=9; i++)
	{
		if (x<=10)
		{
			cout << "Number " << x << ": "; 
			cin >> input[i];
			x++;
		}
		else if (x==10)
		{
			break;
		}
	}

	sort(input, N);
	
	cout << "Here are the numbers in sorted order:" << endl;
	
	for (i=0; i<=9; i++)
	{
		cout << input[i] << " ";		
	}
	
	y=((input[N/2] + input[N/2-1])/2);
	cout <<"\nThe median is " << y << endl;
	
	for(i=0; i<=9; i++)
	{	
		sum=sum+input[i];
	}
	
	total=(sum/10);
	cout << "The mean is " << total << endl;
	
	return EXIT_SUCCESS;
}

I'm trying to give an error message when the user doesn't enter a number, but I haven't been able to get it to work.

this is what I tried:
Code:
	cout << "Enter 10 numbers: " << endl;
	for (i=0; i<=9; i++)
	{
		if (x<=10)
		{
			cout << "Number " << x << ": "; 
			cin >> input[i];
			if (!cin)
                        {
                              cin.clear();
                              cout << "Error: You Must Enter A Number. " << endl;
                              cin >> input[i];
                         }
                        x++;
                        
		}
		else if (x==10)
		{
			break;
		}
	}
I tried that out but it would repeat the error for all 10 input, instead of giving an error for that 1st one. I want to give an error for that one time they enter a letter, then have the program tell them to redo their input, then have it proceed to the next number "number 2: " So anyone have any ideas of what I should try?
 
Physics news on Phys.org
Welcome to PF, odinx! :smile:

You also need to flush the input to get rid of the letter that's there.
Adding cin.flush() will do that.
Note that you also need cin.clear() to clear the invalid state of the stream.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K