URGENT! User input validation

How do I get this program to validate user input. I just want to make sure no negative numbers are input. i used a while look and a if even statement and cant get it to work. I may be putting them in the wrong place.

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  	
#include <iostream>
#include <string>
 #include <iomanip>

using namespace std;



struct customerBundle
{
 string name;
 double internet;
 double voice;
 double television;
};

void billing(customerBundle AllCustomers[] ,int);

int main()
{
	
	
    const int size = 10;
    customerBundle AllCustomers[size];
    billing(AllCustomers, size);
    
    return 0;
}




void billing(customerBundle AllCustomers[], int size)
{
	
	 
    
    	
for (int i = 0; i < size; i++) // use ; not ,
	{

	
	while (i < 0)
		{
			cout << "Invalid input try again";
			cin >> i;
		

}
		cout << "What is the name of the customer? " << endl;
		getline(cin, AllCustomers[i].name);


		cout << "What value will this customer pay for internet? " << endl;
		cin >> AllCustomers[i].internet;
		cout << "What about TV? " << endl;
		cin >> AllCustomers[i].television;
		cout << "How much for voice? " << endl;
		cin >> AllCustomers[i].voice;
		cin.ignore();
	
}
        
 

 	
		cout<<AllCustomers[0].name<<"\n"<<AllCustomers[0].internet<<"\n";
cout<<AllCustomers[0].voice<<"\n"<<AllCustomers[0].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[0].voice+AllCustomers[0].television+AllCustomers[0].internet;
cout<<endl;  

cout<<AllCustomers[1].name<<"\n"<<AllCustomers[1].internet<<"\n";
cout<<AllCustomers[1].voice<<"\n"<<AllCustomers[1].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[1].voice+AllCustomers[1].television+AllCustomers[1].internet;
cout<<endl;  

cout<<AllCustomers[2].name<<"\n"<<AllCustomers[2].internet<<"\n";
cout<<AllCustomers[2].voice<<"\n"<<AllCustomers[2].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[2].voice+AllCustomers[2].television+AllCustomers[2].internet;
cout<<endl;


cout<<AllCustomers[3].name<<"\n"<<AllCustomers[3].internet<<"\n";
cout<<AllCustomers[3].voice<<"\n"<<AllCustomers[3].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[3].voice+AllCustomers[3].television+AllCustomers[3].internet;
cout<<endl;

cout<<AllCustomers[4].name<<"\n"<<AllCustomers[4].internet<<"\n";
cout<<AllCustomers[4].voice<<"\n"<<AllCustomers[4].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[4].voice+AllCustomers[4].television+AllCustomers[4].internet;
cout<<endl;
cout<<AllCustomers[5].name<<"\n"<<AllCustomers[5].internet<<"\n";
cout<<AllCustomers[5].voice<<"\n"<<AllCustomers[5].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[5].voice+AllCustomers[5].television+AllCustomers[5].internet;
cout<<endl;
cout<<AllCustomers[6].name<<"\n"<<AllCustomers[6].internet<<"\n";
cout<<AllCustomers[6].voice<<"\n"<<AllCustomers[6].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[6].voice+AllCustomers[6].television+AllCustomers[6].internet;

cout<<endl;cout
<<AllCustomers[7].name<<"\n"<<AllCustomers[7].internet<<"\n";
cout<<AllCustomers[7].voice<<"\n"<<AllCustomers[7].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[7].voice+AllCustomers[7].television+AllCustomers[7].internet;
cout<<endl

;cout<<AllCustomers[8].name<<"\n"<<AllCustomers[8].internet<<"\n";
cout<<AllCustomers[8].voice<<"\n"<<AllCustomers[8].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[8].voice+AllCustomers[8].television+AllCustomers[8].internet;
cout<<endl;

cout<<AllCustomers[9].name<<"\n"<<AllCustomers[9].internet<<"\n";
cout<<AllCustomers[9].voice<<"\n"<<AllCustomers[9].television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers[9].voice+AllCustomers[9].television+AllCustomers[9].internet;
cout<<endl;

}			
Last edited on
Your check while (i < 0) will immediately fail. Your compiler might (probably?) optimize it out because it can tell it'll never be reached.

Are you trying to make sure they don't enter a negative number for the amount they will pay?
1
2
3
4
5
6
7
8
double input = -1;
std::cout << "Input payment amount: ";
std::cin >> input;

while (input <= 0) {
  std::cout << "Only non-negative amounts are allowed. Try again: ";
  std::cin >> input;
}

Assuming you don't enter an alphabetic character and you don't enter spaces. You will need something else to combat that.

You could stick the input validation into a function so you don't have to repeat it a bunch of times.
So are you saying repeat the while loop after every function in order to get that to work or stick it in a function?
Topic archived. No new replies allowed.