While loop doesn't exit even with false conditions applied [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 3 years ago.
Improve this question
My program won't exit even upon returning the wrong value in the while loop. What should I do? I just started programming and can't figure it out. I've googled for answers and copied tutorials and it still won't work. I'm assuming there is some conflicting code or syntax errors but I can't find it. Please help.
using namespace std;
int power(int baseNum, int powNum){
int result = 1;
for(int i=0; i<powNum; i++){
result = result * baseNum;
}
return result;
}
int main() {
char restart;
while(restart = 'r')
{
int numb1;
int numb2;
char op;
cout << "Enter the first number: ";
cout << endl;
cin >> numb1;
cout << "Enter operator: ";
cout << endl;
cin >> op;
cout << "Enter the second number: ";
cout << endl;
cin >> numb2;
switch(op){
case '+':
cout << "Sum = " << numb1 + numb2 << endl;
break;
case '-':
cout << "Difference = " << numb1 - numb2 << endl;
break;
case '*':
cout << "Product = " << numb1 * numb2 << endl;
break;
case '/':
cout << "Quotient = " << numb1 / numb2 << endl;
break;
case '^':
cout << "Result = " << power(numb1, numb2) << endl;
break;
default:
cout << "Error: Invalid Operator!" << endl;
}
cout << "Press [R] to restart." << endl;
cin >> restart;
}
cout << "the end" << endl;
return 0;
}

The bug is here: while(restart = 'r') it should be while(restart == 'r')

Related

C++: I'm trying to create a calculator, but every time I try to divide, substract, or multiply, the calculator only keeps adding [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 1 year ago.
Improve this question
I'm trying to create a calculator, but every time I try to divide, substract, or multiply, the calculator only keeps adding. I'm dutch btw, so
Add,
Substract,
Multiply,
Divide
Antwoord = answer
Bewerking = operator
Pls help! It's for a school assignment.
Code below
#include <iostream.h>
#include <conio.h>
int main ()
{
cout << "Calculator [v.1.0]" << endl;
cout << "(c) 2021 <Chayenne van der Erf>" << endl << endl;
cout << "Kies een bewerking en druk op Enter:" << endl;
cout << "1. Optellen 2. Aftrekken" << endl;
cout << "3. Vermenigvuldigen 4. Delen" <<endl;
cout << "5. Kwadraat 6. Worteltrekken" <<endl;
cout << "7. Reciproke 8. Logarithme" <<endl;
cout << "0. Exit" << endl << endl;
int Bewerking;
cout << "Bewerking: ";
cin >> Bewerking;
cout << "" << endl;
switch (Bewerking) {
case 1:
cout << "+";
break;
case 2:
cout << "-";
break;
case 3:
cout << "*";
break;
case 4:
cout << "/";
break;
default: "Invalid Number";
}
cout << "" << endl << endl;
float A, B;
cout << "Enter een waarde: ";
cin >> A;
cout << "Enter een waarde: ";
cin >> B;
float antwoord;
if (Bewerking = '+') {antwoord = A + B;}
else if (Bewerking = '-' ) {antwoord = A - B;}
else if (Bewerking = '*') {antwoord = A * B;}
else if (Bewerking = '/') {antwoord = A / B;}
cout << "" << endl;
cout << "= " << antwoord << endl;
getch();
return 0;
}
The if (Bewerking = '+') {antwoord = A + B;} always returns true, since you assign Bewerking = '+' not compare Bewerking == '+'.
You need to change all the conditions to have the following form:
if (Bewerking == '+');
Yet, clearing this bug still gives invalid results.
This is because in cin >> Bewerking; user supplies a number, while the check we make is if (Bewerking == '+'), where we look for a character. So, in an none of the if clauses will be really true, thus the antwoord will remain uninitialized, leading to bizarre output at the end.
You just have to stick to the numbers then:
double antwoord = 0; // Better supply a default value
if (Bewerking == 1) antwoord = A + B;
if (Bewerking == 2) antwoord = A - B;
if (Bewerking == 3) antwoord = A * B;
if (Bewerking == 4) antwoord = A / B;
// If none is true, the antwoord defaults to 0
Or you can make Bewerking a char and build all the logic around that.

Can you help me to solve errors in this code? [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 2 years ago.
Improve this question
I started studying C ++ recently and I'm trying to create a "calculator" with a few operations.
But I'm stuck in string 26 (cin >> choose;), which seems unresponsive. Why?
//CALCULATOR
#include <iostream>
using namespace std;
int main()
{
int start;
int choose;
int first;
int second;
int unic;
int result;
cout << "CALCULATOR (2 values)" << endl;
cout << "Click a botton to continue: " << endl;
cin >> start;
cout << "Write: " << endl;
cout << "- '1' for sum" << endl;
cout << "- '2' for subtraction" << endl;
cout << "- '3' for moltiplication" << endl;
cout << "- '4' for power of 2" << endl;
cout << "Your answer: " << endl;
cin >> choose;
cout << "________________________" << endl;
{
if (choose=1, 2, 3)
cout << "Insert the first value: " << endl;
cin >> first;
cout << "Insert the second value: " << endl;
cin >> second;
{
if (choose=1)
result = first + second;
}
{
if (choose=2)
result = first + second;
}
{
if (choose=3)
result = first * second;
}
{
if (choose=4)
cout << "Insert the value: " << endl;
cin >> unic;
result = unic * unic;
}
}
cout << "Your result is: " << result;
}
It doesn't give me any errors, but it continues to execute all the "cout" operations I wrote, without giving me the possibility to write my values ​​with "cin".
There are several issues in your code,
In C++ = stands for assignment to check value equivalency you have to use ==
if (choose = 1, 2, 3){
...
// this doesn't work in C/C++
// change it into
if (choose == 1 || choose == 2 || choose == 3)
When you have more than one line of code under a conditional (if/else or loops for/while) you will need to explicitly block them inside curly braces. So that changes if first if block into this
if (choose == 1 || choose == 2 || choose == 3){
cout << "Insert the first value: " << endl;
cin >> first;
cout << "Insert the second value: " << endl;
cin >> second;
...
Same goes for the nested if condition.
Also there's no reason to take input for start.
If you fix all the errors you should get a code like this ->
//CALCULATOR
#include <iostream>
using namespace std;
int main()
{
int start;
int choose;
int first;
int second;
int unic;
int result;
cout << "CALCULATOR (2 values)" << endl;
cout << "Click a botton to continue: " << endl;
// cin >> start;
cout << "Write: " << endl;
cout << "- '1' for sum" << endl;
cout << "- '2' for subtraction" << endl;
cout << "- '3' for moltiplication" << endl;
cout << "- '4' for power of 2" << endl;
cout << "Your answer: " << endl;
cin >> choose;
cout << "________________________" << endl;
if (choose == 1 || choose == 2 || choose == 3){
cout << "Insert the first value: " << endl;
cin >> first;
cout << "Insert the second value: " << endl;
cin >> second;
if (choose == 1)
result = first + second;
if (choose == 2)
result = first + second;
if (choose == 3)
result = first * second;
}
if (choose == 4){
cout << "Insert the value: " << endl;
cin >> unic;
result = unic * unic;
}
cout << "Your result is: " << result << endl;
}
Footnote: Please use the given code as reference and try to understand the basics carefully. It is importat you do that.

How can I make my loop ask me for my next input in C++ [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 2 years ago.
Improve this question
I am trying to make a Yahtzee game told hold my dice. But I keep getting an infinite loop.
cout << "Choose what Dice you want to hold. Use numbers from 0-4. If you are done selecting Dice type 5. ";
cin >> Hold;
for(int i = 0; i < 5; i++)
{
switch (Hold)
{
case 0: cout << "You held the " << NumArray[0] << " Die. ";
continue;
case 1: cout << "You held the " << NumArray[1] << " Die. ";
continue;
case 2: cout << "You held the " << NumArray[2] << " Die. ";
continue;
case 3: cout << "You held the " << NumArray[3] << " Die. ";
continue;
case 4: cout << "You held the " << NumArray[4] << " Die. ";
continue;
case 5: i + 4;
continue;
}
i++;
}
I have tried using a for loop and a while loop. I'm not sure if there is a better way to do this. Or I'm just blind and doing something that is simple. I'm still new to C++.
From the code you've posted I would say you to do this:
cout << "Choose what Dice you want to hold. Use numbers from 0-4. If you are done selecting Dice type 5. ";
do
{
cin >> Hold;
switch (Hold)
{
case 0:
cout << "You held the " << NumArray[0] << " Die. ";
break;
case 1:
cout << "You held the " << NumArray[1] << " Die. ";
break;
case 2:
cout << "You held the " << NumArray[2] << " Die. ";
break;
case 3:
cout << "You held the " << NumArray[3] << " Die. ";
break;
case 4:
cout << "You held the " << NumArray[4] << " Die. ";
break;
default:
break;
}
} while(Hold != 5);
Or better:
cout << "Choose what Dice you want to hold. Use numbers from 0-4. If you are done selecting Dice type 5. ";
do
{
cin >> Hold;
if(Hold >= 0 && Hold <= 4
cout << "You held the " << NumArray[Hold] << " Die. ";
} while(Hold != 5);
However, if it's a game I suppose you should save the input data somewhere to use it later. I think you could do this:
bool heldDies[5] = {false};
cout << "Choose what Dice you want to hold. Use numbers from 0-4. If you are done selecting Dice type 5. ";
do
{
cin >> Hold;
if(Hold >= 0 && Hold <= 4
{
heldDies[Hold] = !heldDies[Hold];
cout << heldDies[Hold] ? "You held the ":"You dropped the " << NumArray[Hold] << " Die. ";
}
} while(Hold != 5);
Then you would have the heldDies array to use it later.
It's hard to understand what you actually meant, but I think it could be as simple as that:
cout << "Choose what Dice you want to hold. Use numbers from 0-4. If you are done selecting Dice type 5. ";
vector<int> HeldIndices;
while(true)
{
cin >> Hold;
if (Hold == 5) break;
HeldIndices.push_back(Hold)
}
// the next will print all the recorded dice like:
// "You held the 4 5 6 Dice."
if (HeldIndices.size() > 0)
{
cout << "You held the ";
for (auto& HeldIndex : HeldIndices)
{
cout << NumArray[HeldIndex] << ' ';
}
cout << " Dice." << endl;
}
else
{
cout << "You held no dice";
}
The vector<int> is like an array, but it has dynamic size and it's easier to work with.

What seems to be wrong in my C++ calculator? [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
#include <iostream>
using namespace std;
int main()
{
int num1,num2,answer;
char choice = 'Y',input;
while (choice == 'Y' || choice == 'y')
{
cout << "Enter the first number: " << endl;
cin >> num1;
cout << "Enter the second number: " << endl;
cin >> num2;
cout << "What operation would you like to use?" << endl << endl;
cout << "Press [A] if you want to use Addition." << endl;
cout << "Press [S] if you want to use Subtraction." << endl;
cout << "Press [M] if you want to use Multiplication." << endl;
cout << "Press [D] if you want to use Division." << endl;
switch(input)
{
case 'A':
case 'a':
{
answer=num1+num2;
cout << "This is the sum of your equation: " << answer << endl;
break;
}
case 'S':
case 's':
{
answer=num1-num2;
cout << "This is the difference of your equation: " << answer << endl;
break;
}
case 'M':
case 'm':
{
answer=num1*num2;
cout << "This is the product of your equation: " << answer << endl;
break;
}
case 'D':
case 'd':
{
answer=num1/num2;
cout << "This is the quotient of your equation: " << answer << endl;
break;
}
default:
{
cout << "Invalid Operation..." << endl;
break;
}
}
cout << "Do you want to go again? (Y/N) " << endl;
cin >> choice;
}
cout << "See you later." << endl;
return 0;
}
So I just started college about a month and a half ago, and I thought that I'd try out the codes that still hasn't been taught to us. But I ran into a problem, whenever I build my program it shows no error. But it does not do what I had intended it to do, to be a calculator. It immediately jumps to, "Do you want to go again?" After inputting the 2 numbers and it won't let the user even choose an operation let alone calculate it. What seems to be wrong with my code?
[EDIT]
I forgot to put cin >> input; right after asking for which operation to use.
As the comments suggest, you need to get a value for your input variable at some point. I would suggest immediately before the switch that depends on it:
cin >> input; // You forgot to put this line in, I think!
switch(input)
{
...
If you increase the warning level on your compiler, for example using -Wall for GCC, you get a useful warning explaining your problem:
<source>: In function 'int main()':
<source>:8:19: warning: 'input' may be used uninitialized in this function [-Wmaybe-uninitialized]
8 | char choice = 'Y',input;
| ^~~~~
Compiler returned: 0

Constexpr Errors [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
Okay I fixed the errors. Thank you guys. But now when I run it, I choose D in the menu but only "You chose to split the words & remove the duplicates in the paragraph" & "This is it:" prints out. It doesn't show anything after that ... Anyone know what it could be? Thank you in advance!!
This is how it should be:
When the 4th choice (“Split Words”) is selected, the words should be put into an array or a structure of your and each word should be displayed with a loop. After this duplicate removal should be performed and the program must determine the duplicate words and eliminate them. After this, the word list should be printed again
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
#include <sstream>
#include <set>
using namespace std;
int main()
{
string s;
char selection;
string w;
string buf;
cout << "Enter a paragraph or a sentence : " ;
getline(cin, s);
int sizeOfString = s.length();
//cout << "The paragraph has " << sizeOfString << " characters. " << endl; ***Dummy call to see if size works.
//cout << "You entered " << s << endl; *** Dummy function !!
cout << "" << endl;
cout << " Menu " << endl;
cout <<" ------------------------" << endl;
cout << "" << endl;
cout << "A -- Convert paragraph to all caps " << endl;
cout << "B -- Convert paragraph to all lowercase " << endl;
cout << "C -- Delete whitespaces " << endl;
cout << "D -- Split words & remove duplicates " << endl;
cout << "E -- Search a certain word " << endl;
cout << "" << endl;
cout << "Please select one of the above: " ;
cin >> selection;
cout << "" << endl;
stringstream ss(s);
set<string> tokens;
switch (selection) //Switch statement
{
case 'a':
case 'A': cout << "You chose to convert the paragraph to all uppercase" << endl;
cout << "" << endl;
for(int i=0; s[i]!='\0'; i++)
{
s[i]=toupper(s[i]);
}
cout << "This is it: " << s << endl;
break;
case 'b':
case 'B': cout << "You chose to convert the paragragh to all lowercase" << endl;
cout << "" << endl;
for (int i=0; s[i] !='\0'; i++)
{
s[i]=tolower(s[i]);
}
cout << "This is it: " << s << endl;
break;
case 'c':
case 'C': cout << "You chose to delete the whitespaces in the paragraph" << endl;
cout << "" << endl;
for(int i=0; i<s.length(); i++)
if(s[i] == ' ') s.erase(i,1);
cout <<"This is it: " << s << endl;
break;
case 'd':
case 'D': cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
cout << "" << endl;
// Insert the string into a stream
// Create vector to hold our words
while (ss >> buf)
tokens.insert(buf);
cout << "This is it: " << endl;
for (set<string>::iterator it = tokens.begin(); it != tokens.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
break;
case 'e':
case 'E': cout << "You chose to search for a certain word in the paragraph. " << endl;
cout << "" << endl;
cout << "Enter the word you want to search for: ";
cin >> w;
s.find(w);
if ( s.find( w ) != std::string::npos )
{
cout << w << " was found in the paragraph. " << endl;
}
else
{
cout << w << " was not found in the paragraph. " << endl;
}
}
return 0;
}
1)
set<s>::iterator
should be
set<string>::iterator
2) Add brackets around the case statements for the local variables.
case 'D':{
}
break;
case 'e':
case 'E':{
}
break;
You are creating stringstream with empty string, which, in turn leads to stream being empty. Consider creating stringstream object after you actually read the string. stringstream doesn't hold a reference to your string object, so any modification to a string the stringstream was based on, doesn't reflect those changes in the stream.