im making a random question game. what im having trouble with is figuring out the correct way to make it check the answers

this is what i have.

for(int r=0; r<10; r++)
	{	
		int theDice = ( rand() % 10) +1;
		answer[theDice];
		cout<<question[theDice]<<endl;
		cout<<"Response: ";
		cin>>inquestion;

		if (strcmp(inquestion, answer[theDice]) == 0 )
		cout<<"correct"<<endl;
		else
			cout <<"WRONG"<<endl;
		
	}

i want it to compare what teh user inputs to what ever the roll gives. my though process? if a=a then its right but apparently c++ doesn't work that way :/. is there another way to approach it that i havent thought of or missed? or should i just go do every answer by hand like
if (answer=="Who is alex?")
cout<<"GOOD JOB";

thanks for the help

Recommended Answers

All 7 Replies

You are on the right track. How did you declare those arrays ? And what is inquestion?

i declared those as string. NUM is a constant int = 10.
answer[NUM],
question[NUM]

inquestion is the "answer" the user inputs to


edit;
if it helps this is the error

Error 2 error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string [10]' to 'const char *'

if question is a string array and inquestion is a string, then just use the == operator to compare them

std::string answer[NUM];
std::string inquestion;
<snip>
if (inquestion == answer[theDice])

the problem is already tried that. it compiles and builds. works? not exactly which is why i tried strcmp

this is what comes out

This cartoon is well known for the high use of profanity.
Response: what is south park?
WRONG
This cartoon is well known for the high use of profanity.
Response: WRONG
This Mel Brooks film was the first movie to have people fart on film.
Response: WRONG
This man is the only president to become a Supreme Court Justice after his Term.

Response: WRONG
This man is the only president to become a Supreme Court Justice after his Term.

Response: and so forth

im gussing maybe theres something up with the for loop i cant see. is that it?

What you have posted doesn't tell me a damed thing because all you posted was code snippet. Since I can't read your mind and I can't see your monitor from where I am sitting I guess your next best option is to post all the code.

Also remember that string comparisons are case-sensitive -- that is "World" is not the same as "WORLD". So if you mistype the case of the characters they they will not match. Best thing to do is convert everything to all upper case or lower case then do the comparisons.

sryy i know everything is a mess :/
all the answers are in lower case. i dont think the questions need to be

#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;

const int NUM=10;
int main()
{
	ifstream questionfile("QUESTIONS.txt");
	ifstream answerfile("ANSWERS.txt");

	string answer[NUM], question[NUM], inquestion;
	string input;

	srand( time(NULL));

	//----------get information--------------//
	for (int m=0; m<NUM; m++) 
	{ 
		getline(questionfile, question[m]);
		getline(answerfile, answer[m]);
	}
		questionfile.close ();
		answerfile.close ();

	
	
	for(int r=0; r<10; r++)
	{	
		int theDice = ( rand() % 10) +1;
		answer[theDice];
                cout<<question[theDice]<<endl;
		cout<<"Response: ";
		cin>>inquestion;
		
		if (inquestion == answer[theDice])
			cout<<"correct"<<endl;
		else
			cout <<"WRONG"<<endl;

	}



	return 0;
}

i already tried the tolower also

if (tolower(inquestion) == answer[theDice])

and it gives me an error

Error 2 error C2664: 'tolower' : cannot convert parameter 1 from 'std::string' to 'int'

When working with strings you have to convert the entire string, not just a single character. One way to do it is to use the transform() function

#include <algorithm>
#include <string>
...
<snip>
...
std::string inquestion = "Hello World";
// convert to all lower-case
std::transform(inquestion.begin(), inquestion.end(), inquestion.begin(),  tolower);

You will want to also use transform() on each line of the file that is read.

getline(answerfile, answer[m]);
std::transform(answer[m].begin(), answer[m].end(), answer[m].begin(),  tolower);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.