-1

I have made one program:

char a1[100]="Ques 1" , a2[100]="Ques 2" , a3[100]="Ques 2";
int count=1;
while (count<=3)
{
  cout << ....;
}

Now I want to display the questions one after another. So what do I type in the place of ....? like

cout << a(count);

So that the questions are displayed in sequence.

Thanks in advance

RK Tilak
  • 11
  • 1
  • 3
  • What objection do you have to `std::string`? – Bathsheba Aug 25 '17 at 14:15
  • I am a beginner, and I don't know the function. Please give me the syntax. – RK Tilak Aug 25 '17 at 14:16
  • `for (const auto &q : {a1, a2, a3}) { std::cout << q << '\n'; }`. This question should have been answered by a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282). – nwp Aug 25 '17 at 14:16
  • better yet, `a1`, `a2`, `a3` should have been a single array/container/collection to start with (in basically any programming language, not just C++). Definately spend some more time with programming books. They should introduce all these important things. – Fire Lancer Aug 25 '17 at 14:19
  • More appropriate would be to use an array of strings for your questions rather than discrete character arrays, `a1`, `a2`, `a3`, etc. See [this article](http://www.geeksforgeeks.org/array-strings-c-3-different-ways-create/) for different approaches, but approach #3 is the most "C++ way". – lurker Aug 25 '17 at 14:19
  • @lurker I cant recall ever seeing `: {a1, a2, a3}` style used in any actual code. Id bet I could search my companies entire code base (of post C++11 projects) and not find that. – Fire Lancer Aug 25 '17 at 14:21
  • @FireLancer you totally misunderstood my comment. I was saying that the OP doesn't need to define `a1`, `a2`, `a3`, etc at all, but use an array or container of strings instead. Maybe you were referring to nwp's comment? – lurker Aug 25 '17 at 14:45
  • Yes, I read "#3" very differently :) – Fire Lancer Aug 25 '17 at 14:47

2 Answers2

3

The fact that you've used a different variable for each question makes the output stage hard to organise.

Why not use an array of std::string?

std::string questions[] = {"Quesstion one", "Question two", "Question three"};

and output using

for (auto& question : questions){
    std::cout << question;
}

This makes use of the innovations in C++11.

Finally, in order to read a text file into a std::vector<std::string>, see Reading line from text file and putting the strings into a vector?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • but if I need to type big questions, it would be difficult. Also I want to write 20-30 questions. – RK Tilak Aug 25 '17 at 14:19
  • Well yes your hands might hurt from all the typing. Eventually you'll read in the `std::string` array from a file. – Bathsheba Aug 25 '17 at 14:19
  • how can I make use of a file in std::string? – RK Tilak Aug 25 '17 at 14:21
  • See this https://stackoverflow.com/questions/8365013/reading-line-from-text-file-and-putting-the-strings-into-a-vector – Bathsheba Aug 25 '17 at 14:22
  • 2
    "File" being a `std::fstream`. for lines you want `std::getline`. I *would not* recommend trying to learn basic programming concepts via Q&A, you **will** miss important things needed. This is why books and courses exist. – Fire Lancer Aug 25 '17 at 14:23
  • 3
    @RKTilak: Facetiously, Turbo C++ is not a C++ compiler. It's a relic from the 1990s the usage of which will only damage your market value. Think of it costing you $100 per hour every time you sully your experience wrestling with it. Spend your weekend upgrading to a free one. gcc is excellent although sadly I no longer use it. – Bathsheba Aug 25 '17 at 14:23
  • 2
    Yes, forget about Turbo C++ its ancient and not supported (not even sure about its "successor" C++ Builder). If your on windows get Visual Studio (free for personal and small businesses) https://www.visualstudio.com/vs/community/. If you on Linux or Mac you have GCC and a number of free IDE's to choose from. – Fire Lancer Aug 25 '17 at 14:25
0

If you must use character arrays, you'll need an array of character arrays.

const size_t MAX_QUESTION_LENGTH = 100;
const size_t MAX_QUESTIONS = 5;

char question_texts[MAX_QUESTIONS][MAX_QUESTION_LENGTH] =
{
  "Question 1",
  "Question 2",
  //...
  "Question 5",
};

int main()
{
  for (size_t i = 0; i < MAX_QUESTIONS; ++i)
  {
    std::cout << "\n"
              << question_texts[i]
              << "\n";
  }
  return 0;
}

Another alternative is to use a vector of string:

std::vector<std::string> question_database;
//...
question_database.push_back("Question 1");
//...
for (i = 0; i < question_database.size(); ++i)
{
  std::cout << "\n"
            << question_database[i]
            << "\n";
}

Arrays must have their capacities specified at compile time.
Strings and vectors grow dynamically during runtime.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154