Displaying invalid entry...

I need to display "invalid entry" if the user puts a character instead of an integer. I cant find a way to do that correctly. Sorry, im a beginner, lol.

and can you guys give any advise in making a program concise
thank you! Replies are very much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 #include <iostream>
using namespace std;

main()
{
 	  int number[4];
 	  int i, j;
 	  
 	  for(i = 0 ; i <= 3 ; i++)
	  {
	   		cout << "Enter a number: ";
	   		cin >> number[i];
			
			}	   		

	   		

      
      for(i = 0 ; i <= 2 ; i++)
      {
	   		for(j = i+1 ; j <= 3 ; j++)
	   		{
			 	  if(number[i] < number[j])
			 	  {
				   		int temp;
						   
						   temp = number[i];
						   number[i] = number[j];
						   number[j] = temp;
						     
				   			   }
			 	  
			 	  }
	   		
	   		}

 cout << "\n\nDescending: ";
for(i = 0 ; i <= 2 ; i++)
{		
       
 	  cout << number[i] << ", " ;
	  }
	   cout<< number[i];

      
      
      
      
    
      for(i = 0 ; i <= 2 ; i++)
      {
	   		for(j = i+1 ; j <= 3 ; j++)
	   		{
			 	  if(number[i] > number[j])
			 	  {
				   		int temp;
						   
						   temp = number[i];
						   number[i] = number[j];
						   number[j] = temp;
						     
				   			   }
			 	  
			 	  }
	   		
	   		}

cout << "\n\nAscending: ";
for(i = 0 ; i <= 2 ; i++)
{		
		
 	  cout << number[i] << ", " ;
	  }

	   cout<< number[i];
 cout << "\n\n\n";
      
      
      system("pause");
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	string str;
	int num;
	cin>>str;
	for(int i=0;i<str.length();i++)
	{     
                if(i==0 && (str[i]=='-') )
		{
			continue;
		}

		else if(!isdigit(str[i]))
		{
			cout<<"Enter a valid number input:\n";
			cin>>str;
			i=0;
		}
	}
	num=atoi(str.c_str());
	cout<<num;

this piece of code does your work, modify your code suitably!!!
Last edited on
Thank you, sir! :)

Btw, where should i put that? I am really a noob
Last edited on
Topic archived. No new replies allowed.