how to??

Hi. im having a problem with 2 dimensional array.
im assigned to read the input of 45 names with corresponding gender, then display then and the number of males and females.
so far i got:
#include<iostream>


using namespace std;
int main()
{
char sname[45][4],row;
int male=0,female=0;
for (row=0;row<45;row++)
{
cout<<"\nEnter Last name: ";
cin>>sname[row][1];
cout<<"Enter First name: ";
cin>>sname[row][2];
cout<<"Enter mi: ";
cin>>sname[row][3];
cout<<"Enter gender: ";
cin>>sname[row][4];
do{
if (sname[row][4]=='m'||sname[row][4]=='M')
male++;
else if (sname[row][4]=='f'||sname[row][4]=='F')
female++;
else
cout<<"Invalid gende. Enter again.";
}while (sname[row][4]=='m'||sname[row][4]=='M'||sname[row][4]=='f'||sname[row][4]=='F');
}
row=0;
for (row=0;row<45;row++)
{
cout<<sname[row][1]<<", "<<sname[row][2]<<" "<<sname[row][3]<<" \n";
}
return 0;
}

but when i type more than 1 character the program passes the name codes as the number of character i typed...

pls help me Y_Y

sorry for the trouble DX



closed account (z05DSL3A)
char sname[45][4] is an array of chars, cin>>sname[row][1]; can only put a single char into the array.

Do you know about std::string and struct yet?

This should get you going again:
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
#include <iostream>
#include <string>  

using namespace std;

int main()
{
    string sname[45][4];    //changed from a array of char to an array of strings
    int row=0, male=0,female=0; //row should be an int not a char
    for (row=0;row<45;row++)
    {
        cout<<"\nEnter Last name: ";
        cin>>sname[row][1];
        cout<<"Enter First name: ";
        cin>>sname[row][2];
        cout<<"Enter mi: ";
        cin>>sname[row][3];
        cout<<"Enter gender: ";
        cin>>sname[row][4];
        do{
            if (sname[row][4]=="m"||sname[row][4]=="M")  // changed character literals('') to string literals ("") 
                male++;
            else if (sname[row][4]=="f"||sname[row][4]=="F")
                female++;
            else
                cout<<"Invalid gende. Enter again.";
        }while (sname[row][4]=="m"||sname[row][4]=="M"||sname[row][4]=="f"||sname[row][4]=="F");
    }
    row=0;
    for (row=0;row<45;row++)
    {
        cout<<sname[row][1]<<", "<<sname[row][2]<<" "<<sname[row][3]<<" \n";
    }
    return 0;
}

Last edited on
we didn't tackle strings. so im getting a hard time doing this ><.

thx for the help. but why does the program hung up after i typed in the gender??


closed account (z05DSL3A)
It looks like you need to put the cin>>sname[row][4]; inside the do...while loop, as it is the user does not get to enter a different gender if it is incorrect. Plus the logic is slightly wrong, you only want it to stay in the loop if those conditions are not true. e.g.
}while (!(sname[row][4]=="m"||sname[row][4]=="M"||sname[row][4]=="f"||sname[row][4]=="F"));

or do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        bool invalid(false);
        do{
            invalid = false;
            cin>>sname[row][4];
            if (sname[row][4]=="m"||sname[row][4]=="M")   
                male++;
            else if (sname[row][4]=="f"||sname[row][4]=="F")
                female++;
            else
            {
                cout<<"Invalid gende. Enter again.";
                invalid = true;
            }
        }while (invalid);
woud try the first one since i don't know about bool.

i would like to know more about that bool thing...
(suck our professor doesn't teach that to us)

thx again
closed account (z05DSL3A)
If you haven't already looked, take some time to go through this sites tutorials:
http://www.cplusplus.com/doc/tutorial/
ill try that

thx again~
can you please help me with this...

i forgot that the deadline of this is tommorow.

sorry for the trouble
closed account (z05DSL3A)
Are you still trying to do the array?

if you wanted a single name you would do somthing like:
char yourname [20];

if you want first name, second name, you would do something like:
char yourname [2][20];

if you want space for 45 copies of first name, second name, you would do something like:
char names [45][2][20];

1
2
3
4
        cout<<"\nEnter Last name: ";
        cin>>sname[row][0][0];
        cout<<"Enter First name: ";
        cin>>sname[row][1][0];

Last edited on
Haha i just solved it now thx for the help~

Topic archived. No new replies allowed.