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 6881335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:05:41+00:00 2026-05-27T05:05:41+00:00

i’m just beignning to learn about structs and seperating things into different files. At

  • 0

i’m just beignning to learn about structs and seperating things into different files.

At the moment i have a Main.cpp file like so:

#include <iostream>
    #include "StudentAnswerSheet.hpp"
using std::cout;
using std::endl;

int main(){

    StudentAnswerSheet sheet = {
        "Sally",
        {'a', 'b', 'a', 'd', 'c'}
    };

    cout << sheet.studentName << ":" <<endl;
    for(int i = 0; i <5; i++){
    cout << sheet.studentAnswers[i] << " " ;
    }
    return 0;
}

and a separate header file that contains my struct StudentAnswerSheet data type:

#include <string>
using std::string;

struct StudentAnswerSheet{
    string studentName;
    char studentAnswers[5];
};

Ideally i’d like to be able to have an array of UP TO 5 chars to hold student answers. I think i might need to change from char to char* to get a degree of flexibility but when i tried implementing it i got an error message “too many intialisers for char [0]” and wasn’t sure how to change the sheet intialization.

I’m also not sure what the best way to go about keeping track of how many elements my array contains if i switch to an array of char*.. if i take in the student answers with cin then i can keep track of the number of answers up to 5 but if i just wanted to initialize the answers myself like i am at the moment for testing im not sure what the most efficent way to calculate the size of studentAnswers would be, so any advice on that would be much appreciate too.

Thanks for any help!

  • 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-05-27T05:05:41+00:00Added an answer on May 27, 2026 at 5:05 am

    Since it seems you’re allowed to use std::string, then why dont you use std::vector<char> instead of using char[5] or thinking of using char* for flexibility? In your case, you can simply use std::string and then interpret the each character in it as student answer.

    Also, since StudentAnswerSheet is not POD, which means the following would give compilation error, unless you use C++11:

    //error in C++98, and C++03; ok in C++11
    StudentAnswerSheet sheet = {
        "Sally",
        {'a', 'b', 'a', 'd', 'c'}
    };
    

    Here is what I would do:

    struct StudentAnswerSheet
    {
        std::string studentName;
        std::string studentAnswers;
    
        //constructor helps easy-initialization of object!
        StudentAnswerSheet(const std::string & name, const std::string & answers) 
                    : studentName(name), studentAnswers(answers) {}
                  //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    };            // it is called member-initialization list
    

    then use it as:

    StudentAnswerSheet sheet("Sally", "abadc");//easy: thanks to the ctor!
    
    std::cout << sheet.studentName << std::endl;
    for(size_t i = 0; i < sheet.studentAnswers.size(); ++i)
    {
         std::cout << sheet.studentAnswers[i] << " " ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I am currently running into a problem where an element is coming back from

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.