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++;
}
...
Related
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.
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 7 years ago.
Improve this question
I am trying to make a part of the game hangman, where the user inputs a letter and then a loop check for that letter in a random array. If the letter is found, it then couts a changed array including now that letter and offers the user to again, input another letter. It seems for loops are not working since the program doesnt scan the whole array for ever letter inputted. How can I fix this?
int main(){
string guess[25];
string password[5];
srand((unsigned)time(0));
string letters[5] = {"_ ","_ ","_ ","_ ","_ "};
char array[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','z'};
for(int r = 0; r < 5; r++){
int g = rand() % 24;
password[r] = array[g];
}
cout << endl;
for(int z = 0; z < 25; z++){
cout << "Enter Letter: " << endl;
cin >> guess[z];
for(int b = 0; b < 5; b++){
if(uguess[z] == password[b]){
letters[b] = guess[b];
cout << letters[b];
}else{
cout << letters[b];
}
}
cout << endl;
}
can someone point me in the right direction. Thanks
It always says that the word being guessed if asdfg, but it messes it up very badly, as in doesn't always show the letter, even if it has been guessed, it shows it later.
crke[b] = ugib[b];
This line should be:
crke[b] = ugib[z];
You might want to consider investing some time in learning how to use a debugger, which would've helped you figure it out.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
int main()
{
vector<int>numbers;
int numb = 0;
int i = 0;
int j = i - 1;
while (cin >> numb)
{
if (numb == numbers[j]) HERE IS THE PROBLEM!*
{
cout << "Numbers repeated\n";
}
numbers.push_back(numb);
cout << "numbers[" << i << "] = " << numbers[i] << endl;
++i;
}
/*** I don't understand why a exception, run time error, break or whatever it names.....................................................
On the first iteration through the loop, j is -1. Accessing numbers[-1] is undefined behavior because the index is outside the bounds of the vector.
Indeed, accessing any index is out of bounds until you put something in the vector, so you cannot index numbers at all until you have called push_back on it at least once.
This code will display the message if the user enters a number already in the vector:
while (cin >> numb) {
vector<int>::iterator found = std::find(numbers.begin(), numbers.end(), numb);
if (found == numbers.end()) {
cout << "Numbers repeated" << endl;
}
// If you don't want duplicate numbers in the vector, move this statement
// into an "else" block on the last "if" block.
numbers.push_back(numb);
}
This code on the other hand will only display the message when a number was the same as the last number entered, that is, if sequential numbers are the same:
while (cin >> numb) {
if (!numbers.empty() && numb == numbers.back()) {
cout << "Numbers repeated" << endl;
}
numbers.push_back(numb);
}
You need to initialize your numbers vector with initial data, or check to make sure j is withing in vector size. A vector is essentially an array, and like any array, you cannot go out of bounds.
if (j < numbers.size() && numb == numbers[j]) HERE IS THE PROBLEM!*
{
cout << "Numbers repeated\n";
}
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/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm having a super simple problem, but I also had a question, so I figured I'd post both.
First of all, I'm not sure what's not working about this program cycling through an array and out putting the values. What's happening now, is that it just outputs the last value in the array.
int myArray[10] = {0,1,1,2,3,5,8,13,21,34};
int i = 0;
for( i = 0; i < 9; i++);
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
that's not working for some reason, but I also wanted to know why, if I initialize the i variable inside the for loop, it says myArray[i] inside of the for loops, at the cout, isn't initialized at all. Peculiar to me.
I just tested the following code and it worked:
#include <iostream>
using namespace std;
int main() {
int myArray[10] = {0,1,1,2,3,5,8,13,21,34};
for(int i = 0; i < 10; i++) {
cout << i + 1 << '\t' << myArray[i] << endl;
}
}
Notice the syntactical changes that were made. Also you were previously leaving out the last element in the array.
What's important to note is that a semicolon is a null statement. By placing a semicolon after a for loop, you are executing the null statement (i.e. do nothing) for the total number of iterations through the loop. Afterwards, the code inside the curly braces is run as if it were regular code in the body of your method. That's why only the last iteration was printing, because the for loop had "done nothing" for all the other iterations but still incremented i. Therefore, when the code within the braces ran, it did so for the last expected iteration.
for( i = 0; i < 9; i++);
// ^here
Remove the extra semicolon. The original code in your question is equivalent to:
for( i = 0; i < 9; i++)
{
//do nothing
}
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
Now you can declare i inside the for loop:
for( int i = 0; i < 9; i++)
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
You have an extra semicolon here:
for( i = 0; i < 9; i++); // there should be no semi-colon at the end of this line.
// ^ remove this!
What's happening is that the compiler iterates the loop completely, running the "empty" loop body (that extra semicolon). After the loop is done, it reaches your cout statement.
for( i = 0; i < 9; i++);
// ^ THIS TINY PIECE OF ABOMINATION
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
Remove it.
The block below the for statement isn't associated with it because the for is actually associated with a null statement (indicated by the ; immediately after the for()).
You should also put your declaration of i inside for
for(int i = 0; i < 9; i++) {
...
That way, you limit its scope and makes you less prone to some errors.