registration using do-while activity

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.

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
#include <iostream>
using namespace std;
int main(){
	string a, b, c, d;
	int e;
	char f='y';
	do{
		cout<<"Name: ";
		cin>>a;
		cout<<"Address: ";
		cin>>b;
		cout<<"Age: ";
		cin>>e;
		cout<<"Course: ";
		cin>>c;
		cout<<"Gender: ";
		cin>>d;
		cout<<"Do you need to register another user?\n";
		cout<<"Press 'y' if yes and press 'n' if no: ";
		cin>>f;
		
		if(f=='y'||f=='Y'){
			continue;
		}else if(f=='n'||f=='N'){
			break;
		}
	}
	while(true);{
				
}
}


I don't know where should I put the display of the list of registered users.
You are "registering" a single user, not a bunch of users. Any additional user's data entered will overwrite the single-use variables.
can you tell me how to add additional user's data? I'm still a beginner here.
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.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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 { };
	unsigned int 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 (const auto& 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.

https://www.learncpp.com/cpp-tutorial/introduction-to-structs-members-and-member-selection/
Last edited on
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
#include <iostream>

using namespace 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;
}
@againtry can you tell me how to display all the list of registered users? that's the only one that I'm really having a problem with it.
@zephina21
Have a read of http://www.cplusplus.com/reference/list/list/begin/
Theres a sample program there and all you do is use a loop to run through the list in the same fashion as an ordinary array.

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 :)
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
43
44
45
#include <iostream>
#include<list>

using namespace 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
Now you have to combine that with the concept of struct's.

I might add in passing that <list>'s aren't necessarily the best choice of container here. You could in fact just have a c-style array of User's

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
43
44
45
46
47
48
#include <iostream>
//#include<list>

using namespace 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
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
//#include<list>

using namespace 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
What in the while loop are you guys doing?

@OP: Don't misunderstand the assignment. Projects like these are an exercise in program flow.

WARNING: There is an Easter egg which will probably get you in a LOT of trouble if you use my code as is:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <vector>

void pause()
{
    std::cout << "\nPAUSED\n";

    std::cin.clear();
    std::cin.ignore();
}

struct USER
{
        std::string name;
        std::string address;
        short age;
        std::string course;
        long double gender;

        USER()
        {

            std::cin.ignore();

            std::cout << "Name: ";
            std::getline(std::cin, name);

            std::cout << "Address: ";
            std::getline(std::cin, address);

            std::cout << "Course: ";
            std::getline(std::cin, course);

            std::cout << "Gender Code: ";
            std::cin >> gender;
        }

        void Display()
        {
            std::cout << "Name: ";
            std::cout << name << std::endl;

            std::cout << "Address: ";
            std::cout << address << std::endl;

            std::cout << "Course: ";
            std::cout << course << std::endl;

            std::cout << "Gender Code: ";
            std::cout << gender << std::endl;

            std::cout << std::endl;
        }
};



int main()
{
    bool Continue = true;
    char cont;
    int choice = 6;
    int iterate = 0;
    std::vector<USER> Students;



    while(Continue)
    {

        std::cout << "1.) Enter Students \n2.) Display Students\n";
        std::cin >> choice;

        std::cin.clear();

        if(choice == 1)
        {
            USER NewGuy;
            Students.push_back(NewGuy);
        }
        if(choice == 2)
        {

            if(!Students.empty())
            {
                iterate = 0;

                while(iterate != Students.size())
                {
                    Students.at(iterate).Display();
                    iterate++;
                }
            }
        }

        std::cout << "\nDo You Wish To Continue? ";
        std::cin >> cont;

        if(toupper(cont) == 'N')
        {
            Continue = false;
        }
    }


    //pause();

}
Last edited on
so my instructor gave me the clue.. and i kinda figure it out..
like this
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
#include <iostream>
using namespace 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
Do you mean for L29:

 
    g += '\n';

Topic archived. No new replies allowed.