Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:48:56+00:00 2026-06-18T06:48:56+00:00

I have a structured a data type called bookStruct and books is the name

  • 0

I have a structured a data type called bookStruct and books is the name of the variable associated with the bookStruct data type. book[10] is the array that is 10 characters long and has 4 characters of data, meaning book[0] to book [3] have datas in them when the rest are empty (o values). Now I want to print the datas that are availabe already in the array and not print the ones that are empty otherwise 0. I tried the below code, with no luck.What am I doing wrong here?

for (int i=0;i<MAX_BOOKS && books[i]!='\0';i++)
            {
                cout << "Book Title: " << books[i].bookTitle << endl;
                cout << "Total Pages: " << books[i].bookPageN << endl;
                cout << "Book Review: " << books[i].bookReview << endl;
                cout << "Book Price: " << books[i].bookPrice<< "\n\n" << endl;
            }

here is the declaration for book struct

struct bookStruct
{
    string bookTitle;
    int bookPageN;
    int bookReview;
    float bookPrice;
};

bookStruct books[10];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:48:57+00:00Added an answer on June 18, 2026 at 6:48 am

    It’s a little hard to tell what’s being asked here. You see, it’s quite easy to just keep count of how many books you have stored:

    int numBooks = 0;
    

    When you add a book, you increment numBooks up until MAX_BOOKS.

    for( int i=0; i<numBooks; i++ ) ...
    

    If you don’t want to do that, you certainly can’t test books[i] != '\0' because that is testing the struct against a single character.

    Instead, you might want to test books[i].bookTitle.size() != 0

    (or indeed !books[i].bookTitle.empty()).

    for( int i=0; i<MAX_BOOKS && !books[i].bookTitle.empty(); i++ ) ...
    

    Another alternative is to store books in a vector instead of an array, so you don’t have to worry about maximum counts and current counts. The vector can shrink and grow for you.

    vector<bookStruct> books;
    
    ...
    
    for( int i = 0; i < books.size(); i++ ) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data file called accounts.dat that is structured like below: 1000:Cash 3000:Common
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have text files containing structured data (it is a proprietary format and not
I have a restful webservice which receives some structured data which is put straight
I have a dataset of securities prices in a database. The data is structured
I'm a bit confused on this. I have a data table structured like this:
I have this array {$man_data} which is structured like 10 > 'Text 8' 14
This semester in university I have a class called Data Structures, and the professor
I have a class called SparseMatrix which contain a private vector of type Cell.
I have a structure defined in a header file called data.h. I am including

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.