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 2 years ago.
Improve this question
This a simple number guessing game. If you guessed the number right, it outputs "You win!!!", but if the number of tries (numberofguesses) is exceeded, it should output "You lose", but it is showing "You win!!!" even though I checked the values of numberofguesses, secretnum and guess after the while loop. Answer in simple words, I'm a beginner.
#include <iostream>
using namespace std;
int main()
{
int secretnum = 7;
int guess = 0;
int numberofguesses = 3;
while (secretnum != guess && numberofguesses != 0) {
cout << "enter your guess: ";
cin >> guess;
--numberofguesses;
}
if (secretnum = guess && numberofguesses != 0) {
cout << "You win!!!";
}
else
{
cout << "You lose";
}
}
You have mistaken the assignment operator = with the comparison operator ==.
In this line here:
if (secretnum = guess && numberofguesses != 0)
cout << "You win!!!";
Change it to:
if (secretnum == guess && numberofguesses != 0) {
cout << "You win!!!";
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 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
It seems, the if statement is not being called in the given program. The output says it's a consonant even if the input is a vowel.
#include <iostream>
using namespace std;
int main() {
char input[1];
cout << "Enter an alphabet:\n";
cin >> input;
if (input == "a" || input == "e" || input == "i" || input == "o" || input == "u") {
cout << "It is a vowel";
}
else
cout << "It is a consonant";
return 0;
}
First of all you don't need to use an array. And on top of that you should use single quotes, so you should have something like this :
int main(){
cout<<"Enter an alphabet:\n";
char input;
cin>> input;
if (input=='a' || input=='e' || input=='i' || input=='o' || input=='u' ){
cout<<"It is a vowel";
}
else
cout<<"It is a consonant";
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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
string word;
int l,eFound,xFound;
l = word.size();
cout <<"Enter a word: ";
cin >> word;
for (l>0 ; word.at(l)!='x' || word.at(l)!='e'; l--)
if (word.at(l) == 'e'){
eFound = true;
}
else if (word.at(l) == 'x'){
xFound = true;
}
if (eFound == true && xFound == true){
cout << "Your word, "<<word<<", contains the character 'e'"<<"\n";
cout << "Your word, "<<word<<", contains the character 'x'";
}
if (eFound == true && xFound != true){
cout << "Your word, "<<word<<", contains the character 'e'";
}
if (xFound == true && eFound != true){
cout << "Your word, "<<word<<", contains the character 'x'";
}
I'm not sure what is going on I'm trying to use a for loop to detect either e or x in a input of some word. I've clicked on other pages with the same error but they have different codes and I don't really understand what is explained. So what is causing this error? I'm 2 weeks into my first programming class, sorry if I'm asking a dumb question.
The issue is that indexing of std::string starts from zero. Not from 1. So, word.at(l) will crash if l = word.size();.
You should change the statement to: l = word.size() - 1;.
Also, Change your loop condition to for (; l >= 0 ; l--)
Suggestion:
Please go for library functions:
Like this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cout <<"Enter a word: ";
cin >> word;
bool eFound = word.find('e') != string::npos;
bool xFound = word.find('x') != string::npos;
if (eFound) {
cout << "Your word, "<<word<<", contains the character 'e'" << "\n";
}
if (xFound) {
cout << "Your word, "<<word<<", contains the character 'x'" << "\n";
}
return 0;
}
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 4 years ago.
Improve this question
why are my else if loops not working
the code when ran skips over the else statement and executes the if statement even if it is not true
#include<iostream>
using namespace std;
int a;
int b;
char operation;
int sum;
int sub;
int mul;
int divide;
int mod;
int main()
{
cout<< "enter a number \n";
cin >> a>>operation>>b;
if (operation= '')
{
sum=a+b;
cout << a<<operation<<b<<sum;
}
else if (operation= '-')
{
sub=a-b;
cout << a<<operation<<b<<sub;
}
else if (operation= '/')
{ if(b==0){
cout<<"You cannot divide by zero";
}
else{divide=a/b;
cout << a<<operation<<b<<divide;}
}else if (operation= '*')
{
mul=a+b;
cout << a<<operation<<b<<mul;
}
else if (operation= '%')
{
mod=a%b;
cout << a<<operation<<b<<mod;
}
else{
cout<<"Invalid input";
}
return 0;
}
the output is always just adding the numbers no matter what sign the input has it wont go over my else if statements even if the operation isnt + infact even if i use a wrong operation it just adds the two numbers
there are two thing to get right,
the comparison in the if statement should be done with == not with =
= will assign the rightside value to the variable operation also you missed + in the first comparison
if (operation= '') should be if (operation == '+')
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 6 years ago.
Improve this question
#include <iostream>
#include <string>
using namespace std;
int main ()
{
do
{
string name, answer;
cout << "Welcome to the prime number checker! Please enter your name: ";
getline (cin, name);
int a;
cout << "\nHello " << name;
cout << "\nPlease enter an integer: ";
cin >> a;
cin.sync();
if (a == 2)
{
cout << "\nThis is a prime number" << endl;
}
else
{
for (int b = 2; b < a; b++)
{
if (a % b == 0)
{
cout << "This number is not prime number" << endl;
break;
}
else
{
cout << "This number is a prime number." << endl;
break;
}
}
}
cout << "Do you want to do this again (Yes or No)?";
getline (cin, answer);
}
while (answer == "yes" || answer == "YES" || answer == "Yes"); //Not declared in this scope
return 0;
}
You declared answer within the do block. But then try to reference answer outside of that scope block.
Declare answer at the top of main instead of in the do block.
You need to move the declaration of answer outside the loop:
string answer;
do {
string name;
...
} while (answer == "yes" || answer == "YES" || answer == "Yes");
If you declare it inside the loop, it no longer exists by the time the while clause is evaluated.
As other people said, the "answer" variable only exists inside the loop - it isn't accessible from outside it.
One other recommendation: rather than checking every possible permutation of capitalization just cast the whole string to lowercase. (You actually missed several - there are 6 total because each position could have one of 2 possible values. Presumably something like "YeS", for example, should still be accepted as "yes").
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 8 years ago.
Improve this question
I'm working on a validation if loop which checks for pipes at the beginning and end and makes sure there are 32 valid characters (valid chars are : and |)
I'm wondering why my program is not reading the if statement correctly for a 32 character input. Here is what I have so far.
void checkitout(string validate)
{
string check;
check = validate;
if ((check.length() == 31) &&
(check.substr(0,1) == "|") &&
(check.substr(31,1) == "|"))
{
cout << "is this running?";
for (int i = 0; i < 31; i++)
{
cout << "for loop running";
if (!(check.substr(i, 1) == ":") || !(check.substr(i, 1) == "|"))
{
cout << "Please enter acceptable barcode.";
return;
}
}
}
else
{
cout << "else Please enter acceptable barcode";
}
}
I'm new to this but I think I'm on the right track. The couts are to test to see if the loop is working. It goes right to the else state. Here is a sample input
||:|:::|:|:||::::::||:|::|:::|||
As always, any thoughts on how to do this better are greatly appreciated.
Your string has a lenght of 32, thus the if-condition is false because of check.length() == 31.
Also the if-condition in your loop needs an "&&" instead of an "||", since you want it to be neither "|" nor ":" to be an unacceptable barcode.
Changes are marked in bold.
void checkitout(string validate)
{
string check;
check = validate;
string one = check.substr(4,1);
cout << (check.substr(4,1) == one) << endl;
if ((check.length() == **32**) &&
(check.substr(0,1) == "|") &&
(check.substr(31,1) == "|"))
{
cout << "is this running?";
for (int i = 0; i < 31; i++)
{
cout << "for loop running";
if (!(check.substr(i, 1) == ":") **&&** !(check.substr(i, 1) == "|"))
{
cout << "Please enter acceptable barcode.";
return;
}
}
}
else
{
cout << "else Please enter acceptable barcode";
}
}