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?
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):
#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
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.
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.
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.