Click to See Complete Forum and Search --> : Booleans


Shodan
06-14-2004, 06:15 PM
Im very new to programming in C++. I was wondering if there is a way to create a kind of Boolean Array that would relate to a character array, so in terms of a question and answer program, if the user was to get a question wrong a boolean flag would go up on that question number and so the person can go back to the question they got wrong.

Here is the program so far


#include<iostream.h>
#include<conio.h>
int Validate(void);
char YorN(void);
void main(void)
{
char Question [4][120] = {
{"\nQUESTION 1\n\n\n\t 123 - 39 = \n\n\t\t1\t64\n\n\t\t2\t44\n\n\t\t3\t74\n\n\t\t4\t84\n\n\n"},
{"\nQUESTION 2\n\n\n\t 123 + 39 = \n\n\t\t1\t162\n\n\t\t2\t166\n\n\t\t3\t62\n\n\t\t4\t66\n\n\n"},
{"\nQUESTION 3\n\n\n\t 123 * 9 = \n\n\t\t1\t1007\n\n\t\t2\t1107\n\n\t\t3\t1106\n\n\t\t4\t1116\n\n\n"}
};
char Answers[4] = {'4','1','2'};
char Answer[4];
bool Correct;

cout<<"\n\n Welcome to Total Objective Multi-Surveys (TOMS) prototype on-line test";
cout<<"\n\n\n\n\t\tPress ANY key to continue";
cin.get();
clrscr();
for(int loop=0;loop<3;loop++)
{
cout<<Question[loop];
cout<<"Please Enter Answer : ";
Answer[loop]=Validate();
if(Answer[loop]==Answers[loop])
{
Correct=true;
}
clrscr();
}

cout<<"Answer 1 "<<Answer[0]<<" Answer 2 "<<Answer[1]<<" Answer 3 "<<Answer[2];
cin.get();
}
int Validate(void)
{
bool valid=false;
char ch;
do{
ch=getch();
if ((ch>='1')&&(ch<='4'))
{
putchar(ch);
valid=true;
}


}while(valid==false);
return(ch);
}


The problem im having should be placed around line 30 as you can see where I put the "Correct=true" line.

Hope you can help me out, the only problem is I need to solve this in 3 days so the 18 June at the latest. I hope you can help because im stumped.

Many Thanks

James

Danny
06-15-2004, 01:47 AM
I'm not sure if this is what you're looking for but how about defining a simple struct that contains a bool flag and the question's number:

struct Q
{
bool correct;
int num;
};

Then, define an array of such structs and fill it with the right values for each question:

Q qs[100];

if qs[0].correct=false
//display to user qs[0].num;

Shodan
06-15-2004, 12:33 PM
I already have a way of storing the correct answers but I need a way to compare the answer the user has given with that of the correct one, therefore, if they are different then something must mark it like a bool that would turn false. such as:

if (UserAnswer[]==CorrectAnswer[])
{
bool Wrong=true
}

This would be easy if I could create a Boolean array because then
char Question[1] would coincide with bool Wrong[1], if you see what I'm saying. This way the user can go back to only those questions he/she got wrong i.e. the ones that are flagged.

Bear in mind, I'm very new to programming, so explinations must be simple, lol.

emanresu
06-15-2004, 01:28 PM
I am not very clear about what you are asking, but try this:


bool gotRight[20];
...

for (int i = 0; i < 20; i++) {
gotRight[i] = false;
}

...
...


if(Answer[loop]==Answers[loop])
{
gotRight[loop] = true;
}
...



then after the user is done, you just go through the gotRight[] array and re-display all questions that are not correct.

hope this helps.

Shodan
06-16-2004, 04:14 PM
Cheers emanresu,

I didn't know you could make a boolean array because when I tried it it kept throwing up errors, but for some reason I did it your way and it went without any problems. This has solved my problem perfectly, many thanks.

James

emanresu
06-16-2004, 09:43 PM
you are quite welcome, Shodan. And yes, you can make an array of virtually anything. C++ is beautiful, isn't it? :) And I am glad it solved your problem