How to output one particular field in a form

Pages: 12
I need help in writing a program in c++ that will get the input of 50 different users for the following the fields (surname, other names, sex, status, date of birth) and after entering the data for these 50 users, it will then output only the list of all FEMALE users who were registered...
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string surname, other_names1, other_names2, status, sex, dob;
for (int i=1; i<=50; i++)
{
esc: cout<<"Profile for User << i <<endl;
cout<<"Registration Form"<<endl;
cout<<endl;
cout<<"Surname: ";
cin>>surname;
cout<<"Other Names: ";
cin>>other_names1;
cin>>other_names2;
cout<<"Sex: ";
cin>>sex;
cout<<"Marital Status: ";
cin>>status;
cout<<"Date of Birth: ";
cin>>dob;
goto esc;
}
... I don't really know what to do next - all my attempts have not really given me the result I want.

Perhaps I could use some help from a pro
Thanks!
Last edited on
Instead of having 6 dedicated variables like that (because in the end you'll only be able to store the information for the 50th person), you should create a user struct and then have an array of 50 of them. After that, you just loop through each user, check to see if the current one is female, and output her name.

If you don't know about structures or arrays, http://www.cplusplus.com/doc/tutorial/structures/ & http://www.cplusplus.com/doc/tutorial/arrays/

You need to get rid of that goto. You're using a loop so adding that goto is redundant and in this case causes an infinite loop. Also, it's generally not good practice to use goto in programs.
Last edited on
^ thanks, I get your point...
Please if you can, display some samples of code as regards the subject (program). I'm still finding it hard to crack...
Thanks!
There are lots of code samples in those tutorial pages. That should give you a good foothold in both structures and arrays.

Where exactly are you getting stuck?
Last edited on
"There are lots of code samples in those tutorial
pages. That should give you a good foothold in both
structures and arrays.
Where exactly are you getting stuck?"
How to output the surname and other names of ONLY female users
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//create a struct to hold an individual user's data

struct User{
string surname;
// other variable definitions
}

//create an array of structs
const int SIZE = 50 // or other number
User group[SIZE];

for (int i = 0, i < SIZE; ++i)
{
//get input for all users
std::cout << "Enter surname: ";
std::cin >> group[i].surname; 
//etc.
}

for (int i = 0, i < SIZE; ++i)
{
//if sex of current user is female
//output name
}
Last edited on
Pls I would like to upload some screen shot of the error messages in the program when compiling. How can I do that?
Can you just copy and paste your current code (use the <> button in the format palette at the right side of the post to format the code part) and the compiler error messages?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct User{
sting surname, firstname, sex, dob, dept;
};
const int SIZE = 3;
User group[SIZE];
int main()
{
for (int i=1; i<=SIZE; i++)
{
std::cout<<"Profile of Student "<< i;
std::cout<<endl;
std::cout<<"1. Surname: ";
std::cin>>group[i].surname;
std::cout<<"2. First Name: ";
std::cin>>group[i].firstname;
std::cout<<"3. Sex: ";
std::cin>>group[i].sex;
std::cout<<"4. Date of Birth: ";
std::cin>>group[i].dob;
std::cout<<"5. Department: ";
std::cin>>group[i].dept;
}
for (int i=1; I<=SIZE; i++)
{
if (group[i].sex == "female" && group[i].dept == "music")
{
cout<<group[i].surname<<" "<<group[i].firstname<<endl;
}
}
system("pause");
return 0;
}
--------------------------------------------
Please help me correct and modify
Thanks
Last edited on
The program should be able to list all the FEMALE users in Music department at the end of the loop (i.e after completing the 3 loops - SIZE)
Compiler errors are coming from typos in these lines...

sting should be string
sting surname, firstname, sex, dob, dept;

uppercase I in middle condition should be lower case
for (int i=1; I<=SIZE; i++)
Also - array elements are numbered 0 to size - 1, so a 3 element array will have valid indices 0,1 and 2. For loop should start at 0 and be less than SIZE.

for (int i=0; i<SIZE; i++)

edit:
and you can offset the i value by 1 in this line so to the user it appears to start at student 1.
std::cout<<"Profile of Student "<< i+1;
Last edited on
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct User{
string surname, firstname, sex, dob, dept;
};
const int SIZE = 3;
User group[SIZE];
int main()
{
for (int i=0; i<SIZE; i++)
{
std::cout<<"Profile of Student "<< i+1;
std::cout<<endl;
std::cout<<"1. Surname: ";
std::cin>>group[i].surname;
std::cout<<"2. First Name: ";
std::cin>>group[i].firstname;
std::cout<<"3. Sex: ";
std::cin>>group[i].sex;
std::cout<<"4. Date of Birth: ";
std::cin>>group[i].dob;
std::cout<<"5. Department: ";
std::cin>>group[i].dept;
}
for (int i=0; i<SIZE; i++)
{
if (group[i].sex == "female" && group[i].dept == "music")
{
cout<<group[i].surname<<" "<<group[i].firstname<<endl;
}
}
system("pause");
return 0;
}
--------------------------------------------
Still, after correcting the semantic errors, I have not got the output I want.
The program should be able to output the list of all female user(s) in music department at the end of the loop but am not getting that.
Please somebody help me crack this nut!
Run the program and you will understand what I'm talking about... Error free but not my desired output
Last edited on
What data are you putting in? So far it's been working for me.

Profile of Student 1
1. Surname: Adams
2. First Name: Alice
3. Sex: female
4. Date of Birth: 05052005
5. Department: music
Profile of Student 2
1. Surname: Boop
2. First Name: Betty
3. Sex: female
4. Date of Birth: 06062006
5. Department: music
Profile of Student 3
1. Surname: Charles
2. First Name: Colleen
3. Sex: female
4. Date of Birth: 07071997
5. Department: music
Adams Alice
Boop Betty
Charles Colleen
Profile of Student 1
1. Surname: Dolan
2. First Name: Donny
3. Sex: male
4. Date of Birth: 04042004
5. Department: chemistry
Profile of Student 2
1. Surname: Edmunds
2. First Name: Eloise
3. Sex: female
4. Date of Birth: 08082008
5. Department: music
Profile of Student 3
1. Surname: Finney
2. First Name: Frank
3. Sex: male
4. Date of Birth: 09092009
5. Department: english
Edmunds Eloise
Last edited on
@wildblue, tanx... We got it!
But there's a little problem; if the string of the value you enter as input is a compound word (english language) it'd make the next input to be empty. How do I resolve it?
This is what I mean:
1. Surname: Tony
2. First Name: Peace
3. Sex: female
4. Date of Birth: 05301990
5. Department: English Language
Profile of Student 2
1. Surname: 2. First Name: _
You can use getline instead of the cin statements - cin just reads in until the blank space.

getline(std::cin, group[i].dept);
But it's like "getline" cannot be used with to get the input of an integer variable.
It's giving me this error: no matching function for call to 'getline(std::istream&, int&)'
Where are you using it on an integer? All the fields in your User structure were strings.
I added a new member of integer type to the struct but I've changed it to a string type and it's working fine. Thanks.
Now, please how can I convert the output of group[i].surname and group[i].firstname from lower case to upper case?
Pages: 12