Can I have a do while statement in C++ that checks for both a character and a int value before looping? [closed] - c++

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 very new at this and have an assignment in which I would like for a loop to exit if the user inputs(trans) 'e' but also end if a calculation balance(bal) is less than a constant I have set. Basically as my question states one is a character and the other an integer, will that work? I'm not trying to get people to do my homework for me, so I'm not posting all of my code or assignment, hope it makes sense.
This is the line of code I have
do {
ask user input(&trans)
e or calculation
{
while (trans != 'e'| bal < -OVR);

Just use regular unconditional loop and multiple exit conditions:
while( true ) {
char trans;
std::cin >> trans;
if( !std::cin or trans == 'e' )
break;
calculation;
if( bal > -0VR )
break;
}
So first of all you would not do unnecessary calculations, but what is more important you would make your code more readable and easier to understand - you make loop exit decision where it should be instead of pushing it into the end.

Related

Check an Array String character value without extra variable [closed]

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 1 year ago.
Improve this question
suppose I've this code:
string str[] = {"devil", "chandra"};
// check if str[0] has properly devil, character by character or not without extra variable
Now I want to check str[ 0 ]'s all character which is 'd','e','v','i','l' one by one without extra variable.
with extra variable code will be :
string n1 = "devil";
for(int i=0; i<1; i++){
string s1 = str[i]
for(int j=0; j<s1.size(); j++){
if(s1[i] == n[i]){
cout << s1[i] << " ";
}
}
Basically, I want O(n) loop where I can access all indexes string and among them all characters.
Like s[ i ] is "devil" and s[[i]] = 'd' something like this, Know it's not valid, but is there any way to do that??
Even I don't know is it a valid question or not!
I'm not sure why you would need an extra variable. If you need a conditional that checks that the first value in the array of strings is "devil", it shouldn't be anymore complicated than:
if (str[0] == "devil")
{
* Do things *
}
C++ can check a standard string all at once. You don't need to check each individual character if that's what you're thinking.
Keep in mind, this isn't going to account for situations where the string is not exactly the same. For instance, if str[0] has "Devil" instead of "devil", then the conditional will evaluate to false.

Finding maxium and minimum element in binary search tree [closed]

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
I am a newbie coder . Today I learned about Binary Search Trees,saw a task of finding minimum and maximum element in bst. here is my code. i am getting the "-1" every time.
For a binary tree, if you always take the leftmost child, you'll find the min element, if you always take the rightmost child, you'll find the max element:
min_node = root;
while( min_node.left != NULL ) {
min_node = min_node.left;
}
max_node = root;
while( max_node.right != NULL ) {
max_node = max_node.right;
}
In both FinMin and FindMax swap the "else if" and "else" statements and you will get the right answer.

C++ while loop with 2 conditions, VS. for loop with 2 conditions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
If I wanted to iterate through a string, and stop once I found either the letter "o" or the end of the string, would it make any difference to use a while loop to check the 2 conditions, or a for loop with 2 conditions?
const int STRING_SIZE = 16;
char str[STRING_SIZE] = "a bunch of words";
for loop
for (int i = 0; i < STRING_SIZE && str[i] != 'o'; i++){
//do something
}
while loop
int i = 0;
while (i < STRING_SIZE && str[i] != 'o'){
//do something
i++
}
Is there a performance difference between the two? Is one better practice than the other?
There is no difference in performance between the two loops except that:
for() Checks condition then if true its body is executed. So for is simile to while loop.
do-while loop works a slightly different: It executes then checks so at least an execution is ensured.

Can someone explain please what does this do : n&1? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Here is the code, I know what does it do , but I don't understand, what does the if condition do?
if(n&1)
{
for(i=n/2,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
else
{
for(i=n/2-1,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
return 0;
}
Its a bitwise operator. There's a search term for you!
The & operator provides a mask that "cancels out" bits in the first depending if they're set in the second parameter - so assume N is the number 17, that expressed in binary is 00010001, the number 1 in binary is 00000001, so masking the two together will "blank" the first set of bits, leaving you with N as 00000001.
Basically that particular if statement drops all except the last bit, which is either 0 or 1, so it is a condition detecting if N is odd or even.

Small thing about input using while loop [closed]

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 6 years ago.
Improve this question
I hope you guys all are having a great day!
I have a quick question about using the while loop for competitive programming (we do not know the size of the input, so we have to read until the end of file or 0 value)
For this particular program, the program end with 2 values of 0 as "0 0", and the code I saw used this:
while (cin >> r >> n, r || n) {
// code
}
My question is about the >>> , r || n <<<< part:
Is the while loop as the same meaning as
while ( (cin >> r >> n) || (r || n) )
can I have some preferences to read more about the multi conditions for the while loop.
Please regard my dump question :( Tks you all for reading this post!
Basically.... comma has the lowest precedence and is left-associative.
Given A , B
A is evaluated
The result of A is ignored
B is evaluated
The result of B is returned as the result.
Further Reading : https://stackoverflow.com/a/19198977/3153883
So in your case, cin loads r and n. The return value from that operation is ignored. r or n happens and is the result of the whole while expression. So, a 0 0 will cause the while loop to terminate.