How to define char sizes in C++ in an array? -


recently, have been working on console project in which, when prompted to, user input 10 questions (individually) , 10 answers (individually) proceed basis of ui study guide.

currently, here code (snippet only):

#include<iostream> #include<cstdlib> #include<string> #include<cstdio>   using namespace std;  int main() //the use of endl; in consecutive use visual effects  {            char z;     char a[500], b[500], c[500], d[500], e[500], f[500], g[500], h[500], i[500], j[500]; //questions     char a1[1000], b1[1000], c1[1000], d1[1000], e1[1000], f1[1000], g1[1000], h1[1000], i1[1000], j1[1000]; //answers      cout << "hello , welcome multi use study guide!" << endl;     cout << "you prompted enter questions , after, answers." << endl << endl;     system("pause");     system("cls");      cout << "first question: ";     cin.getline(a,sizeof(a));     cout << endl;        system("pause");     system("cls"); 

in snippet, defining multiple char variables , assigning them size, retrieving user input put specified variable.

my question being, how define size of single char variable in array instead of using multiple variables in 1 line?

you can declare array of char arrays:

#define nquestions 10 char question[nquestions][500]; char answer[nquestions][1000]; 

you can use loop enter questions , answers.

for (int = 0; < nquestions; i++) {     cout << "question #" << i+1 << ":";     cin.getline(question[i], sizeof(question[i])); } 

but c++ way these things std::vector containing std::string.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -