Code will not print out information?

Hello everyone, I am trying to print out the names thatI listed below in my code... Not sure what I am doing wrong? Could anyone help me out?

It should output like this:
First Name: Bill
Last Name: Smith
Phone number: Bill'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
49
50
    #include <iostream>
    #include <string>

    struct IDCard
    {
    std::string firstName;
    std::string lastName;
    std::string phoneNumber;
    };


    void PrintID(IDCard &idCard) {


    for(int i = 0; i < 3; i++) {

    int indexFirst = idCard.firstName[i];
    int indexLast = idCard.lastName[i];
    int indexNum = idCard.phoneNumber[i];


    std::cout << "First Name: " << indexFirst << std::endl; 
    std::cout << "Last Name: " << indexLast <<std::endl;
    std::cout << "Phone Number: " << indexNum <<std::endl;

    } 
    }

     int main () {

     IDCard testID[3];


    testID[0].firstName = "Bill";
    testID[0].lastName = "Smith";
    testID[0].phoneNumber = "111-232-1456";

    testID[1].firstName = "Sally";
    testID[1].lastName = "Smith";
    testID[1].phoneNumber = "111-256-1456";

    testID[2].firstName = "Billy Jr.";
    testID[2].lastName = "Smith";
    testID[2].phoneNumber = "111-348-1800";

    PrintID(testID[0]);

    return 0;
    }
Last edited on
You're only passing in the first element of the array to PrintID(), which then prints the first 3 characters of firstName, lastName and phoneNumber.

idCard.firstName is a string.
idCard.firstName[i]; will return the character at index i.
int indexFirst = idCard.firstName[i]; is implicitly converting a char to an int, which is allowed, but not what you want.

1
2
3
    int indexFirst = idCard.firstName[i];
    int indexLast = idCard.lastName[i];
    int indexNum = idCard.phoneNumber[i];

This is redundant.
Last edited on
closed account (LA48b7Xj)
I made a few corrections and changed the pass by reference to pass by const reference which is more proper if you don't want the function to modify the data of the argument.

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>

struct IDCard {
    std::string firstName;
    std::string lastName;
    std::string phoneNumber;
};

void PrintID(const IDCard& idCard)
{
    std::cout << "First Name: " << idCard.firstName << std::endl;
    std::cout << "Last Name: " << idCard.lastName << std::endl;
    std::cout << "Phone Number: " << idCard.phoneNumber << std::endl;
}

int main()
{
    IDCard testID[3];

    testID[0].firstName = "Bill";
    testID[0].lastName = "Smith";
    testID[0].phoneNumber = "111-232-1456";

    testID[1].firstName = "Sally";
    testID[1].lastName = "Smith";
    testID[1].phoneNumber = "111-256-1456";

    testID[2].firstName = "Billy Jr.";
    testID[2].lastName = "Smith";
    testID[2].phoneNumber = "111-348-1800";

    for(int i=0; i<3; ++i)
        PrintID(testID[i]);
}

Last edited on
Topic archived. No new replies allowed.