Write a program that can register a user with name, address, age, course and gender. Then the system will ask the user if she/he need to register another user, if yes the system will go back from the start, if no it will display the list of registered users below using do-while.
Fair warning, this probably uses parts of C++ you haven't learned yet. Plus it is not the best code. Written as a first pass quickie, with no data entry checking so garbage values can be entered. Especially when entering age.
#include <iostream>
#include <string> // std::string
#include <list>
// create a struct to organize a bunch of data
// default initialize the data using uniform initialization
// https://mbevin.wordpress.com/2012/11/16/uniform-initialization/struct User
{
std::string name { };
std::string address { };
unsignedint age { };
std::string course { };
std::string gender { };
};
int main()
{
// create a list to hold your registered users
// no users at the start
std::list<User> users;
bool again { true };
do
{
// create a temp user for data entry
User temp;
// display the current user to register
// use the list's size + 1
std::cout << "User #" << users.size() + 1 << ":\n";
// use std::getline to get an entire line of data
// including spaces
std::cout << "Name: ";
std::getline(std::cin, temp.name);
std::cout << "Address: ";
std::getline(std::cin, temp.address);
std::cout << "Age: ";
std::cin >> temp.age;
// mixing std::getline and std::cin >> causes input problems
// https://www.cplusplus.com/forum/general/51433/
// correct it with
std::cin.ignore(100, '\n');
std::cout << "Course: ";
std::getline(std::cin, temp.course);
std::cout << "Gender: ";
std::getline(std::cin, temp.gender);
char yesno { '\0' };
// add the entered user to the list
users.push_back(temp);
std::cout << "Do you want to enter another user? ";
std::cin >> yesno;
std::cin.ignore(100, '\n');
if (yesno != 'y' && yesno != 'Y') again = false;
} while (again);
// walk through the list of users to display them
// no formatting
for (constauto& itr : users)
{
std::cout << itr.name << ", "
<< itr.address << ", "
<< itr.age << ", "
<< itr.course << ", "
<< itr.gender << '\n';
}
}
User #1:
Name: George P
Address: 1234 Anywhere Lane
Age: 45
Course: This one
Gender: M
Do you want to enter another user? y
User #2:
Name: Sarah J
Address: 45 Utopia Court
Age: 23
Course: Another one
Gender: f
Do you want to enter another user? n
George P, 1234 Anywhere Lane, 45, This one, M
Sarah J, 45 Utopia Court, 23, Another one, f
The person entering the data has to be accurate. Entering anything but a number for the age will bollix this up.
Oh, I forgot to give you a nice tasty link for learning about structs. Enjoy.
#include <iostream>
usingnamespace std;
int main()
{
string a, b, c, d;
int e;
char f='y';
// TO MAKE SURE YOU EVEN WANT TO ENTER A RECORD IN THE FIRST PLACE
cout << "Do you want to enter data? ";
cin >> f;
while(
toupper(f) == 'Y'
and cout<<"Name: "
and cin>>a
and cout<<"Address: "
and cin>>b
and cout<<"Age: "
and cin>>e
and cout<<"Course: "
and cin>>c
and cout<<"Gender: "
and cin>>d
and cout
<<"Do you need to register another user?\n"
<<"Press 'y' if yes and press 'n' if no: "
and cin>>f
)
{
// DO WHATEVER THE STORAGE SYSTEM REQUIRES
}
return 0;
}
Don't for get though, if you are saving each record as a struct then you have to 'tell' the machine (ie write a program) how your record must be displayed.
If I was you and it's all a bit of a mystery then I would satrt with commenting out all the extra bits and concentrate on just getting a start on a list of names, and then printing those out. I'll show you how shortly :)
#include <iostream>
#include<list>
usingnamespace std;
int main()
{
list<string> names_only;
string name, b, c, d;
int e;
char f='y';
// TO MAKE SURE YOU EVEN WANT TO ENTER A RECORD IN THE FIRST PLACE
cout << "Do you want to enter data? ";
cin >> f;
while(
toupper(f) == 'Y'
and cout<<"Name: "
and cin>>name
// and cout<<"Address: "
// and cin>>b
// and cout<<"Age: "
// and cin>>e
// and cout<<"Course: "
// and cin>>c
// and cout<<"Gender: "
// and cin>>d
and cout
<<"Do you need to register another user?\n"
<<"Press 'y' if yes and press 'n' if no: "
and cin>>f
)
{
// DO WHATEVER THE STORAGE SYSTEM REQUIRES
names_only.emplace_back(name);
}
// Display list
for (auto& x: names_only)
std::cout << x << '\n';
return 0;
}
Do you want to enter data? y
Name: Bob
Do you need to register another user?
Press 'y' if yes and press 'n' if no: y
Name: Bill
Do you need to register another user?
Press 'y' if yes and press 'n' if no: y
Name: Betty
Do you need to register another user?
Press 'y' if yes and press 'n' if no: n
Bob
Bill
Betty
Program ended with exit code: 0
#include <iostream>
//#include<list>
usingnamespace std;
int main()
{
string names_only[10];
string name, b, c, d;
int e;
char f='y';
int COUNTER{0};
// TO MAKE SURE YOU EVEN WANT TO ENTER A RECORD IN THE FIRST PLACE
cout << "Do you want to enter data? ";
cin >> f;
while(
toupper(f) == 'Y'
and cout<<"Name: "
and cin>>name
// and cout<<"Address: "
// and cin>>b
// and cout<<"Age: "
// and cin>>e
// and cout<<"Course: "
// and cin>>c
// and cout<<"Gender: "
// and cin>>d
and cout
<<"Do you need to register another user?\n"
<<"Press 'y' if yes and press 'n' if no: "
and cin>>f
)
{
// DO WHATEVER THE STORAGE SYSTEM REQUIRES
names_only[COUNTER] = name;
COUNTER++;
}
// Display list
for (int i = 0; i < COUNTER; i++)
std::cout << names_only[i] << '\n';
return 0;
}
Do you want to enter data? y
Name: Bob
Do you need to register another user?
Press 'y' if yes and press 'n' if no: y
Name: Bill
Do you need to register another user?
Press 'y' if yes and press 'n' if no: y
Name: Betty
Do you need to register another user?
Press 'y' if yes and press 'n' if no: n
Bob
Bill
Betty
Program ended with exit code: 0
#include <iostream>
//#include<list>
usingnamespace std;
int main()
{
string name, b, c, d;
int age;
char f='y';
struct User
{
string name{'?'};
int age{-99};
void print()
{
cout << name << ' ' << age << '\n';
}
};
User names_ages_only[10];
int COUNTER{0};
User temp;
// TO MAKE SURE YOU EVEN WANT TO ENTER A RECORD IN THE FIRST PLACE
cout << "Do you want to enter data? ";
cin >> f;
while(
toupper(f) == 'Y'
and cout<<"Name: "
and cin>>name
// and cout<<"Address: "
// and cin>>b
and cout<<"Age: "
and cin>>age
// and cout<<"Course: "
// and cin>>c
// and cout<<"Gender: "
// and cin>>d
and cout
<<"Do you need to register another user?\n"
<<"Press 'y' if yes and press 'n' if no: "
and cin>>f
)
{
// DO WHATEVER THE STORAGE SYSTEM REQUIRES
temp.name = name; temp.age = age;
names_ages_only[COUNTER] = temp;
COUNTER++;
}
// Display list
for (int i = 0; i < COUNTER; i++)
names_ages_only[i].print();
return 0;
}
Do you want to enter data? y
Name: Bob
Age: 21
Do you need to register another user?
Press 'y' if yes and press 'n' if no: y
Name: Bill
Age: 18
Do you need to register another user?
Press 'y' if yes and press 'n' if no: y
Name: Betty
Age: 19
Do you need to register another user?
Press 'y' if yes and press 'n' if no: n
Bob 21
Bill 18
Betty 19
Program ended with exit code: 0
#include <iostream>
usingnamespace std;
int main(){
string a, b, c, d, e, g;
char f='y';
do{
cout<<"Name: ";
cin>>a;
cout<<"\n";
cout<<"Address: ";
cin>>b;
cout<<"\n";
cout<<"Age: ";
cin>>e;
cout<<"Course: ";
cin>>c;
cout<<"Gender: ";
cin>>d;
g+=a;
g+=',';
g+=b;
g+=',';
g+=e;
g+=',';
g+=c;
g+=',';
g+=d;
g+=' ';
cout<<"Do you need to register another user?\n";
cout<<"Press 'y' if yes and press 'n' if no: ";
cin>>f;
}
while(f=='y'|| f=='Y');
cout<<g;
}
but im still having problem.. i dont know how to make it.. umm how do you say this.. "endl" i mean i want my output to make it "next line" like row.. my English are not that good