C++ Exam Grader [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Alright, I've been able to simply the code. My current issue is dealing with the output. Looping structures to include 5 students but have the number elements reset like the percentages and answeredCorrectly. Also at the end of questions missed portion, I have to add in what the correct answer is. Our professor gave us 2 functions in order to accomplish this, I just do not understand how to implement them correctly. That code is not posted, but if anyone is interested in helping solve that problem, Ill be happy to post it.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdio>
using namespace std;
int main()
{
int z = 0;
const int WRONGQUESTIONS = 20;
int wrongCounter[WRONGQUESTIONS];
const int QUESTIONS = 20;
const int STUDENT_QUESTIONS = 100;
ifstream inputFile;
inputFile.open("CorrectAnswers.txt");
char correctAnswers[QUESTIONS];
for (int i=0; i<20; i++)
{
inputFile >> correctAnswers[i];
}
ifstream inputFile2;
inputFile2.open("StudentAnswers.txt");
char studentAnswers[STUDENT_QUESTIONS];
for (int t=0; t<STUDENT_QUESTIONS; t++)
{
inputFile2 >> studentAnswers[t];
}
int answeredCorrectly = 0;
for(int c = 0; c < 5; c++)
{
int z = 0;
//now we use a for loop to go through the questions and store whether the answer was right or wrong
for (int x = 0; x < QUESTIONS; x++)
{
if (studentAnswers[x] == correctAnswers[x])
answeredCorrectly++;
else
wrongCounter[z]++;
z++;
}
cout << "Report for Student " << c+1 << ":" << endl;
cout << "---------------------" << endl;
cout << "Missed " << 20 - answeredCorrectly << " out of 20 questions for " << (answeredCorrectly / 20) * 100 << "% correct." << endl;
cout << "Answered Correctly: " << answeredCorrectly << endl;
cout << "Questions missed:" << endl << endl;
}
}

I think I've found the a problem
while (badEntry = true)
use ==
while (badEntry == true)
There are other issues too
while (badEntry = true)
{
cout << "Invalid entry" << endl;// I GET AN INFINATE LOOP HERE????
if (studentAnswers[x] == A || B)
badEntry = false;
}
First off, what if none of the studentAnswers are equal to A or B?
Secondly, I think you want your if condition to be the following
if (studentAnswers[x] == A || studentAnswers[x] == B)

You miss one "=" in "while (badEntry = true)". Should be "while (badEntry == true)". And in fact, you can do "while (badEntry)".
Same mistake for "if (studentAnswers[y] = 'N')".
To avoid such problems, you should always put the constant first. For example, do "if ('N' == studentAnswers[y])".

This isn't the main issue, other people have already posted the real problem. However, this also happens to make your program not work properly.
if (studentAnswers[x] != A || B) is an interesting conditional.I imagine you were testing if studentAnswers[x] wasn't equal to either A or B. You can't say this in C++ or with (what I aware of) most languages. Your statement would parse to if((studentAnswers[x] != A) || B). ( != comes before || in binary operator precedence). To fix this, just create a != check for both A and B.
It should look like this: if (studentAnswers[x] != A || studentAnswers[x] != B).
The same goes for ==.
Just looked up to see that "Sam I am" put this in his answer already. Sorry about that.
Anyways, be wary of your boolean test, that seems to be what screwed up most of the program.
EDIT: Looks like "TonyArra" also said the same thing, oops. Well at least you'll remember now.

Couple of problems: while (badEntry = true) will set badEntry to true every time it checks the conditional. So it ends up checking while(true) (aka infinite loop). As others have said, use while(badEntry)instead.
Furthermore, if (studentAnswers[x] == A || B) is also incorrect. It should be: if ( (studentAnswers[x] == 'A') || (studentAnswers[x] == 'B') )
Furthermore, if (studentAnswers[x] == 'A' || 'B') is interpreted as:
if ( (studentAnswers[x] == 'A') || ('B') ) which will come out to true every time because 'B' is non-null. You were also missing the quotations around the letter to denote it as a character.
You may find this helpful: http://www.cplusplus.com/forum/articles/3483/

Related

Why does my int output something different than what it's supposed to? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
My bin in my second-to-last line displays something like 3282692812 when it's meant to be different. Everything else is fine, I have tried searching online but I can't find anything about it.
string a;
int amount;
cout << "1-10k 2-2k 3-1k: ";
cin >> a;
cout << "\n";
cout << "How many numbers do you want to be generated?: ";
cin >> amount;
cout << "\n";
long bin = 0;
if (int(a) = 1)
{
bin = 60457811425;
}
else if (a == 2)
{
bin = 60457811474;
}
else if (a == 3)
{
bin = 6045781165;
}
for (int i = 0; i < amount; i++)
{
cout << bin << rand() % 10 << rand() % 10 << rand() % 10 << rand() % 10 << rand() % 10 << rand() % 10 << "|" << setw(2) << setfill('0') << rand() % (13 - 1) + 1 << "|" << rand() % (2031 - 2022) + 2022 << "|" << setw(3) << setfill('0') << rand() % 999 << "\n";
}
system("pause");
You only converted to a to an int once, you didn't do it with a legal converter ("constructing" an int from a std::string doesn't work like that, and I'd be surprised if your compiler didn't warn you), and you assigned to the result (=) rather than comparing (==) (also something I'd expect a compiler to warn you about).
Later checked about those warnings: As it happens, it looks like gcc accepts both constructs in tandem as a completely different construct (it interprets if (int(a) = 1) as declaring a shadowing int variable named a, initialized to 1, that expires after the if/else if chain; that's fun). It only warns at all if you compile with -Wall, and even then, it only complains because it thinks it should be if (int a = 1) to make the name shadowing variable without extraneous parentheses, ugh. Bright side is that fixing the assignment to a comparison would then reveal the other problem. That's a good reason to always compile with warnings turned up (-Wextra is sometimes overly chatty, but -Wall is almost always pointing out real problems), sadly it's a rather indirect warning in this case.
Convert it once up front with a valid string to int converter like std::stoi, and then test the converted int value against other ints, not std::string against int, e.g.:
const int aint = std::stoi(a);
if (aint == 1) // Using ==, not =
{
bin = 60457811425;
}
else if (aint == 2) // Testing int == int, not string == int
{
bin = 60457811474;
}
else if (aint == 3) // Testing int == int, not string == int
{
bin = 6045781165;
}
Or for slightly more succinct code (and no extra named variables):
switch(std::stoi(a)) {
case 1: bin = 60457811425; break;
case 2: bin = 60457811474; break;
case 3: bin = 6045781165; break;
/* Maybe put a default case here to handle invalid input? */
}

Hangman game - how to do a "wrong guess" in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My game works but not the "wrong guess" part, that "error" means "it says I'm wrong even if my guess is right or it says wrong several times, not just once".
So, in my beginner program, that's where I'm stuck.
char u_recherche;
std::string mot_secret(choisi_ton_mot.length(), '-');
std::string choisi_ton_mot;
int faute = 0;
while (i < 999)
{
//code
for (int i = 0; i < mot_secret.length(); i++) // to check each letter
{
if (choisi_ton_mot[i] == u_recherche) //guess right
{
mot_secret[i] = u_recherche; // if right change "-" to the right letter
std::cout << "lettre trouver ! " << std::endl;
}
}
if (choisi_ton_mot[i] != u_recherche) //guess wrong
{
std::cout << "rater !" << std::endl;
faute++;
}
`
apart from that your code is not complete, the actual errors are easy to spot:
You have two loops. The outer "while" loop and the inner "for" loop. Those both use "i" as index, so the inner "i" hides the outer. This is not a bug in itself, but easily leads to other errors, like in your program.
Your second "if", the one that checks for a wrong guess, is outside the "for" loop, which means that the "i" used is the outer one, and not the one you want to use.
The wrong guess code should trigger only if right guess has not. One way to do this is to introduce a helper variable
So with this in mind, it could be rewritten to:
int tries = 0;
while (tries < 999)
{
//code
bool guess_wrong = true;
for (int i = 0; i < mot_secret.length(); i++) // to check each letter
{
if (choisi_ton_mot[i] == u_recherche) //guess right
{
guess_wrong = false
mot_secret[i] = u_recherche; // if right change "-" to the right letter
std::cout << "lettre trouver ! " << std::endl;
}
}
if (guess_wrong)
{
std::cout << "rater !" << std::endl;
faute++;
}
...

Trouble finding sum of odd integers cubed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Tried several times yet still didnt manage to find my mistake: here is my program. i need to find the odd numbers from 1 and integer x and find the sum of them cubed.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int x;
int i = 1;
int result;
cout <<" Enter the value for n" << endl;
cin >> x;
while (i >x)
if (i%2 == 0) {}
else {
result += pow(i,3);
i++;
}
cout << "The sum of odd integers cubes from " << i << " to " << x << "= " << result << endl;
return 0;
}
Minimally, you should change the compare in while
from
while (i > n)
to
while (i <= n)
There a many numbers where i will be greater than the number entered, n.
You didn't add in your curly brackets for the while loop.
while (i > x)
if (i%2 == 0) {}
needs to be:
while (i > x){
if (i % 2 == 0) {}
}
Plus what are you doing inside of that if statement? You should decrement x, to find if each number is odd.
Plus, your program ends early because i is 1 and if the user enters a number above 1, your while loop won't even run. You're telling the while loop to run ONLY when i is larger than x. Try changing it to less than:
from:
while (i > x){
to:
while (i < x){
Plus you're not doing anything with x. You want to decrement x, not add i. Although, I would recommend using a do-while loop. ( a dowhile loop does one iteration first before incrementation)
do{
if (x % 2 == 0) { // if the remainder of x/2 is 0, do this
x--;
cout << "Equal: " << x << endl;
}
if(x % 2 != 0) { //if the remainder of x/2 is not 0, do this.
temp = pow(x,3);
//you don't want to take the power of the added sum,
//you were taking the power 3 of x which was being added to.
//you want to add the sums of each power. So here we have temp, a
//temporary variable to store your addends.
result = result + temp;
cout << "Not equal, temp:" << temp <<endl;
cout << "Result: "<< result << endl;
x--; //you didn't have a decrement, you need to bring x eventually down to i if you want the loop to end, or even look through all of the numbers
}
}
while (i < x);
//You have to have this semi colon here for the compiler to know its a do-while.
cout << "The sum of odd integers cubes from " << i << " to " << userVar
<< " = " << result << endl;
return 0;
}
note: if-else statements are for flow control, its like true and false, one or the other, so that your data will flow somewhere. I used two if statements because I want to have complete control over the flow.
note2: It's ok to use:
using namespace std;
at first, but eventually you want to start learning what library each command is using. When you get into more complex programming, you start using commands from different libraries than the standard one.

Enhanced if statement with two (different) variable comparison operators [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wanted to make a simple enhanced if statement (that is the correct definition right?) when a == 1 OR b > 2 it prints out how to say how are you sir in Arabic.
Is it possible to compare two different variables in an if and else if statement? I got myself in a Chinese finger trap with all the ('s and )'s and the different types of logical operators.
Here is my c++ code:
#include <iostream>
using namespace std;
//If / Else statement is basically a enhanced if statement.
int main()
{
int a = 1;
int b = 2;
if ((a==1)||(b>2)){
cout << "Kevak chala komm?" << endl;
}
if (((else)) (a == 1) && (b == 2)))) {
cout << "Louis C.K. is back my brothers!" << endl;
}
else{
cout << "Jek shi mash? " << endl; // How are you in polish.
}
return 0;
}
The correct syntax for that looks like this:
if (a == 1 || b > 2 ){
std::cout << "A" << std::endl;
}
else if (a == 1 && b == 2) {
std::cout << "B" << std::endl;
}
else {
std::cout << "C" << std::endl;
}
But it doesn't make any sense in your particular case, since B will never be printed (if a == 1, the first clause will hit, never using the second).
Is it possible to compare two different variables in an if and else if statement?
Yes it is very much possible, you can compare two or more different variables in if and else if stetement and also there can be multiple else if statement as well.
The below code line is wrong
if (((else)) (a == 1) && (b == 2)))) {
write else if instead.

Is this the right way to loop through Arrays?

#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main(){
string Whitelist[4] = {"Stian", "Mathias", "Modaser"};
for (int x = 0; x < 3; x++){
cout << x + 1<< ". " << Whitelist[x] << endl;
if (Whitelist[x] == "Stian" && "Mathias" && "Modaser"){
cout << "" << Whitelist[x] << " is here" << endl;
}
else{ cout << "no one is here" << endl; }
}
cin.get();
return 0;
}
//so yeah basically im just trying to loop through my array and see if any of these names are there. so i guess u can pretty much read what the code does since most of u are pros :P. but when i asked my friend, whos been coding for 1-2 years, he said that i couldnt loop through arrays like this and told me to use a vector. what does he mean by that? and my code works?
This set of code is wrong
if (Whitelist[x] == "Stian" && "Mathias" && "Modaser"){
cout << "" << Whitelist[x] << " is here" << endl;
}
Why? because suppose the first condition of the if statement evaluates to true like this:
if (true && "Mathias" && "Modaser")
{
//...
}
Then the code wouldn't make sense. In an if statement, you have to check for every condition separately, like this:
if (Whitelist[x] == "Stian" && Whitelist[x] =="Mathias" && Whitelist[x] =="Modaser"){
cout << "" << Whitelist[x] << " is here" << endl;
}
But since any 1 string cannot be three names at the same time, this condition will fail, (you used &&). Fix your code using the || operator, like this, for your final code (Also, remove << "", that is just redundant, and unnecessary):
if (Whitelist[x] == "Stian" || Whitelist[x] =="Mathias" || Whitelist[x] =="Modaser"){
cout << Whitelist[x] << " is here" << endl;
}
BTW: As a recommendation, use a std::vector<std::string>, not a raw array, so you get easier and more capabilities than an array.
Lastly, you also have 4 elements in your array, of which one is unused. It might be a typo, so make your array size 3.
There is nothing fundamentally wrong with looping over an array like this.
We can only guess what your friend meant, but I can perform my own review of your code.
However, you have four array elements and only loop over three of them, which may be a mistake; if it is, it's evidence that you'd be better off using iterators, rather than hard-coding numbers that you can get wrong.
Furthermore, your if conditional is wrong: did you mean || ("or"), instead of && ("and")? And you have to write out the adjoined conditions in full, so:
if (Whitelist[x] == "Stian" || Whitelist[x] =="Mathias" || Whitelist[x] =="Modaser")
I'm not sure why you're comparing against all these values, when they're the only ones in the array. Well, except for that empty fourth element; perhaps you're trying to catch that. We don't know, because you didn't tell us. Did you mean to search Whitelist while iterating over some other array? We have no way of knowing. Maybe that's what your friend really meant? Again, I couldn't say.
Streaming "" to std::cout just waits resources and does literally nothing else. Remove it.
Finally, and somewhat tangentially, it would be better not to block waiting for input as a means to keep your console window open. That is not your program's job.