Results 1 to 2 of 2

Thread: C++ Question: Printing out information! Please Help

  1. #1
    Join Date
    Oct 2016
    Posts
    1
    Qt products
    Qt3
    Platforms
    MacOS X

    Default C++ Question: Printing out information! Please Help

    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

    Qt Code:
    1. #include <iostream>
    2. #include <string>
    3.  
    4. struct IDCard
    5. {
    6. std::string firstName;
    7. std::string lastName;
    8. std::string phoneNumber;
    9. };
    10.  
    11.  
    12. void PrintID(IDCard &idCard) {
    13.  
    14.  
    15. for(int i = -1; i < 2; i++) {
    16.  
    17. int indexFirst = idCard.firstName[i];
    18. int indexLast = idCard.lastName[i];
    19. int indexNum = idCard.phoneNumber[i];
    20.  
    21.  
    22. std::cout << "First Name: " << indexFirst << std::endl;
    23. std::cout << "Last Name: " << indexLast <<std::endl;
    24. std::cout << "Phone Number: " << indexNum <<std::endl;
    25.  
    26. }
    27. }
    28.  
    29. int main () {
    30.  
    31. IDCard testID[3];
    32.  
    33.  
    34. testID[0].firstName = "Bill";
    35. testID[0].lastName = "Smith";
    36. testID[0].phoneNumber = "111-232-1456";
    37.  
    38. testID[1].firstName = "Sally";
    39. testID[1].lastName = "Smith";
    40. testID[1].phoneNumber = "111-256-1456";
    41.  
    42. testID[2].firstName = "Billy Jr.";
    43. testID[2].lastName = "Smith";
    44. testID[2].phoneNumber = "111-348-1800";
    45.  
    46. PrintID(testID[0]);
    47.  
    48. return 0;
    49. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: C++ Question: Printing out information! Please Help

    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.

    Qt Code:
    1. void PrintID(IDCard &idCard) {
    2.  
    3. std::cout << "First Name: " << idCard.firstName << std::endl;
    4. std::cout << "Last Name: " << idCard.lastName <<std::endl;
    5. std::cout << "Phone Number: " << idCard.phoneNumber <<std::endl;
    6.  
    7. }
    8.  
    9. int main () {
    10.  
    11. IDCard testID[3];
    12.  
    13.  
    14. testID[0].firstName = "Bill";
    15. testID[0].lastName = "Smith";
    16. testID[0].phoneNumber = "111-232-1456";
    17.  
    18. testID[1].firstName = "Sally";
    19. testID[1].lastName = "Smith";
    20. testID[1].phoneNumber = "111-256-1456";
    21.  
    22. testID[2].firstName = "Billy Jr.";
    23. testID[2].lastName = "Smith";
    24. testID[2].phoneNumber = "111-348-1800";
    25.  
    26. int count = sizeof(testID) / sizeof(IDCard);
    27.  
    28. for (int i = 0; i < count; i++)
    29. {
    30. PrintID(testID[i]);
    31. }
    32.  
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 0
    Last Post: 1st November 2015, 10:17
  2. Replies: 0
    Last Post: 29th April 2013, 11:47
  3. Printing question
    By laszlo.gosztola in forum Qt Programming
    Replies: 0
    Last Post: 27th June 2011, 13:52
  4. Printing Question
    By rogerholmes in forum Newbie
    Replies: 4
    Last Post: 5th May 2009, 04:39
  5. QMessageBox::information box question
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2007, 17:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.