Validating user input

I am looking to validate user input in my program.
I used an if else statement but I do not think I am using it correctly here is my code.
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
118
  
#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 ,
	{
	
		if(i<0)
		{
			cout<<"Invalid input try again";
		}
		else
		{
		
		
    	    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();//ignore newline character left after number for voice
}
		}
   
        
 

 	
		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;

}	

I posted this earlier and I posted on it seven times I figured out some of the answers to my questions. In that case I am sorry if I am overusing the forum. I appreciate any and all help I really do.
Last edited on
Instead of
1
2
3
4
5
6
7
8
	if (i<0)
	{
		cout << "Invalid input try again";
	}
	else
	{
//code
	}

use something like
1
2
3
4
5
6
		while (i < 0)
		{
			cout << "Invalid input try again";
			cin >> i;
		}
//code 


This:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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();//ignore newline character left after number for voice

	}
Last edited on
When I plug while in it still doesn't work. I plugged it in right I believe.
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
#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();//ignore newline character left after number for voice
}
        
 

 	
		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;

}	
Are you experiencing any problem?
BTW, what about simplifying your code a bit? You correctly created a const int size = 10; and took advantage of it once.
Why not twice?
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
#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 ,
   {
      cout << "\n\nCustomer n. " << i+1 << "\n==============\n";
      cout << "What is the name of the customer? ";
      getline(cin, AllCustomers[i].name);

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

   for (int i = 0; i < size; i++)
   {
      cout << "\nCustomer n. " << i+1 << ": " << AllCustomers[i].name
           << "\n   internet:   " << AllCustomers[i].internet
           << "\n   voice:      " << AllCustomers[i].voice
           << "\n   television: " << AllCustomers[i].television
           << "\n   Monthly total: "
           << AllCustomers[i].voice + AllCustomers[i].television +
               AllCustomers[i].internet
           << endl;
   }
}

Topic archived. No new replies allowed.