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 6 years ago.
Improve this question
I have this code, its just a part from another bigger code. This may seem like a very simple mistake, but I still cannot figure out what it is. My problem is that the value of 'house1' cannot exit the while loop and move on to switch statement. for example, if I entered house1 = 0, the program will print "Invalid value, please enter again" but if I entered house1 = 1 after I enter the invalid value, it will also print "invalid value, please enter again". Does anyone have any idea what was my mistake?
cin >> house1 ;
while ((0 <= house1) || (house1 >= 6))
{
cout << "Invalid value! please enter again:";
cin >> house1;
}
switch (house1)
{
case '1':
h1_p1 = h1_p1 - 5;
h2_p1 = h2_p1 + 1;
h3_p1 = h3_p1 + 1;
h4_p1 = h4_p1 + 1;
h5_p1 = h5_p1 + 1;
house_p1 = house_p1 + 1;
break;
case '2':
h2_p1 = h2_p1 - 5;
h3_p1 = h3_p1 + 1;
h4_p1 = h4_p1 + 1;
h5_p1 = h5_p1 + 1;
house_p1 = house_p1 + 2;
break;
If you meant the valid value should be from 1 to 5, the condition should be
while ((house1 <= 0) || (house1 >= 6))
{
cout << "Invalid value! please enter again:";
cin >> house1;
}
BTW: You're not checking invalid input (the failbit of cin) for house1.
The main problem with this code is this statement
while ((0 <= house1) || (house1 >= 6))
See if you enter 0,1,2,3,4,5,6,7,... or any positive integer number (as the value of house1) this while statement is correct for that.
So the control goes into the while loop and will not come out of the loop until you enter the negative number... Because when you enter the negative number only then the condition in the while statement will not satisfy and the control will come out of the loop.
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 3 years ago.
Improve this question
in this example :
int numbers=0 ,sum=0;
while (numbers >=0)
{
cout<<"Enter positive numbers: ";
cin>>numbers;
sum += numbers;
}
cout<<"The result = "<<sum<<"\n";
Can u help me what I should to do please?
In the loop, you'll have to deal with two situations.
The user enters invalid input or EOF.
The user enters a number less than 0.
For the first, you'll need to use:
if ( cin >> numbers )
{
// Reading to numbers was successful.
}
else
{
// Deal with the error.
}
For the second situation, you'll need to use:
if ( numbers < 0 )
{
break;
}
Put it all together,
while ( true )
{
cout << "Enter positive numbers: ";
if ( cin >> numbers )
{
if ( numbers < 0 )
{
break;
}
}
else
{
// Deal with error. Perhaps break out of the loop too?
break
}
sum += numbers;
}
cout << "The result = " << sum << "\n";
You could add:
if (numbers < 0) {
return;
}
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 3 years ago.
Improve this question
both of my options are coming up when I run the code
I have tried two if statements else if and else
cout << "would you like to hit or stand?" << endl; //asking if you would ike to hit or stand
bool hit; //the hjitting opption
bool stand; // the stand / hit option
cin >> hit, stand; // the avaiabillity for hitting or standing (player input)
if ( hit = true) // if you hit
{
for (n = 1; n <= 1; n++) //loop for how many cards are you have
{
num = rand() % 13; //get random number
cout << num << "\t"; //outputting the number
}
}
{
cout << "you stand \n"; // saying you stand
I expect the code to output the number when u say hit and say you stand when you say stand but it is out putting either just the hit just the stand or bothenter code here
The snippet:
bool hit;
bool stand;
cin >> hit, stand;
does not magically set one of the booleans based on what you enter. Your cin statement will attempt to get two separate booleans from the user.
What you probably want to do is get a string on then act on that, something like:
std::string response;
std::cin >> response;
if (response == "hit") {
do_hitty_things();
} else if (response == "stand") {
do_standy_things();
} else {
get_quizzical_look_from_dealer();
}
In addition (though irrelevant if you take my advice), the expression hit = true is an assignment rather than a comparison. A comparison would use ==. The result of if (hit = true) is to first set hit to true and then use that as the condition. Hence it will always be true.
Also see here for the absurdity of explicitly checking booleans against true and/or false.
Hit or stand is one choice so you need one boolean variable.
bool hit;
cin >> hit;
hit is a boolean variable so it's already true of false, you don't need to compare it with true (or false). So just if (hit) is OK. If you were to compare it with true then it's == not =, so if (hit == true) would be OK as well.
Finally since your choice results in two alternatives you need an if ... else ... statement.
if (hit)
{
for (n = 1; n <= 1; n++) //loop for how many cards are you have
{
num = rand() % 13; //get random number
cout << num << "\t"; //outputting the number
}
}
else
{
cout << "you stand \n"; // saying you stand
}
When you are still learning the basics of C++ syntax and rules you need to write smaller amounts of code. Even in this short program you had multiple errors and it's hard to figure out what's wrong when that happens. At this stage you should literally be writing one line of code at a time. Test that to make sure it's works before you write the next line.
This question already has an answer here:
How to reset std::cin when using it?
(1 answer)
Closed 5 years ago.
int departmentNo;
bool depNumIncorrect = false;
do
{
depNumIncorrect = false;
cout << "Please enter your department number... ( 1 / 2 / 3 )" << endl;
cin >> departmentNo;
if (departmentNo < 1 || departmentNo > 3)
{
cout << "Invalid Entry." << endl;
depNumIncorrect = true;
}
} while (depNumIncorrect == true);
Whenever I input a correct input (1/2/3), the code works fine and continues without looping. Whenever I input an incorrect integer, the code loops as it is supposed to. But when I input a character or a string, it puts the code into an infinite loop. The "Please enter your department number... ( 1 / 2 / 3 )" is repeatedly output to the console.
How could I go about changing this so that it doesn't start an infinite loop when a character or string is input, but rather loops as it is supposed to and allows me to input again?
deparmentNo is (I assume) an Int and you have a problem of conversion. As mentioned by others you should clear error flag of std::cin. Moreover you probably should not cin directly into an int but rather to a string , and then attempting conversion to int. The result of conversion attempt (successful or not) would be an additional criteria to check for valid entry
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 7 years ago.
Improve this question
when I was trying to calculate the sum of even numbers the result isn't correct if the starting number is negative?
Here is the code:
#include <iostream>
using namespace std;
int main() {
signed int startingNumber, upperLimit;
signed int result = 0;
cout << "please enter the starting number: "; cin >> startingNumber;
cout << "please enter the upper limit: "; cin >> upperLimit;
while (true) {
if (startingNumber % 2 != 0) result += startingNumber;
if (upperLimit == startingNumber) break;
startingNumber++;
}
cout << "The result is: " << result << endl;
}
But when I run the script the following is the result:
please enter the starting number: -2
please enter the upper limit: 4
The result is: 3
I think the result should be 4 not 3, as the calculation is -2 +0 + 2 + 4 = 4.
Regards,,,
You're using if (startingNumber % 2 != 0) which means it will only add up numbers which are not divisible by two, resulting in -1 + 1 + 3 = 3. The condition in the if statement is only true if the remainder of dividing startingNumber by two is non-zero, which will be the case for odd numbers.
Use if (startingNumber % 2 == 0) result += startingNumber; instead and you should get the right sum.
Change this (startingNumber % 2 != 0) with this:
(startingNumber % 2 == 0)
Because what you want is to sum the even numbers and not odd numbers.
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 years ago.
Improve this question
My goal is to create a C++ program that executes a chunk of code repeatedly until the user enters in an appropriate value and does so with the use of a while loop. My code is just repeating over and over and even if I input a "0" it still repeats the chunk of code in the loop.
Here is my source code:
#include <iostream>
using namespace std;
int main()
{
int num = 0;
bool repeat = true;
while (repeat = true)
{
cout << "Please select an option." << endl;
cout << "[1] Continue Program" << endl;
cout << "[0] Terminate Program" << endl;
cout << "---------------------" << endl;
repeat = false;
cin >> num;
cout << endl;
if (num = 1)
{
repeat = true;
//execute program
}
else if (num = 0)
repeat = false;
else
cout << "Please enter an appropriate value.";
}
return 0;
}
while (repeat = true)
^^
is one of your problems:
while (repeat == true)
^^
With an assignment, the condition always evaluates to a true.
Some people advocate using Yoda condition to avoid these typos. Another way is to simply compile your program with the highest warning levels:
-Wall
Check your operators. You're using the assignment operator = instead of the comparison operator == in your while and if arguments.
while (repeat = true)
In the while condition, you are using the assignment operator =, not equality ==.
It's valid C++ syntax, but not what you expected. repeat is assigned to true, so the condition is always true.
The same error exists in if (num = 1) and else if (num = 0).