What seems to be wrong in my C++ calculator? [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
#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

Related

cin input not being accepted in while loop [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 11 months ago.
Improve this question
Hi I'm trying to use a cin input but on my second while loop, the inputs are not running though correctly. How would I go about this?
Here's what I have:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using std::endl;
using std::cout;
using std::cin;
using namespace std;
int a, A;
int main() {
int choice, num;
cout << "State Search" << endl;
cout << "" << endl;
cout << "1. Enter the first letter of your desired state" << endl;
cout << "2. Press 2 to Quit" << endl;
cout << "" << endl;
bool done = false;
while(!done) {
//user inputs desired choice
cin >> choice;
if(choice == a || choice == A)
{
cout << "Which State? (enter a number)" << endl;
cout << "1. Alabama" << endl;
cout << "2. Alaska" << endl;
cout << "3. Arizona" << endl;
cout << "" << endl;
while (!(cin >> num))
{
if(num == 1)
{
cout << "test output 1" << endl;
}
else if (num == 2)
{
cout << "test output 2" << endl;
}
else if (num == 3)
{
cout << "test output 3" << endl;
}
} break;
} break;
}
return 0;
}
Once the user chooses one of the above states using a number I want to display the chosen output.
Here is a cleaned up version of what you are trying to do with some inline comment where you were going wrong.
// You only need to include iostream in your example program
// Only include what you need
#include <iostream>
#include <limits>
// You do not need to include all of the std namespace in the local scope
// if you are using the things you want.
using std::endl;
using std::cout;
using std::cin;
using std::streamsize;
int main()
{
cout << "State Search\n\n";
cout << "1. Enter the first letter of your desired state\n";
cout << "2. Press 2 to Quit\n" << endl;
// Removed all the while(!done) / break; as it is just noise in the current iteration of your program.
// if you use an int for the choice variable, you will actually try to parse the input
// as an integer and return 0 if the user inputs 'a'
// you need to use a char for what you want to do
char choice;
cin >> choice;
// Also your a and A int value do not make sense as they are init to 0
// (global initialization), I guess what you want to do is compare to the char 'a' or 'A'. You could also transform the input to lowercase and compare only to 'a'.
if (choice == 'a' || choice == 'A')
{
// Use \n not std::endl as it will flush the output buffer
// all the time. std::endl is a false friend, use it only
// when you want to present your text to the user.
cout << "Which State? (enter a number)\n";
cout << "1. Alabama\n";
cout << "2. Alaska\n";
cout << "3. Arizona\n" << endl;
// We want to wait until we get a correct input and _then_
// do something with our input.
int num;
while (!(cin >> num))
{
// You might want to say something to your user if he doesn't input a correct number here.
// This will succeed if the user inputs 4, so you might also want to handle that case differently and ask the user to retry too in that case.
// The stream is now in error, you need to reset the state of the stream by clearing the error and emptying the buffer.
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
}
// Just showing another way of doing the if / else if dance in C/C++.
switch(num)
{
case 1: cout << "You chose Alabama" << endl; break;
case 2: cout << "You chose Alaska" << endl; break;
case 3: cout << "You chose Arizona" << endl; break;
default: cout << "I do not recognize the State" << endl; break;
}
}
// if we didn't receive 'a' or 'A' we just quit the program.
return 0;
}

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.

While loop doesn't exit even with false conditions applied [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
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')

How to check If cin buffer contains the same value for 0.5sec (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 5 years ago.
Improve this question
I want to make a program so that when I type in 1 it will print go ahead infinite times and like that 2-go left 3-go right. And when I enter 0 it should stop everything and print !!!stop!!!
I have tried many things but I can't even make the 1 command work. Please help.
#include "stdafx.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int value;
cout << "Would you please enter the command " << endl;
cout << " _______________________" << endl;
cout << " 1- go ahead |" << endl;
cout << " _______________________" << endl;
cout << " 2- make a left turn |" << endl;
cout << " _______________________" << endl;
cout << " 3- make a right turn |" << endl;
cout << " _______________________" << endl;
cout << " 4- go back |" << endl;
cout << " _______________________" << endl;
cout << " 0- stop everything |" << endl;
cout << " _____________________________________________________" << endl;
cin >> value;
switch (value)
{
case 1:
while (int value == 1)
{
cout << " * go ahead " << endl;
if (value == 0)
{
break;
}
}
break;
case 2:
cout << " * turn left " << endl;
break;
case 3:
cout << " * turn right " << endl;
break;
case 4:
cout << " * reverse " << endl;
break;
case 0:
cout << " !!!Stop!!! " << endl;
break;
}
return 0;
}
for(value==1) is no valid C++ syntax and even if it would be, your code would print "go ahead" infinitely after 1 has been entered.
Besides that, your switch cases are checking for ASCII 1 instead of numeric 1. Change '1' to 1.
You need to surround your input switch statement with some kind of loop like this:
int value = 0;
do{
cin >> value;
switch(value){
case 1:
cout << "go ahead" << endl;
break;
case 2:
cout << "turn left" << endl;
break;
case 3:
cout << "turn right" << endl;
break;
case 4:
cout << "reverse" << endl;
break;
case 0:
cout << "stop" << endl;
break;
}
}while(value!=0);
This Code will ask for input and print the direction afterwards.
You have three problems. Firstly, '1' is a character literal and 1 is an integer literal (and I know of no implementation where they have the same value). You need to get rid of the quotes.
Secondly, for (int value == 1) is not valid C++. You need to change this to while (value == 1).
Finally, and most significantly, nothing inside the loop will allow value to change. It will be an infinite loop (and hence undefined behaviour). You will need to run the indefinite loop on one thread, and do input in a loop on the other thread.
(Also, please don't use using namespace std;)
remove '' in case statements
switch (value)
{
case 1:
for (value == 1)
{
cout << " * go ahead " << endl;
if (value == 0)
{
break;
}
}
break;
case 2:
cout << " * turn left " << endl;
break;
case 3:
cout << " * turn right " << endl;
break;
case 4:
cout << " * reverse " << endl;
break;
case 0:
cout << " !!!Stop!!! " << endl;
break;
}

Do While Loop menu in C++ [closed]

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'm having trouble with this do-while loop menu for a program I'm working on for school. I've checked, and as far as I'm concerned I have written the code correctly. However, when testing, if I type 'y' or 'n' the result is the same: the menu streaming down 100's of times non stop until I exit the program. Any idea on what I'm doing wrong and how I can get it to display the menu properly every time? Thanks in advance.
#include <iostream>
#include <iomanip>
#include <string>
#include "CashRegister.h"
#include "InventoryItem.h"
using namespace std;
int main()
{
// Variables
int selection, numUnits, cont;
double price;
// Use the first constructor for the first item
InventoryItem item1;
item1.setCost(5.0);
item1.setDescription("Adjustable Wrench");
item1.setUnits(10);
// Use the second constructor for the second item
InventoryItem item2("Screwdriver");
item2.setCost(3.0);
item2.setUnits(20);
// Use the third constructor for the remaining items
InventoryItem item3("Pliers", 7.0, 35);
InventoryItem item4("Ratchet", 10.0, 10);
InventoryItem item5("Socket Wrench", 15.0, 7);
do
{
cout << "#\t" << "Item\t\t\t" << "qty on Hand" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "1\t" << item1.getDescription() << "\t" << setw(3) << item1.getUnits() << endl;
cout << "2\t" << item2.getDescription() << "\t\t" << setw(3) << item2.getUnits() << endl;
cout << "3\t" << item3.getDescription() << "\t\t\t" << setw(3) << item3.getUnits() << endl;
cout << "4\t" << item4.getDescription() << "\t\t\t" << setw(3) << item4.getUnits() << endl;
cout << "5\t" << item5.getDescription() << "\t\t" << setw(3) << item5.getUnits() << endl;
cout << "Which item above is being purchased? ";
cin >> selection;
// Validate the selection
while (selection < 1 || selection > 5)
{
cout << "Error, please make a valid item selection: ";
cin >> selection;
}
cout << "How many units? ";
cin >> numUnits;
// Validate the quantity of units to make sure it isn't a negative value
while (numUnits < 0)
{
cout << "Error, please enter a valid quantity: ";
cin >> numUnits;
}
// Use a switch statement to figure out which cost to pull
switch (selection)
{
case 1: {price = item1.getCost();
item1.changeUnits(numUnits); }
break;
case 2: {price = item2.getCost();
item2.changeUnits(numUnits); }
break;
case 3: {price = item3.getCost();
item3.changeUnits(numUnits); }
break;
case 4: {price = item4.getCost();
item4.changeUnits(numUnits); }
break;
case 5: {price = item5.getCost();
item5.changeUnits(numUnits); }
break;
}
// Create a CashRegister object for this particular selection
CashRegister transaction(price, numUnits);
// Display the totals
cout << fixed << showpoint << setprecision(2);
cout << "Subtotal: $" << transaction.getSubtotal() << endl;
cout << "Sales Tax: $" << transaction.getSalesTax() << endl;
cout << "Total: $" << transaction.getPurchaseTotal() << endl;
// Find out if the user wants to purchase another item
cout << "Do you want to purchase another item? Enter y/n: ";
cin >> cont;
} while (cont != 'n' && cont != 'N');
system("pause");
return 0;
}
Your loop will never break unless you explicitly enter 110 which is the 'n' char in ASCII Codes or 78 which is the 'N'. So change your cont declaration from int cont; to char cont; and then you won't get the infinite loop anymore, and its condition will be valid to possibly break by then unless you have another hidden logical error which will require you to debug it.