I built a simple calculator out of c++, it uses chars, so (+,-*,/) will works as math operators but when a user inputs an '=' it does't work.
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
#define PI 3.14159265359
#define NEWLINE '\n'
void printf(string string)
{
cout << string;
}
int main ()
{
char operation;
double a,b,c, value;
double answer = 0;
bool more = true;
cout << "Welcome to My Calculator\n\nInput a value: ";
cin >> answer;
operations:
cin >> operation;
if (operation != '=') {
if (operation == '+') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer += value;
cout << "Equals " << answer << "\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '-') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer -= value;
cout << "Equals " << answer << "\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '*') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer *= value;
cout << "Equals " << answer << "\n\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '/') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer /= value;
cout << "Equals " << answer << "\n\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '^') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer = pow(answer, value);
cout << "Equals " << answer << "\n\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '=') {
cout << "\nFinal Answer = " << answer << "\n\nNew operation [yes/no]: ";
string check;
cin >> check;
if (check == "yes") {
cout << "\nInput value: ";
cin >> answer;
cout << "\n";
goto operations;
} else {
cout << "\nGoodbye for now...\n";
return 0;
}
}
} else {
cout << "Unknown Error! Program Closing...";
return 0;
}
return 0;
}
When a user uses any operation besides = it works perfectly, but if I use and equals sign it doesn't work.
Example program out put:
Welcome to My Calculator
Input a value: 4
+4
4 + 4
Equals 8
8 - New Operation? - 3
8 - 3
Equals 5
5 - New Operation? * 5
5 * 5
Equals 25
25 - New Operation? /2
25 / 2
Equals 12.5
12.5 - New Operation? ^2
12.5 ^ 2
Equals 156.25
156.25 - New Operation? =
Unknown Error! Program Closing...
if (operation != '=') {
...
if (operation == '=') {
}
}
If operation isn't equal to "=" and if it's equal to "=". I think you planned to put a closure operator or something like that in the first outer if.
your statement if (operation != '=') causes the control to be transferred to the else statement
Because you have an if (operation != '=') outside the if checking for =. You don't want to do that.
Related
Trying to let only numbers be a vaild input for a quadratic equation solver. I used bool and broke my code and no matter the input it just gives me my error message even if it is a vaild input.
After the progarm ask you to confirm the coefficients are correct, even if you put Y you get the " Please input a number. Please try again". how do you fix this?
I added the bool becasue that what my teacher showed me to use for checking inputs, very new to c++ and coding. I just added the whole code, the HW was to update HW 5 to do a few things more. I broke it trying to check inputs, when adding bool.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string first, last;
double a, b, c, x1, x2, discriminant, realpart, imaginarypart;
char ch;
cout << "Please enter First and Last name," << endl;
cout << " " << endl;
cout << "First Name= ";
cin >> first;
cout << "and" << endl;
cout << "Last Name=";
cin >> last;
cout << " " << endl;
cout << " Hello " << first << " " << last << " "
<< "welcome." << endl;
cout << " " << endl;
bool isCorrect{ true };
do {
start:
isCorrect = true;
cout << "Enter the coefficients of a: ";
cin >> a;
cout << " " << endl;
cout << "Enter the coefficients of b: ";
cin >> b;
cout << " " << endl;
cout << "Enter the coefficient of c: ";
cin >> c;
cout << " " << endl;
cout << " A = " << a;
cout << " B = " << b;
cout << " C = " << c << endl
<< endl;
cout << " Confirm the coefficients value are correct or not (y/n): " << endl;
cout << " " << endl;
cin >> ch;
if (ch == 'n' || ch == 'N')
goto start;
cout << " " << endl;
discriminant = b * b - 4.0 * a * c;
if (cin.fail())
;
{
isCorrect = false;
cin.clear();
cin.ignore(245, '\n');
cout << " Please input a number. Please try again" << endl;
}
} while (!isCorrect);
bool isExit(false);
if (a == 0) {
cout << " " << endl;
cout << "Error message: a can not = zero (0) " << endl;
}
else if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2.0 * a);
x2 = (-b - sqrt(discriminant)) / (2.0 * a);
cout << "Roots are real and different." << endl;
cout << " " << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 =" << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
cout << " " << endl;
x1 = -b / (2.0 * a);
cout << "x1 = x2 ="
" "
<< x1 << endl;
}
else {
//cout << "Error message: No real solution exist." << endl; // Part 1 error code for no real solutions
realpart = -b / (2.0 * a); //Code for part 2
imaginarypart = sqrt(-discriminant) / (2.0 * a); // Code for part 2
cout << "Roots are complex and different." << endl; // Code for part 2
cout << " " << endl;
cout << "x1 = " << realpart << "+" << imaginarypart << "i" << endl; // Code for part 2
cout << "x2 = " << realpart << "-" << imaginarypart << "i" << endl; // Code for part 2
}
cout << " " << endl;
cout << " Would you like to solve another quadratic equation (y/n): " << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
goto start;
else
(ch == 'n' || ch == 'N');
return 0;
}
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.
My program will compile but I'm running into a couple problems. My first cout statement requiring e/E to end works, but in my second while loop where I state (+ || - || * || /) won't run. +/-/*// returns "Operation type invalid". Can you guys help me see my error?
First sentinel loop, just learning loops:
#include <iostream>
using namespace std;
int main()
{
int numOne;
int numTwo;
int result;
string operation;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation == "e" || "E")
{
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
while (operation == "+" || operation == "-" || operation == "*" || operation == "/")
{
cout << "Please enter integer one: " << endl;
cin >> numOne;
cout << "Please enter integer two: " << endl;
cin >> numTwo;
if (operation == "+")
{
result = numOne + numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "-")
{
result = numOne - numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "*")
{
result = numOne * numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "/")
{
if (numTwo == 0)
{
cout << "You cannot divide by zero!" << endl;
}
else
{
result = numOne / numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
}
}
return 0;
}
while (operation == "e" || "E")
Here you are comparing one of two conditions:
Does operation == "e"?
If not, is "E" a valid pointer?
That second condition is your problem: "E" is of course a valid pointer, so that condition will always be true. Always. Notice that in the second condition, you are not comparing operation to "E".
You are forever stuck here:
while (operation == "e" || "E")
{
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
You simply need to have:
while (operation == "e" || operation == "E")
This is probably just a typo or an oversight more than anything else.
I recently developed a calculator, this is the code:
/*
*All 4 operations + percentage finder + Raise to power and more....
* by Ulisse
* ulissebenedennti#outlook.com
* Feel free to take some parts of this code an put them
* in yours, but do not take all the code and change/delete
* the comments to take the credit, trust me, it doesn't
* gives the satisfaction you expect.
*/
#include <iostream> //For cin and cout
#include <iomanip> //For setprecision()
#include <windows.h> //For SetconsoleTitle()
#include <stdlib.h> //For system()
#include <cmath> //For pow()
#include <cctype> //For isdigit()
using namespace std;
int main(){
reset:
system("cls"); //Screen cleaner
system("color 0f");
SetConsoleTitle("Calculator by Ulisse");//Setting window title
char op; //Start of the variables declaration
double a, b, ra;
string p, ms, d, me, e;
p = " + ";
ms = " - ";
d = " : ";
me = " x ";
e = " = "; //End of the variable declaration
cout << "Type now '?' for help page, or another character to continue." << endl;
cin >> op;
if (op == '?'){
help:
system("cls");
cout << "Write the whole operation.\nEXAMPLE: 2 ^ 3 \n OUTPUT: 2 ^ 3 = 8"<< endl;
cout << "(+) Sum between a and b\n(-) Subtraction between a and b" << endl;
cout << "(^) Raise to power\n(%)finds the a% of b\n(x or *)Multiplicate a by b" << endl;
cout << "(: or /) Divide a by b" << endl;
system("pause");
system("cls");
goto start;
}
else{
system("cls");
while(1){
start:
cout << "CALC> ";
cin >> a;
cin >> op;
cin >> b;
//The four operations
if (op == '+'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << p << b << e << a + b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '-'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << ms << b << e << a - b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '*' || op == 'x'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << me << b << e << a * b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '/' || op == ':'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << d << b << e << a / b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '%'){
cout << "RESULT" << endl;
cout << setprecision(999) << "The " << a << "% of " << b << " is " << b / 100 * a << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '^'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " ^ " << b << " = " << pow (a, b) << endl;
cout << "________________________________________________________________________________" << endl;
}
//Some useful functions
if (op == 'c'){
system("cls");
}
if (op == '?'){
system("cls");
goto help;
}
if (op == 'r'){
goto reset;
}
if (op == 'b'){
system("color 0c");
Beep(400,500);
cout << "CLOSING, ARE YOU SURE?(y/n)";
system("color 0c");
cin >> op;
if(op == 'y'){
cout << "Closing..." << endl;
system("cls");
system("color 0f");
system("pause");
break;
}
if(op == 'n'){
goto start;
}
}
if (op == '<'){
if (a < b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " < " << b << e << " TRUE " << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " < " << b << e << " FALSE " << endl;
cout << "________________________________________________________________________________" << endl;
}
}
if (op == '>'){
if (a > b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " > " << b << e << "TRUE" << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " > " << b << e << "FALSE" << endl;
cout << "________________________________________________________________________________" << endl;
}
}
if (op == '='){
if (a == b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " = " << b << " is TRUE" << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " = " << b << " is FALSE" << endl;
cout << "________________________________________________________________________________" << endl;
}
}
}
}
}
This is how it works:
You write a number, then an operator(like +, - plus other functions...) and it makes the operation between the two numbers you typed depending o what is the typed operato, so if you type 4 + 3 it will output 4 + 3 = 7.
Now that you understand how it works, let us go to the qyestion...
Is there an indentifier for a number or a character? When you type a sttring or a character when you cin >> (not a number variable) the application will start printing out characters that you did never inserted:
Input
I think like this(console output) will be printed out(until you dont close the process.
So i would like to prevent the applicatin for failing when you type an invalid input for the variable and making it executes another instruction, here's what i mean:
if(anumbervariable != number || anumbervariable == string){
cout << "invalid input" << endl;
}
This isn't a real working code, it's just a representation of what i mean, or i wouldn't came hre to make you waste you lives :)
Thanks in advance.
You can do something like follows
int getNumber(){
int x;
cin >> x;
while(cin.fail()){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "invalid input"<<endl;
cin >> x;
}
return x;
}
If you want to do a character by character thing, C++ has an isalpha() function, so you can use !isalpha(). The numeric limits thing is the max buffer that can be taken before a new line. If you print it out, it's just some large number so that it can ignore that amount of input.
I'm solving some C++ problems from ebooks. I made this C++ program but it isn't working properly. I've 2 problems:
Even after applying the forumla (votePercentage = firstAnswer/totalVotes*100;) it isn't showing the output, but only 0.
The program should display the bar chart, how am I suppose to do that? Any hints, reference or solution will be appreciated.
Here is my code:
/*
* Write a program that provides the option of tallying up the
* results of a poll with 3 possible values.
* The first input to the program is the poll question;
* the next three inputs are the possible answers.
* The first answer is indicated by 1, the second by 2, the third by 3.
* The answers are tallied until a 0 is entered.
* The program should then show the results of the poll—try making
* a bar graph that shows the results properly scaled to fit on
* your screen no matter how many results were entered.
*/
#include <iostream>
#include <string>
void startPoll (void);
void showPoll (void);
void pollCheck (void);
std::string pollQuestion, answer1, answer2, answer3;
int pollChoice, firstAnswer, secondAnswer, thirdAnswer;
int main (void)
{
int totalVotes = 1;
float votePercentage;
startPoll();
showPoll();
for(;;totalVotes++)
{
if (pollChoice == 1)
{
firstAnswer = firstAnswer + 1;
}
else if (pollChoice == 2)
{
secondAnswer++;
}
else if (pollChoice == 3)
{
thirdAnswer++;
}
else
{
std::cout << "==============*======*======*==============\n"
<< " RESULT \n"
<< "==============*======*======*==============\n"
<< "Question: " << pollQuestion << "\n"
<< "Total Votes: " << totalVotes << "\n";
votePercentage = (firstAnswer/totalVotes)*100;
std::cout << answer1 << ": " << firstAnswer << " votes. | " << votePercentage << "\n";
votePercentage = secondAnswer/totalVotes*100;
std::cout << answer2 << ": " << secondAnswer << " votes. | " << votePercentage << "\n";
votePercentage = thirdAnswer/totalVotes*100;
std::cout << answer3 << ": " << thirdAnswer << " votes. | " << votePercentage << "\n";
return 0;
}
std::cout << "\nEnter your vote again\nOR\nuse 0 to show the results.\n";
std::cin >> pollChoice;
}
std::cout << "Error: Something went wrong!\n";
}
void startPoll (void)
{
std::cout << "Enter your poll question:\n";
getline (std::cin, pollQuestion, '\n');
std::cout << "Enter answer 1:\n";
getline (std::cin, answer1, '\n');
std::cout << "Enter answer 2:\n";
getline (std::cin, answer2, '\n');
std::cout << "Enter answer 3:\n";
getline (std::cin, answer3, '\n');
}
void showPoll (void)
{
std::cout << "==============|======|======|==============\n"
<< " POLL \n"
<< "==============|======|======|==============\n"
<< pollQuestion << "\n"
<< "1. " << answer1 << "\n"
<< "2. " << answer2 << "\n"
<< "3. " << answer3 << "\n\n"
<< "Enter 1,2 or 3:\n\n";
std::cin >> pollChoice;
pollCheck();
}
void pollCheck (void)
{
if (pollChoice != 1 && pollChoice != 2 && pollChoice != 3)
{
std::cout << "Wrong choice entered! Please try again.\n\n";
return showPoll();
}
}
You need to take care that integer/integer = integer. In your case, changing
(firstAnswer/totalVotes)*100
to
(1.0*firstAnswer/totalVotes)*100
or
(firstAnswer*100.0/totalVotes)
should work. They all give a floating point result.
Well, the solution for the Bar Chart could be the following:(Not written by me) I think thats very self explaining because its really basic
void line (int n, char c)
{
// this is the loop for n
for (int i = 0; i < n; i++)
cout << c << endl;
}
Here is my solution, you can see how I made the bars work by reading the comments.
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
int c = 0;
cout << "What is your favorite animal? 1 Cat, ";
cout <<"2 Dog, 3 Fish, 0 Count votes" << endl;
//Choice counter
while (true)
{
int choice;
cout << "Choice: ";
cin >> choice;
if(choice == 1)
a++;
else if(choice == 2)
b++;
else if(choice == 3)
c++;
else if(choice == 0)
break;
else
continue;
}
cout << endl << " 1: " << a << endl;
cout << " 2: " << b << endl;
cout << " 3: " << c << endl;
cout << endl << "1\t" << "2\t" << "3\t" << endl;
//Finds the max voted option
int max = 0;
if(a > b && a > c)
max = a;
else if(b > c && b > a)
max = b;
else if(c > a && c > b)
max = c;
/* If the max voted option is bigger than 10, find by how much
we have to divide to scale the graph, also making 10 bar
units the max a bar can reach before scaling the others too */
int div =2;
if(max > 10)
{
do
{
max = max/div;
if(max < 10)
break;
div++;
}while(true);
}else
div = 1;
//Sets the final number for the bars
a=a/div;
b=b/div;
c=c/div;
if(a==0)
a++;
if(b==0)
b++;
if(c==0)
c++;
//Creates the bars
while(true)
{
if(a>0)
{
cout << "[]" << "\t";
a--;
}else
cout << " ";
if(b>0)
{
cout << "[]" << "\t";
b--;
}else
cout << " ";
if(c>0)
{
cout << "[]" << "\t";
c--;
}else
cout << " ";
cout << endl;
if(a==0 && b==0 && c==0)
break;
}
}