Hi guys. I am having a hard time figuring out how to code the following;
It is for my assignment that is due tonight;
Write a program to print a particular greeting to members of a forum. The program should ask users to indicate if he/she is a new member or not. A new member will be asked to enter his/her first name and surname. Then a welcome message with the user’s name and the information on how to use the forum are displayed. An existing member will be asked to enter the date of the last visit, and a welcome message with this date will be displayed. This program should include two functions welcomeNewMemeber and welcomeExistingMemeber. Also, this program should i) allow an indefinite number (<= 20) of users to enter the above information and get the corresponding greeting, until the key ‘q’ is entered to stop this program; ii) display greetings with the following information: You are the xth new member so far or You are the xth existing member so far. The “xth” shall increase appropriately with the number of new or existing members that have been entered into this program. Note that you need to use “1st”, “2nd” and “3rd” for the first three new or existing members instead of “xth”.
#include <iostream>
usingnamespace std;
void welcomeNewMember ();
void welcomeExistingMember ();
int main ()
{
char q;
int member;
int numOfMembers=0;
while (member !=q)
{
cout<<"Please press 1 if you are a member or press 0 if you want to become a member: "<<endl;
cin>>member;
numOfMembers+=member;
numOfMembers++;
if (member == 1)
{
welcomeExistingMember();
}
elseif (member == 0)
{
welcomeNewMember();
}
else
{
cout<<"You have entered an invalid number"<<endl;
}
}
return 0;
}
void welcomeExistingMember()
{
int last_visit;
cout<<"Please enter your last visit: "<<endl;
cin>>last_visit;
cout<<"Welcome, your last visit is "<<last_visit;
}
void welcomeNewMember()
{
char name[20];
char surname[20];
cout<<"Please enter your first and last name: "<<endl;
cin>>name>>surname;
cout<<"Welcome "<<name<<" "<<surname;
}
What particular problems are you having? I noticed it runs using the shell. No errors, just a small warning about q not being initialized and no output.
As seen in the description; "You are the xth existing member so far. The “xth” shall increase appropriately with the number of new or existing members that have been entered into this program. Note that you need to use “1st”, “2nd” and “3rd” for the first three new or existing members instead of “xth”."
This is the part that I am trying to figure out. I know I need to somehow put a for loop inside functions.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string name[20], surname[20];
string suffix[4] = {" ", "st", "nd", "rd", "th" };
char Quit;
int memberType;
int numOfMembers = 0;
while ( Quit != 'q' )
{
cout << "Please press 1 if you are a member or press 0 if you want to become a member: "<<endl;
cin>>memberType;
if ( memberType == 0 )\
{
numOfMembers++;
cout<<"Please enter your first and last name: "<<endl;
cin >> name[numOfMembers] >> surname[numOfMembers];
cout<<"Welcome " << name[numOfMembers] << " " << surname[numOfMembers];
switch (numOfMembers) {
case 1:
cout << suffix[1];
break;
case 2:
cout << suffix[2];
break;
// bla blah blah
default:
cout << suffix[4];
}
}
elseif ( memberType == 1 )
{
//blah blah blah
// ... last_visit;
cout<<"Welcome, your last visit is "<<last_visit;
}
else
cout<<"You have entered an invalid number"<<endl;
cout << "Please press q if you weant to quit, anything else to continue "<<endl;
cin >> Quit;
}
return 0;
}