Please help with this code? Really need quick help

I am writing a quiz program and I am not doing with the file thing, I am not using a file for questions and answers. So this is the model, the prototype and the problem is that when I run it and even give all answers correct THE SCORE IS STILL DISPLAYED AS ZERO, please check it and find my mistake, Thanks.

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <time.h>
#include <algorithm>
int score;
using namespace std;

void main()
{
srand(time(NULL)); score=0;
int arr[3]; int m;
for (int i=0; i<3; ++i)
arr[i] = i + 1;
std::random_shuffle(arr, arr + 3);



char answer[15];

char solution[3][15]={"islamabad", "shahbaz", "black"};

for (int i=0; i<=4; i++)
{m=arr[i];


switch (m)
{
case 1:
cout<<"What is the capital of Pakistan?";
cin>>answer;
break;
case 2:
cout<<"What is your name";
cin>>answer;
break;
case 3:
cout<<"What is the color";
cin>>answer;
break;
}
if (answer==solution[m])
score=score+1;
}

cout<<endl<<score;
getch();


}
if (answer==solution[m])

THis is comparing a char pointer (the address of the first element in of the array answer) with another char pointer (the address of the pointer at location m in the array solution). Why would those two addresses be the same? Why are you comparing char pointers? Surely you want to compare the actual chars?

You have misunderstood what an array is, and what a pointer is. If you want to compare two char arrays, use the strcmp function. If you want to do this properly, don't use char arrays. This is C++. Use strings.
Last edited on
can you just gimme a sample on how to do this with strings?
1
2
3
4
string firstString("word");
string secondString("word");

if (firstString == secondString)

Thanks a lot, but I will have so many answers like 30, so do I have to make 30 strings for answers or is there any simpler and intelligent way to do this?
and thanks again
string answers[30]={"one","two","three".....}

While I'm here, this:
void main()
is not C++. It's not. In C++, main returns an int. Anything else is just plain wrong.
int main() is correct.
Last edited on
I am doing with char arrays, not strings! how to do it for arrays
Besides I will have to use a function to check the answers, a function for comparisons now with arrays it is okay, but how can I pass strings as arguments to functions
Last edited on
I am doing with char arrays, not strings! how to do it for arrays

Make up your mind; a minute ago you asked how to do it with strings. You can do it with char arrays. It's very silly because you're making it much harder for yourself, and C++ strings exist for exactly this sort of reason.

That said, I already told you; strcmp can be used to compare char arrays. strcmp is a function. You can use the internet to look up this function.

okay, but how can I pass strings as arguments to functions
If you don't know how to pass a parameter to a function, this is way beyond your ability and you should go back to the basics of "what is a function and how do I write one".
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on

 
if (answer==solution[m])

modify it to
 
if( strcmp(answer,solution[m]) == 0)
Okay thanks a lot man, you helped a lot. THANK YOU SO MUCH
Topic archived. No new replies allowed.