PDA

View Full Version : C++ Question: Printing out information! Please Help



youngcapitan
14th October 2016, 01:48
I am simply trying to print out the names lasted below but cannot seem to do so.
Please help, it should print out 3 times:

First name: Bill
Last name: Smith
Phone #: 123-123-4123
First name: billy
last name: smith
phone #: 123-421-2131
etc



#include <iostream>
#include <string>

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


void PrintID(IDCard &idCard) {


for(int i = -1; i < 2; 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;
}

jefftee
14th October 2016, 03:40
You're not passing the entire IDCard array, only the first IDCard since you're passing testID[0] to your PrintID function, so you don't have access to all three IDCard's you created in main. No idea what you're trying to do with the index -1 to < 2 in your PrintID function and your 3 integer index fields.



void PrintID(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";

int count = sizeof(testID) / sizeof(IDCard);

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

return 0;
}