It looks like you are using an ad blocker. That's okay. Who doesn't? But without advertising revenue, we can't keep making this site awesome. Click the link below for instructions on disabling adblock.
Welcome to the Newschoolers forums! You may read the forums as a guest, however you must be a registered member to post.
Register to become a member today!
yes i know this is newschoolers but i know some of you MF are good at coding and im stuck as hell.and all the other compsci forms arent much help with this.
So im making a drinking game for a final project that asks players questions and then checks if the input is the answer to said problem,
I have the questions and answers saved in a 2D array of size 20x2, mathProbs[j]; i being questions j being the answers.
how do I output only questions and then check if the input matches the answer stored? I can output both but I cant get it to output only one without an error, I also need it to step one down the list (array)after each answer.
here is what i got thus far.
C++ language
//*****************************************************
string FileIOthing(){
int prob=0;
int answer=0;
string ans;
NS cut my code inhalf so idk wtf happend here it is again.
//*****************************************************
string FileIOthing(){
int prob=0;
int answer=0;
string ans;
IliveinutahNS cut my code inhalf so idk wtf happend here it is again.
//*****************************************************
string FileIOthing(){
int prob=0;
int answer=0;
string ans;
One thing I see that may be an issue is your if statement checking if ans is equal to mathprobs[1][0]... You are using a single = which is usually an assignment operator, not an equality check (unless you wrote an operator overload). So in your if statement, mathprobs value is being assigned to ans, probably not what your going for.
I don't know what data type the array mathprobs is, (std::string, char*?) Maybe you should look into a standard string compare function, or write your own... I believe it outputs a 1 if the strings are the same.
I guess another question I have is about your design choice on why your using a 2d array...those things get really confusing (to me at least) Things might be a lot easier if you just had 2 arrays. One for mathquestions[], the other mathanswers[]? That way you could step through the questions and answers simotaneously with 1 index, in a for loop.. it might also clear up some of your issues displaying just the question, or just the answer.
I actually have written a similar program, but just in c and about math problems, but I think the algorithm is very similar, and the syntax in c is very close to c++ so If you are still stuck, feel free to send me a message, I might be able to send you code snippets and try to help you out.
there's always a lot of ways to approach a problem and sometimes what makes the most sense to you, regardless of how unorthodox it is, can be best so design decisions aside just answering your questions:
"How do I output only questions?"
You're pretty much already doing it, instead of cout
haha NS doesn't seem to like the input and output operators > > so its cutting out my response :|
edit 2: also doesn't like 2 dimensional arrays
there's always a lot of ways to approach a problem and sometimes what makes the most sense to you, regardless of how unorthodox it is, can be best so design decisions aside just answering your questions:
"How do I output only questions?"
You're pretty much already doing it, instead of cout MathProbs[0][1] write another for loop and cout MathProbs[ i][1] so it'll iterate through your 20 questions but not through the answers/questions array.
"How do I check if the input matches the answer stored?"
Once again a few ways to do this, the first thing that comes to mind is the same as what the other guy said, using the string library use the string compare function (google for docs and examples) probably something like:
although you might even just be able to directly compare strings in c++ 11 maybe? which would be even simplier ex. if (string1 == string2) not sure though
besides what the other guy said about using = instead of == I also noticed on your single question you print MathProbs[0][1] as the question (so question 0, assuming the 1 is the question element on the 2nd dimension of the array) but then you compare ans to MathProbs[1 ][0] which would be the answer element of question 1. Just from what I can see you would be wanting to use if (ans == MathProbs[0 ][0]).
and then step through it all like i said earlier for loop the whole process:
for (int i = 0; i < 20; i++) {
//print question
cout < < MathProbs[ i][1] < < endl;
//receive input
cin > > ans;
//compare ans to stored answer
if (ans.compare(MathProbs[ i][0]) == 0) {
cout < < "correct" < < endl;
else {
cout < < "wrong" < < endl;
}
hopefully some of that helps/ isnt wrong
**This post was edited on Dec 2nd 2017 at 6:23:46pm
**This post was edited on Dec 2nd 2017 at 6:25:36pm
I'm not too great at C++ so I can't really help unfortunately, but I would suggest that you drop your code/examples in Pastebin and then link it here as needed.
LJhaha NS doesn't seem to like the input and output operators > > so its cutting out my response :|
edit 2: also doesn't like 2 dimensional arrays
there's always a lot of ways to approach a problem and sometimes what makes the most sense to you, regardless of how unorthodox it is, can be best so design decisions aside just answering your questions:
"How do I output only questions?"
You're pretty much already doing it, instead of cout MathProbs[0][1] write another for loop and cout MathProbs[ i][1] so it'll iterate through your 20 questions but not through the answers/questions array.
"How do I check if the input matches the answer stored?"
Once again a few ways to do this, the first thing that comes to mind is the same as what the other guy said, using the string library use the string compare function (google for docs and examples) probably something like:
although you might even just be able to directly compare strings in c++ 11 maybe? which would be even simplier ex. if (string1 == string2) not sure though
besides what the other guy said about using = instead of == I also noticed on your single question you print MathProbs[0][1] as the question (so question 0, assuming the 1 is the question element on the 2nd dimension of the array) but then you compare ans to MathProbs[1 ][0] which would be the answer element of question 1. Just from what I can see you would be wanting to use if (ans == MathProbs[0 ][0]).
and then step through it all like i said earlier for loop the whole process:
for (int i = 0; i < 20; i++) {
//print question
cout < < MathProbs[ i][1] < < endl;
//receive input
cin > > ans;
//compare ans to stored answer
if (ans.compare(MathProbs[ i][0]) == 0) {
cout < < "correct" < < endl;
else {
cout < < "wrong" < < endl;
}
hopefully some of that helps/ isnt wrong
**This post was edited on Dec 2nd 2017 at 6:23:46pm
**This post was edited on Dec 2nd 2017 at 6:25:36pm
TIL LJ likes to get down with the comp sci when off the hill
LJhaha NS doesn't seem to like the input and output operators > > so its cutting out my response :|
edit 2: also doesn't like 2 dimensional arrays
there's always a lot of ways to approach a problem and sometimes what makes the most sense to you, regardless of how unorthodox it is, can be best so design decisions aside just answering your questions:
"How do I output only questions?"
You're pretty much already doing it, instead of cout MathProbs[0][1] write another for loop and cout MathProbs[ i][1] so it'll iterate through your 20 questions but not through the answers/questions array.
"How do I check if the input matches the answer stored?"
Once again a few ways to do this, the first thing that comes to mind is the same as what the other guy said, using the string library use the string compare function (google for docs and examples) probably something like:
although you might even just be able to directly compare strings in c++ 11 maybe? which would be even simplier ex. if (string1 == string2) not sure though
besides what the other guy said about using = instead of == I also noticed on your single question you print MathProbs[0][1] as the question (so question 0, assuming the 1 is the question element on the 2nd dimension of the array) but then you compare ans to MathProbs[1 ][0] which would be the answer element of question 1. Just from what I can see you would be wanting to use if (ans == MathProbs[0 ][0]).
and then step through it all like i said earlier for loop the whole process:
for (int i = 0; i < 20; i++) {
//print question
cout < < MathProbs[ i][1] < < endl;
//receive input
cin > > ans;
//compare ans to stored answer
if (ans.compare(MathProbs[ i][0]) == 0) {
cout < < "correct" < < endl;
else {
cout < < "wrong" < < endl;
}
hopefully some of that helps/ isnt wrong
**This post was edited on Dec 2nd 2017 at 6:23:46pm
**This post was edited on Dec 2nd 2017 at 6:25:36pm
my thinking when it came to how 2D arrays where numberd was my issue the whole time, you nailed it in your explination. I ended fixing it because of your help and now my drinking game is up and running, and i belive i got a half decent grade.