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.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
intconst 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.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
intconst 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.
@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 ?
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{ intconst 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;
}