C++ Loops forever [closed] - c++

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
I wrote the following loop as a part of a program for my CS homework, however regardless of the input, the program keeps looping at this exact point. What am I doing wrong?
#include <iostream>
using namespace std;
char choice;
do
{
cout << "Type 'c' for characters or type 'n' for numbers: ";
cin >> choice;
}while (choice != 'c' || choice != 'n');

A do-while statement loops as long as the while expression is true.
Your while expression is
choice != 'c' || choice != 'n'
In common English, that expression means
choice is not 'c' OR choice is not 'n'
That statement, logically, is always true. choice is always not one of those things.
In both English and C++, you would want to use and/&& in that expression.

Related

Why does the compiler execute the while loop again even when the condition is not getting satisfied? [closed]

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
while(ans == 'Y' || 'y')
{
cout<<"--MENU--"<<endl;
cout<<"1.Create a file"<<endl;
cout<<"2.Display that file"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your choice-";
cin>>ch;
switch(ch)
{
case 1:
create(); //create and display functions are written above this, which are not required
break;
case 2:
display();
break;
case 3:
exit(0);
}
cout<<"Do you want the menu again?(Y or y for Yes and anything else for a No)";
cin>>ans;
}
My expectation is:-
When the input is other than Y or y in ans in the last line, the control of the program should exit the loop and execute the next line...
However it executes while loop again. Why?
Suppose if the input is n in the last line, then the compiler should check whether ans contains the character Y or y and should exit the loop.
The condition inside the while ans=='Y'||'y' always evaluates to true.
It should actuay be: ans == 'Y' || ans == 'y'
The way it was written, it only evaluates the equality comparison for ans=='Y' and then it does a logical or with 'y' which is interpreted as true (any non 0 value in C is considered true) so the whole condition is always evaluated true, regardless of the first part. I hope this makes sense.

Wrong type of input [closed]

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
I have the code below, so basically my problem is: when someone input a character like 'a', it will pop a message that requires a re-input
I tried using the ASCII:
if (a >= 97 && a <= 122) but it still didn't work
double a;
cin >> a;
if (a >= 'a' && a <= 'z')
{
cout << "Wrong input, please re-input a: " << endl;
cin >> a;
}
cout << a;
I expect it to pop the message to re-input but the actual output is always 0 no matter what character I input
The state of a stream can be checked by using it directly in a condition. If all is okay it "returns" true, otherwise "false". So you can do e.g.
if (!(cin >> a))
{
// Invalid input, or other error
}
On invalid input you need to clear the state.
Note that if the input is invalid then the input will not be read, and the next time you attempt to read you will read the exact same input that failed the first time. One way to solve it is to ignore the rest of the line. Another is to read a whole line into a string that you then put into an input string stream for the parsing of the input.

not equal is c++ (if else statements) [closed]

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 5 years ago.
Improve this question
Problem is that i dont know what i am doing wrong here...
i need to get if a = 1 cout is "pasirinkai fizika..."
and if a != 1 cout is "nieko nepasirinkai..."
here is code:
cout << "Pasirinkimai: parasyk skaiciu... \n";
cout << "1 ---- Skaiciuoti fizika 9 klasiai...\n";
cin >> a;
std::getchar();
if (a = 1) {
cout << "pasirinkai fizika...";
}
else if (a != 1) {
cout << "nieko nepasirinkai...";
}
std::getchar();
When i type 2 for example it says that "pasirinkai fizika..."
and as i said before it should say "nieko nepasirinkai..."
= is an assignmenet operator, you are looking for equality operator == e.g. if (a == 1).
Many languages use this C notation, you might need to get used to it.

save variable from if statement [closed]

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
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.

C++ comparing a character of a string [closed]

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 years ago.
Improve this question
Ok, so input is a string. When I try to compile the following code I get
c.cpp:42:10: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
Why?
if(input[i] != ' ')
{
char s = input[i];
if(s == "+")
{
...
}
}
Use single quotes in this statement
if(s == "+")
as here
if(s == '+')
As char s is a character so it can only be compared against another character or ascii value.
Double quotes (" ") are used for string while single quotes (' ') for characters.