Structures


I need help with this program
Whenever I compile it I can run it and enter the name of the employee and the first price but then it just jumps to the other two after that. Also how would I declare this in an array for 10 customers to display 10 customers names and their data? Thanks for the help

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

#include <iostream>
#include <string>
 #include <iomanip>

using namespace std;


void billing();
struct customerBundles
{
 string name;
 double internet;
 double voice;
 double television;
};



	int main()
	{
billing();

	return 0;
}



void billing()
{
	customerBundles customer;
	
	cout<<"What are the names of the customers? "<<endl;
	cin>>customer.name;
	cout<<"What value will this customer pay for internet? "<<endl;
	cin>>customer.internet;
	cout<<"What about TV? "<<endl;
	cout<<"How much for voice? "<<endl;
	cin>>customer.voice;
	
		

//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;
}

Last edited on
Line 34: when you use >> to read a string, the program will stop reading when it gets to any white space. So if you enter "Donald Trump", it will read "Donald" into the string and leave "Trump" in the stream. Use getline() instead to read the entire line:
getline(cin, customer.name);

Line 37: You prompt for the TV cost, but you don't read it.
 
Also how would I declare this in an array for 10 customers


Start with a small change: pass the customer instance that you want to read into billing(). Then declare the customerBundles object inside main and pass it to billing(). This makes the program a lot more flexible:

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
#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 customer;

    cout << "What are the names of the customers? " << endl;
    cin >> customer.name;
    cout << "What value will this customer pay for internet? " << endl;
    cin >> customer.internet;
    cout << "What about TV? " << endl;
    cout << "How much for voice? " << endl;
    cin >> customer.voice;

//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;
}


Now, you can declare an array of customerBundles like this;
customerBundles allCustomers[10];

And you read and print a particular customer like this:
1
2
// read and print customer #4:
billing(allcustomers[4]);


Modify the code by adding a for loop to read and print all 10 customers.
So when I do that it compiles and runs but my second value is turned into scientific notation I guess? So how do I fix that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void billing()
{
	customerBundles customer;
	
	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;
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;
}



What are the names of the customers?
Kody
What value will this customer pay for internet? 
25
What about TV?
15
How much for voice? 
35
Kody
25
2.10169e-317
35
Monthly total: 60






Last edited on
From there I tried implementing the array and everything is working but my for loop what is wrong in it?
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.
Last edited on
44
45
46
cout<<"How much for voice? "<<endl;
cin>>customer.television;
}
and I am dumb lol thanks for your help.
Your for loop runs 10 times, but the code within it reads into the same customer object each time.

The point of adding the customer parameter to billing() is that it lets main call billing to read any customer that main wants. So you want the array and the for loop in main(), not in billing().
Topic archived. No new replies allowed.