Need advice. beginners here

I am new to C++ programming. Recently i'm solving a question. Which involves
Questions :

Anyone donating RM500 and above will be given a ticket (for 1 person). Any donation in the multiple of RM500 will receive an equivalent mutiple number of tickets. Maximum of 100 diners.
Write a program to read names of the donors and the amounts donated consecutively. If the total number of donors is less than 100,the program should allow the user to terminate the input by entering "00" as the name.

Enter 00 to terminate.
Enter name: Bernard
Enter amount : RM 460
Enter name: Ben
Enter amount : RM 1300
Enter name : Neil
Enter amount : RM 2250
Enter name : Jasper
Enter amount : RM 300
Enter name : Jenifer
Enter amount : RM 1900
Enter name : 00
N0   NAME        AMOUNT DONATED(RM)         NUMBER OF TICKETS
1    Bernard     460.00                     0
2    Ben         1300.00                    2
3    Neil        2250.00                    4
4    Jasper      300.00                     0
5    Jenifer     1900.00                    3

The total number of donours is 5
Total amount collected is RM 6210.00
Total number of tickets is 9



This is my current code. i just did 1/4 part and i unable to progress further.
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
 #include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	int const limit=100;
	string name[limit];
        int amount[limit];
	int ticket[limit];
        int i;

	cout<<"Enter 00 to terminate."<<endl;
	
	for(i=0; i<limit; i++)
	{
	   cout<<"Enter name: ";
	   cin.ignore();
	   getline(cin,name[i]);
    if (name[i]=="00")
	{
		break;
	}
	   cout<<"Enter amount: RM ";
	   cin>>amount[limit];
	}
	if(amount[i]%500)
	{
		//statement
		
	}
system("Pause");
return 0;
}



EDIT: I'm not looking for any free answers. But i am very grateful if anyone else good guide me to answer this question. I am a new C++ learner. There is much more technique i need to learn.
Thank you.
Last edited on
It's not clear what you're stuck on.

A few hints:

Line 26: You probably want amount[i] here.

Line 28: You don't want to use the modulo operator here. A simple integer division will do.
1
2
3
4
  int total_tickets = 0;

  tickets[i] = amount / 500;
  total_tickets += tickets[i];


Lines 28-32: You want this if statement inside your for loop since you want to apply the logic to every sale.

After the for loop, you want to print out the list of donors.
Last edited on
Thanks for the reply @AbstractionAnon

for the line28-32

is it just add in

int total_tickets = 0;

tickets[i] = amount[i] / 500;
total_tickets += tickets[i];


The division that you gave have to include inside the if statement on m line 28 ?

this is my latest 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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
	int const limit=100;
	string name[limit];
    int amount[limit];
	int ticket[limit];
    int i;
	int total_tickets=0;

	cout<<"Enter 00 to terminate."<<endl;
	
	for(i=0; i<limit; i++)
	{
	   cout<<"Enter name: ";
	   cin.ignore();
	   getline(cin,name[i]);
    if (name[i]=="00")
	{
		break;
	}
	   cout<<"Enter amount: RM ";
	   cin>>amount[i];

	   ticket[i] = amount[i] / 500;
       total_tickets += ticket[i];
	}
	
    cout<<endl;
	cout<<left;
	cout<<setw(10)<<"NO"<<setw(10)<<"NAME"<<setw(10)<<"AMOUNT DONATED(RM)"<<setw(15)<<"NUMBER OF TICKETS"<<endl;

	for(i=0; i<limit; i++)
	{
	cout<<setw(10)<<i+1<<setw(10)<<name[i]<<setw(10)<<setprecision(2)<<amount[i]<<setw(15)<<total_tickets<<endl;
	}
system("Pause");
return 0;
}


EDIT : There is something wrong with my code. The last for loop doesn't stop when i enter 00 means that i promp to 100 instead of 5 from the output sample. And the ticket output is the same for each of them.
Last edited on
The last for loop doesn't stop when i enter 00

Line 36: limit is 100, so you;re going to loop 100 times. You need to count how many donations were made. Then loop that many times.
@AbstractionAnon Thank you for the reply once again. I will try it out
@AbstractionAnon sorry for bothering again. can i know how to count how many donations were made ? in line 36 the for loop which i<limit is wrong or i need to do another calculations ? can i get an example for 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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{   int const limit=100;
	string name[limit];
    int amount[limit];
	int ticket[limit];
    int total_tickets=0;
    int count = 0;      //  Number of donations
    
	cout<<"Enter 00 to terminate."<<endl;
    for (int i=0; i<limit; i++)
	{   cout<<"Enter name: ";
	    cin.ignore();
	    getline(cin,name[i]);
        if (name[i]=="00")
	        break;
	    cout<<"Enter amount: RM ";
	    cin>>amount[i];
        ticket[i] = amount[i] / 500;
        total_tickets += ticket[i];
        count++;        //  Increment # of donations
	}
	
    cout<<endl;
	cout<<left;
	cout<<setw(10)<<"NO"<<setw(10)<<"NAME"<<setw(10)<<"AMOUNT DONATED(RM)"<<setw(15)<<"NUMBER OF TICKETS"<<endl;

	for (int i=0; i<count; i++)   //  Loop only for # of donations
	{   cout<<setw(10)<<i+1<<setw(10)<<name[i]<<setw(10)<<setprecision(2)<<amount[i]<<setw(15)<<total_tickets<<endl;
	    //  You have a problem in the above line. You're printing total tickets to every donation
	}
    system("Pause");
    return 0;
}
Topic archived. No new replies allowed.