How to validate the input?

I need help declaring an array to get my customer bundles to display 10 different customer bundles using input. I've got most of it input in the code but I can not get my for loop to run the array. For some reason arrays are just baffling me.
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 customerBundles
{
 string name;
 double internet;
 double voice;
 double television;
};

void billing(customerBundles &);

	int main()
	{
		customerBundles customer;
billing(customer);

	return 0;
}



void billing(customerBundles &customer)
{
	customerBundles allCustomers[10];
	for(allCustomer=1,allCustomer<10,allCustomer ++ )
	
	{
	
	cout<<"What are the names of the customers? "<<endl;
	getline(cin, customer.name);
	cout<<"What value will this customer pay for internet? "<<endl;
cin>>customer.internet;
	
	cout<<"What about TV? "<<endl;
	cin>>customer.television;
	
	cout<<"How much for voice? "<<endl;
cin>>customer.television;
}
		

//display data
cout<<customer.name<<"\n"<<customer.internet<<"\n";
cout<<customer.voice<<"\n"<<customer.television<<"\n"<<endl;
cout<<"Monthly total:"<<customer.voice+customer.television+customer.internet;
cout<<endl;
}


I've tried several different ways like using just customer in the loop and customerBundles. Thanks for any and all help
Last edited on
Here's one way to do it. I've created an array of customerBundle type in the main function, the size of which is determined by a constant integer variable size. Then I call the billing function with the name of the array and the size.

1
2
3
4
5
6
7
8
int main()
{
    const int size = 10;
    customerBundle AllCustomers[size];
    billing(AllCustomers, size);
    
    return 0;
}


The billing function then uses the size sent in to control the running of the for loop. The [i] is used to access the current customerBundle in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void billing(customerBundle AllCustomers[], int size)
{
	for(int i=0; i<size; i++) // use ; not ,
	{
    	    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; // the code you posted has a second television here instead of voice
            cin.ignore();//ignore newline character left after number for voice
        }
}


I'm not sure if you need to display each customer's info as it's entered or if you need to wait until all customers are entered and then output for all of them. You could have a separate display function that displays the information for a single customer and/or array of customers.
Last edited on
Okay so when I input that and try to print my statements it tells me that cout<< does not name a type why is that?
1
2
3
4
5
6
cout<<AllCustomers.name<<"\n"<<AllCustomers.internet<<"\n";
cout<<AllCustomers.voice<<"\n"<<AllCustomers.television<<"\n"<<endl;
cout<<"Monthly total:"<<AllCustomers.voice+AllCustomers.television+AllCustomer.internet;
cout<<endl;

Where are you trying to print out the customer bundle info? If you include it in the billing function, you can access each customer the same way as was done to add the information, e.g. AllCustomers[i].internet - the [i] indicates which customer in the array you are trying to print information from.

The array AllCustomers itself does not have a name, internet, television and voice. Each element of the array is a struct that has those fields.
Okay so I figured out why it was telling me that cout wasnt a type I didnt even have it inside the billing function but now it is telling me [Error] request for member 'internet' in 'AllCustomers', which is of pointer type 'customerBundle*' (maybe you meant to use '->' ?)
what do I do there?
Now I've got the cout statement working. Do I have to copy and paste that all the way to 0 to 9 in order to get it to print all 10 customers or is there a shortcut?
1
2
3
4
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;  

This is my code
You can use the same for loop approach you used to input data into the customer array. Instead of [1] you'd use [i] (or whatever variable your using in your for loop control.

I don't know if your assignment asks for a separate print or display function, or if you can add output statements to your for loop in the billing function.

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



#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<<"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; // the code you posted has a second television here instead of 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;
}	


This is my running code and it works good but just out of curiosity how would you validate the input so I there are no negative numbers input? I know it would be an if statement like
 
if (AllCustomers<1) 

but where would it go?
sorry for the forum blow up I've kind of been trying to answer my own questions
Last edited on
You could validate the input right after it's entered in the for loop. You'd have to do that for each service in the bundle. Since it has to be done multiple times, you could consider creating a separate function to get input and validate it. There's other validation you could do - for example, what if they type in an alpha character instead of a number?

And you can pull the output code into the for loop instead of repeating the same code 10 times.

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
void billing(customerBundle AllCustomers[], int size)
{
	
	for(int i=0; i<size; i++)
	{
	    // get input
    	    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;
            while (AllCustomers[i].internet < 1){
                cout // whatever message you want to prompt them to enter at least 1
                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();
      
            // output
            cout<<AllCustomers[i].name<<"\n"<<AllCustomers[i].internet<<"\n";
            cout<<AllCustomers[i].voice<<"\n"<<AllCustomers[i].television<<"\n"<<endl;
            cout<<"Monthly total:"<<AllCustomers[i].voice + AllCustomers[i].television + AllCustomers[i].internet;
            cout<<endl;
        }
Topic archived. No new replies allowed.