In: Computer Science
Tony Gaddis C++ Conference Sessions An upcoming conference about C/C++ programming has three sessions planned:
- A session on the new features of C++ 17
- A session on functional programming in C/C++
- A session on lamda functions
Attendees can subscribe for any session. The organizers want to keep track of which attendees are attending which session.
Write a program that stores this information in a two-dimensional array of Booleans, where each row represents an attendee and each column represents their subscription for a session. You can restrict the program to seven attendees, resulting in a 7X3 array. The program should first have the user input the data for each attendee. Write two functions that take the array as their argument. One function should print out the number of people in each session and the other should return the number of people who subscribed to all sessions.
#include <iostream>
#include<string>
//This method takes the user input and substitutes it with true or false.
std::string substituteWithToF(std::string str){
if(str == "TRUE" ||str == "T" ||str == "true"||str == "YES"||str == "Y"||str == "yes" ||str == "y" )
return "true";
if(str == "FALSE" ||str == "F" ||str == "false"||str == "NO"||str == "N"||str == "no" ||str == "n" )
return "false";
return "invalid";
}
//This method prints number of people attending each session
void printAttendees(std::string sessionManage[7][3]){
int session1Attendees = 0;
int session2Attendees = 0;
for(int i=0;i<7;i++){
for(int j=0;j<3;j++){
if(j==1)
if(sessionManage[i][j]=="true") session1Attendees++;
if(j==2)
if(sessionManage[i][j]=="true") session2Attendees++;
}
}
std::cout<<"Number of people in session 1: "<<session1Attendees;
std::cout<<"Number of people in session 2: "<<session2Attendees;
}
//This method prints number of people attending both session
int printAllAttendees(std::string sessionManage[7][3]){
int sessionAttendees = 0;
for(int i=0;i<7;i++){
if(sessionManage[i][1]=="true" && sessionManage[i][2]=="true" )sessionAttendees++;
}
return sessionAttendees;
}
int main() {
//Taking array of string type instead of boolean to store attendee name, and in later stage the response is converted to true and false. Also, this prevents any error caused due to incorrect input.
std::string sessionManage[7][3];std::string flag;
//Asking user to input details
for(int i=0;i<7;i++){
for(int j=0;j<3;j++){
if(j==0){
std::cout << "Enter students name:\n";
std::cin>>sessionManage[i][j];
}
do{
//do-while loop is added so that user enters true or false values only.
std::cout << "Has student attended session 1? Please enter true/false or yes/no only\n";
std::cin>>sessionManage[i][j];
flag=substituteWithToF(sessionManage[i][j]);
sessionManage[i][j] = flag;
}while(flag == "invalid");
}
}
//Method to print the number of people in each session
printAttendees(sessionManage);
//Method that return the number of people who subscribed to all sessions.
int sessionAttendees = printAllAttendees(sessionManage);
std::cout<<"People attending both the sessions: "<<sessionAttendees;
}