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.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed last month.
Improve this question
i am just learning c++ programming language and stuck at some point will anyone help me out.
the problem is i had google some stuff and came to know that if conditions can change the varaibles value for temporary basis.
my code is below.
#include <iostream>
using namespace std;
int main()
{
int a = 2;
int b = a + 1;
if ((a = 3) == b)
{
cout << a;
}
else
{
cout << a + 1;
}
return 0;
}
in the above code its printing the else block why not the if block the conditions must be true ?
You are mistaken.
If you will change your code the following way
int a = 2;
int b = a + 1;
if (( a = 3 ) == b)
{
std::cout << "if " << a << '\n';
}
else
{
std::cout << "else " << a + 1 << '\n';;
}
then you will see the output
if 3
In the expression of the if statement
if (( a = 3 ) == b)
the left operand of the equality operator is evaluated. As a result a becomes equal to 3 and in turn is equal to b.
If your compiler supports the C++ 17 Standard then you could declare the variables inside the if statement like
if ( int a = 2, b = a + 1; ( a = 3 ) == b )
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++;
}
...
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 2 years ago.
Improve this question
I'm making a C++ program that input a two-digits number, then print screen method to read that number by letter.
The problem that I'm facing is ternary operator. When I execute code below, output is 1 when input is 11, output is 0 when input is 12. I only post a paragraph of my program. I was shortcut it.
My code snippet :
switch(tens_position_value)
{
case 1:
{
if (unit_position_value == 1|| unit_position_value==2)
{
cout << (unit_position_value == 1) ? "Eleven" : "Twelve";
}
}
}
<< has higher precedence than ?:, so the result of unit_position_value != 1 (0 or 1) is printed instead of the strings. Add parentheses here:
cout << ((unit_position_value != 1) ? "Eleven" : "Twelve");
I am not sure about what you want to obtain, but I am supposing that if tens_position_value = 1 and unit_position_value = 1, you want to print "Eleven", while if unit_position_value = 2, you want to print "Twelve":
#include <iostream>
#include <string>
int main()
{
int tens_position_value = 1;
int unit_position_value = 1;
switch(tens_position_value)
{
case 1:
{
if (unit_position_value == 1|| unit_position_value==2)
{
std::string result = (unit_position_value == 1) ? "Eleven" : "Twelve";
std::cout << result << std::endl;
}
}
}
std::cout << "Bye ^_^\n";
}
In this case the output is "Eleven", if you change the value of unit_position_value to 2, you obtain "Twelve"
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.
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/