user Register information using do-while loop

i wanna write a program that can register a user with
name, address, age, course and gender.
the the system will ask the user if he/she wants to register another user,
if yes then the system will go back from the start,
if no it will display the list of registered users below.
using do-while loop.
but every time i place my input like 3 or more, the output display will not show the list of registered people but only the very latest one i added.
where did i go wrong or what went missing?

here's what i did:


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
#include <iostream>
using namespace std;
int main ()
{
	string i, n, a, c, g;
	int age;
	char x;
	
 do { 
	cout<<"Enter Name: ";
	cin>>n;
	cout<<"Enter Age: ";
	cin>>age;
	cout<<"Enter Address: ";
	cin>>a;
	cout<<"Enter Course: ";
	cin>>c;
	cout<<"Enter Gender: ";
	cin>>g;
	
	cout<<"Do you want to register another Person? (Y/N) ";
	cin>>x;
	
	if (x=='Y'||x=='y'){
		continue;
	}else if (x=='N'||x=='n'){
		break;
	}
	
	}while (true);
	cout<<n<<endl;
	cout<<age<<endl;
	cout<<a<<endl;
	cout<<c<<endl;
	cout<<g<<endl;
	x++;
{
	return 0;
}
}
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

Using more descriptive names for your variables would also be helpful. More descriptive names self-documents what the code is doing.

Name instead of n, for example.

You want to use std::strings you should #include <string> .

If there is a possibility the data entered into a string might have spaces using std::cin will not work as intended. std::getline retrieves the entire line, including spaces, for string input.
https://www.cplusplus.com/reference/string/string/getline/
You are only showing the last values entered because you are using single use variables. To hold multiple values (more than one user) you need to use a container, like a std::vector.

Also using a struct to hold the different types of data makes for easy organization.

ONE possible way to do things based on what you've coded so far (with ZERO error checking):
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 <string>
#include <vector>
#include <cctype>  // ::tolower / ::toupper

struct User
{
   std::string Name;
   std::string Address;
   std::string Course;
   int         Age { 18 };     // set a default
   char        Gender { 'M' }; // set a default
};

int main()
{
   std::vector<User> users;  // blank container

   char yesno { 'y' };

   do
   {
      User        temp;    // create temporaries to hold input values
      std::string temp_str; 
      std::cout << "Enter Name: ";
      std::getline(std::cin, temp.Name);
      
      std::cout << "Enter Age: ";
      std::getline(std::cin, temp_str);

      temp.Age = std::stoi(temp_str); // convert string to int, doesn't error check

      std::cout << "Enter Address: ";
      std::getline(std::cin, temp.Address);
      
      std::cout << "Enter Course: ";
      std::getline(std::cin, temp.Course);

      std::cout << "Enter Gender: ";
      std::cin >> temp.Gender;

      temp.Gender = static_cast<char>(::toupper(temp.Gender));

      users.push_back(temp); // add the user to the container

      std::cout << "\nDo you want to enter another? ";
      std::cin >> yesno;
      
      std::cin.ignore(1000000, '\n');  // ignore what's left in the input buffer
   } while ('n' != static_cast<char>(::tolower(yesno)));

   for (size_t i { }; i < users.size(); ++i)
   {
      std::cout << "\nName: " << users[i].Name << '\n';
      std::cout << "Address: " << users[i].Address << '\n';
      std::cout << "Age: " << users[i].Age << '\n';
      std::cout << "Gender: " << users[i].Gender << '\n';
      std::cout << "Course: " << users[i].Course << '\n';
   }
}
Enter Name: Phil McCavity
Enter Age: 25
Enter Address: 100 Nowhere Lane
Enter Course: C++ 101
Enter Gender: m

Do you want to enter another? y
Enter Name: Debra Wing
Enter Age: 18
Enter Address: 123 MS Way
Enter Course: Windows 404
Enter Gender: F

Do you want to enter another? n

Name: Phil McCavity
Address: 100 Nowhere Lane
Age: 25
Gender: M
Course: C++ 101

Name: Debra Wing
Address: 123 MS Way
Age: 18
Gender: F
Course: Windows 404
sorry new to this website.
ow sweet ok i will try to add it in my program now and see if it works.
i tried it out but it did not work.
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
#include <iostream>
#include <string>
using namespace std;
int main ()
{
	std::string name, address, course, gender;
	int age;
	char x;
	
 do { 
	std::cout<<"Enter Name: ";
	std::getline (std::cin,name);
                                          //Error starts here
	std::cout<<"Enter Age: ";                                
	std::getline (std::cin,age);
	std::cout<<"Enter Address: ";
	std::getline (std::cin,address);
	std::cout<<"Enter Course: ";
	std::getline (std::cin,course);
	std::cout<<"Enter Gender: ";
	std::getline (std::cin,gender);
	
	std::cout<<"Do you want to register another Person? (Y/N) ";
	std::cin>>x;
                                                //Error ends here
	
	if (x=='Y'||x=='y'){
		continue;
	}else if (x=='N'||x=='n'){
		break;
	}
	
	}while (true);
	std::cout<<name<<endl;
	std::cout<<age<<endl;
	std::cout<<address<<endl;
	std::cout<<course<<endl;
	std::cout<<gender<<endl;
	x++;
{
	return 0;
}
}
Last edited on
george P i tested the code you gave me but there's a lot of error coming out. but i tested it out on this website it run smoothly. i am using a software called DEV C++ for my program coding.
Last edited on
what errors... got to tell us the exact messages.
While I may use Visual Studio as my main IDE, I just compiled the exact code I pasted earlier in Dev C++ with ZERO errors. And it runs the same.

As jonnin asked, what are the EXACT errors you are getting?

You changed the code without understanding what it is doing.

Retrieving an integer value using std::getline (it's for strings) just won't work. I retrieved Age as a string and then converted it to an int using std::stoi.

You do not need continue or break, nor the if/else if blocks. The condition to continue or finish is done in the while statement.

One thing to note is mixing std::cin and std::getline will cause input problems when you repeat your loop. std::getline removes ALL of the data in the input buffer, including the ending end-of-line terminator ('\n'). std::cin DOES NOT remove it, so when you indicate you want to enter another person the std::getline at the beginning of the next loop that reads the person's name will consume the terminator std::cin left behind. THAT is why I put std::cin::ignore after the final std::cin that retrieves the "do you want another" question.

And again, you have single use variables to store the data you get from the user. Each loop through that data is overwritten so no matter how many people get entered only the last person's data is stored. Asking if you want to enter another person's data is a bit of a waste.
Last edited on
it says:
error function does not declare parameters.
warning extended initializer lists only with -std=ctt0x or -std=gnu++0x [enable by default]
struct user has no member named Age
stai is not a member of std
struct user has no member named Gender
those are the error's that my DEV C++ showed me.
Without using operator>> overload for stream reading, then:

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
#include <iostream>
#include <string>
#include <vector>
#include <iterator>

struct Person {
	std::string name, address, course, gender;
	int age {};
};

int main() {
	char x {};
	std::vector<Person> people;

	do {
		Person per;

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

		std::cout << "Enter Age: ";
		std::cin >> per.age;
		std::cin.ignore();

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

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

		std::cout << "Enter Gender: ";
		std::getline(std::cin, per.gender);
		people.emplace_back(std::move(per));

		std::cout << "Do you want to register another Person? (Y/N) ";
		std::cin >> x;
		std::cin.ignore();

	} while (x == 'Y' || x == 'y');

	for (const auto& per : people)
		std::cout << per.name << ", " << per.age << ", " << per.address << ", " << per.course << ", " << per.gender << '\n';
}

Topic archived. No new replies allowed.