C++ Calculator Prog. Issue - c++

If anyone has some suggestions - I could utilize the help. The program compiles, but does not
compute properly. I have tried numerous reitterations and I am at an impase. Any suggestions?
I have to initialize all these variables? Thank you....
//This program mimics a calculator
//*****************************************
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double firstint, secint, ansrlt, ansrlt2, ansrlt3, ansrlt4;
char operat;
cout << fixed << showpoint;
cout << setprecision(4);
cout << "This program mimics a standard calculator and outputs a result accurate to 4 decimal
places." <<endl;
cout << '\n';
cout << "Please enter 1st Integer to be calculated: ";
cin >> firstint;
cout << '\n';
cout << "Please enter operation to be performed in the form of *, /, +, - : ";
cin >> operat;
cout << '\n';
cout << "Please enter 2nd Integer to be calculated: ";
cin >> secint;
cout << '\n';
if (operat == '/' && secint == 0)
cout << "Division by 0 not allowed enter again." << endl;
cin.get();
if (operat == '*')
ansrlt = firstint * secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl;
cin.get();
if (operat == '/')
ansrlt2 = firstint / secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt2 << endl;
cin.get();
if (operat == '+')
ansrlt3 = firstint + secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt3 << endl;
cin.get();
ansrlt4 = firstint - secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt4 << endl;
cin.get();
return 0;
}

Firstly, you should explain what you do, what happens, and what you expected to happen - as that will generally make things a lot easier for
anyone reading your question.
However, you're missing {} around the body of your if's everywhere.
e.g. it should be
if (operat == '*') {
ansrlt = firstint * secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl;
}
(And get rid of the cin.get() calls, unless you're using them for debugging purposes)

Related

Incorrect user input

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;
}

Creating a Simple Calculator, having an issue with the addition

I'm trying to create a simple calculator and i already encountered an issue when addition is being used. I created a function for addition and whenever i pass in two values i get a different answer. For Example when i add 4,5 i would expect to get 9 but the answer i get is 0029144C . Im still a beginner, so at first i wasn't sure if using type bool for the adding function would affect my result, but i changed it to type float and still getting the same result (in case anyone asks).
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void SimCalcMenu();
void additionSign();
bool makeSum(float num1, float num2);
int main() {
float firstNum, SecondNum;
char operationLetter;
SimCalcMenu();
cout << " Please Select an Operation You Would Like to Perform ";
cin >> operationLetter;
if (operationLetter == 'a' || operationLetter == 'A')
{
additionSign();
cout << " Enter the First Number : ";
cin >> firstNum;
cout << " Enter the Second Number: ";
cin >> SecondNum;
makeSum(firstNum, SecondNum);
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum << endl;
}
else
{
cout << " Error ";
}
return 0;
}
void SimCalcMenu() {
cout << "------------------------------------------------------------------------------" << endl;
cout << " WELCOME TO SIM CALCULATOR " << endl;
cout << "------------------------------------------------------------------------------" << endl;
cout << endl;
cout << " Please Select an Operation : " << endl;
cout << " A.) Addition " << endl;
cout << " B.) Subtraction " << endl;
cout << " C.) Multiplication " << endl;
cout << " D.) Division " << endl;
cout << " E.) Roots ( Only Positive Number)" << endl;
cout << " F.) Power ( Only Positive Number " << endl;
cout << " G.) Percentage " << endl;
cout << " H.) Display functions execution " << endl;
cout << " I.) Quit " << endl;
cout << "------------------------------------------------------------------------------" << endl;
}
void additionSign() {
cout << "------------------------------------------------------------------------------" << endl;
cout << " ADDITION " << endl;
cout << "------------------------------------------------------------------------------" << endl;
}
bool makeSum(float num1, float num2) {
float totSum;
totSum = num1 + num2;
return totSum;
}
makeSum() should return float, because you are returning the sum of two floats.
You are not getting the right result because you are printing makeSum, which is the address of the function. You want to print the value of makeSum(firstNum, SecondNum).
this line
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum << endl;
IS 'printing' 'makesum', makesum is a function so its printing the address of makesum
you need
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum(firstNum, SecondNum) << endl;
now at least it will print the result of makesum. As other have pointerd out that function is wrong (it returns a bool).
should be
float makeSum(float num1, float num2) {
float totSum;
totSum = num1 + num2;
return totSum;
}

Basic C++ Application has extra output then it should

So I'm writing a basic application and for some reason when I run the program a bunch of numbers pop up before my intended output. It was working fine until I added the "std::cout" lines to have the outputs only be 2 decimals. The general gist of the application is a program acts as a self-checkout register at a store and lets the user buy 2 items. And yes I know the code probably looks really bad, I'm still super new to C++.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float price1;
float number1;
float price2;
float number2;
float priceofitemplustax1;
float priceofitemplustax2;
float total;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2;
std::cout << total;
cout << endl << "Please scan your first item." <<endl;
cin.get();
cout << endl << "How many of that item are you buying? "<<endl;
cin >> number1;
cout << endl << "How much is that item?"<<endl;
cin >> price1;
priceofitemplustax1 = (number1 * price1) * 1.0875;
cout << endl << "So you want " << number1 << " of this item? Adding tax that will be " << priceofitemplustax1 << "."<<endl;
cin.get();
cout << endl << "Please scan your second item."<<endl;
cin.get();
cout << endl << "How many of that item are you buying? "<<endl;
cin >> number2;
cout << endl << "How much is that item?"<<endl;
cin >> price2;
priceofitemplustax2 = (number2 * price2) * 1.0875;
cout << endl << "So you want " << number2 << " of this item? Adding tax that will be " << priceofitemplustax2 << "."<<endl;
cin.get();
total = priceofitemplustax1 + priceofitemplustax2;
cout << endl << "So your final total for this shopping trip including tax is " << total << "."<<endl;
cin.get();
cout << endl << "Your reciept will print below."<<endl;
cin.get();
cout << setw(14) << right << "Number of Item" << setw(10) << right << "Price" << setw(20) << "Price plus tax" << endl;
cout << setw(14) << right << number1 << setw(10) << right << price1 << setw(20) << priceofitemplustax1 << endl;
cout << setw(14) << right << number2 << setw(10) << right << price2 << setw(20) << priceofitemplustax2 << endl;
cout << endl;
cout << endl;
cout << setw(8) << right << "Total is" << setw(10) << total << price2 << endl;
cin.get();
}
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2; std::cout << total;
here you write 5 floats
The lines
std::cout << std::fixed; // sets a format
std::cout << std::setprecision(2); // sets a format
set the streams output format.
The lines
std::cout << price1; // outputs a number
std::cout << price2; // outputs a number
std::cout << priceofitemplustax1; // outputs a number
std::cout << priceofitemplustax2; // outputs a number
std::cout << total; // outputs a number
print the variables to the stream.
Just remove the variable output lines. Do not accept this answer - Credit goes to manni66

Program closes after user input, no reason it should

sorry, I'm very new and have to hurry, so I just wanted to put this up. The code randomly ends after trying the first input (choosing what topic, algebra, basic mathematics, etc.) I've tried other sources, tried reformatting and still don't know what it is. I'm fairly new, so I assume it's just a stupid issue that I overlooked.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
int calculatorChoice, choiceMath1;
int addition1, addition2, additionSum;
int subtraction1, subtraction2, subtractionDifference;
int multiplication1, multiplication2, multiplicationProduct;
int division1, division2, divisionQuotient;
cout << "What would you like to figure out?" << endl;
cout << "" << endl;
cout << "[1]: Basic Mathematic Equations" << endl;
cout << "[2]: Figuring out a Grade" << endl;
cout << "[3]: Algebra" << endl;
cout << "[4]: Inquiry Sciences" << endl;
cout << "[5]: Unit Conversion" << endl;
cin >> calculatorChoice;
if (calculatorChoice == 1) {
cout << "What would you like to do?" << endl;
cout << "" << endl;
cout << "[1]: Addition" << endl;
cout << "[2]: Subtraction" << endl;
cout << "[3]: Multiplication" << endl;
cout << "[4]: Division" << endl;
cin >> choiceMath1;
if (choiceMath1 == 1) {
cout << "Choose your first number." << endl;
cin >> addition1;
cout << "Choose your second number." << endl;
cin >> addition2;
additionSum = addition1 + addition2;
cout << "The sum of " << addition1 << " and " << addition2 << " is " << additionSum << "." << endl;
}
else if (choiceMath1 == 2) {
cout << "Choose the first number." << endl;
cin >> subtraction1;
cout << "Choose the second number." << endl;
cin >> subtraction2;
subtractionDifference = subtraction1 - subtraction2;
cout << "The difference of " << subtraction1 << " and " << subtraction2 << " is " << subtractionDifference << "." << endl;
}
else if(choiceMath1 == 3) {
cout << "Choose the first number." << endl;
cin >> multiplication1;
cout << "Choose the second number." << endl;
cin >> multiplication2;
multiplicationProduct = multiplication1 * multiplication2;
cout << "The product of " << multiplication1 << " and " << multiplication2 << " is " << multiplicationProduct << "." << endl;
}
else if(choiceMath1 == 4) {
cout << "Choose the first number." << endl;
cin >> division1;
cout << "Choose the second number." << endl;
cin >> division2;
divisionQuotient = division1 / division2;
cout << "The quotient of " << division1 << " by " << division2 << " is " << divisionQuotient << "." << endl;
}
else {
cout << "That is not a choice." << endl;
}
}
else {
}
}
void calculator() {
}
Your code doesnt have any loop, what means, after enter the input , it shows the result and program finishes. If you want to stop your code somewhere (depends on the IDE, this solution is for VS) just type system("pause")and you will see what your program shows on console. In case you are not on VS, add #include <cstdlib>
If you explain a bit more hat you expect your code to do we can help you easier.

How would I go about outputting the output from ALL of the above code?

Basically I want an exact copy of the code that appears in the console window to also be outputted to a txt file..
#include <conio.h>
#include <iostream>
#include <dos.h>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <fstream>
using namespace std;
//Initialising gotoxy Comand
void gotoxy(int col, int row)
{
COORD coord;
coord.X = col;
coord.Y = row;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
char name1[20], name2[20], name3[30], name4[20];
int funcNum = 0;
int n1Nv, n1Mv, n1SEv, n1SWv;
int n2Nv, n2Mv, n2SEv, n2SWv;
int n3Nv, n3Mv, n3SEv, n3SWv;
int n4Nv, n4Mv, n4SEv, n4SWv;
int n1Total, n2Total, n3Total, n4Total, perTotal;
double n1Per, n1PerTotal;
double n2Per, n2PerTotal;
double n3Per, n3PerTotal;
double n4Per, n4PerTotal;
double maxVote;
//Introduction
cout << "================================================================================";
cout << " Ballot Results" << endl;
cout << " Version 2.1" << endl;
cout << " Created by Team b0nkaz" << endl;
cout << "================================================================================" << endl;
//Candidate Identification
cout << "Enter the candidates running for president" << endl << endl;
//cin.getline (workaround,30); //**
cout << "Candidate One: ";
cin.getline (name1,20);
cout << "Candidate Two: ";
cin.getline (name2,20);
cout << "Candidate Three: ";
cin.getline (name3,20);
cout << "Candidate Four: ";
cin.getline (name4,20);
cout << " " << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
cout << "Input vote numbers from each region pressing enter after each input:" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
//Input Table
//Regions
gotoxy(22,19);
cout << "North" << endl;
gotoxy(31,19);
cout << "Midlands" << endl;
gotoxy(43,19);
cout << "South East" << endl;
gotoxy(57,19);
cout << "South West" << endl;
gotoxy(69,19);
cout << "| Total" << endl;
cout << "_____________________________________________________________________|__________" << endl;
gotoxy(69,21);
cout << "|";
gotoxy(69,22);
cout << "|";
gotoxy(69,23);
cout << "|";
gotoxy(69,24);
cout << "|";
gotoxy(69,25);
cout << "|";
gotoxy(69,25);
cout << "|";
gotoxy(69,26);
cout << "|";
gotoxy(69,27);
cout << "|";
gotoxy(69,28);
cout << "|";
gotoxy(69,29);
cout << "|";
//Candidates
gotoxy(0,22);
cout << name1;
gotoxy(0,24);
cout << name2;
gotoxy(0,26);
cout << name3;
gotoxy(0,28);
cout << name4;
//Equals
cout << endl;
cout << "_____________________________________________________________________|__________" << endl;
//Vote Input
//North
gotoxy(22,22);
cin >> n1Nv;
gotoxy(22,24);
cin >> n2Nv;
gotoxy(22,26);
cin >> n3Nv;
gotoxy(22,28);
cin >> n4Nv;
//Midlands
gotoxy(31,22);
cin >> n1Mv;
gotoxy(31,24);
cin >> n2Mv;
gotoxy(31,26);
cin >> n3Mv;
gotoxy(31,28);
cin >> n4Mv;
//South East
gotoxy(43,22);
cin >> n1SEv;
gotoxy(43,24);
cin >> n2SEv;
gotoxy(43,26);
cin >> n3SEv;
gotoxy(43,28);
cin >> n4SEv;
//South West
gotoxy(57,22);
cin >> n1SWv;
gotoxy(57,24);
cin >> n2SWv;
gotoxy(57,26);
cin >> n3SWv;
gotoxy(57,28);
cin >> n4SWv;
//Total Votes
//Name1
gotoxy(72,22);
n1Total = n1Nv + n1Mv + n1SEv + n1SWv;
cout << n1Total;
//Name2
gotoxy(72,24);
n2Total = n2Nv + n2Mv + n2SEv + n2SWv;
cout << n2Total;
//Name3
gotoxy(72,26);
n3Total = n3Nv + n3Mv + n3SEv + n3SWv;
cout << n3Total;
//Name4
gotoxy(72,28);
n4Total = n4Nv + n4Mv + n4SEv + n4SWv;
cout << n4Total << endl << endl << endl;
//Percentage Calculation
perTotal = n1Total + n2Total + n3Total + n4Total;
//Candidate One
n1Per = n1Total*100;
n1PerTotal = n1Per/perTotal;
//Candidate Two
n2Per = n2Total*100;
n2PerTotal = n2Per/perTotal;
//Candidate Three
n3Per = n3Total*100;
n3PerTotal = n3Per/perTotal;
//Candidate Four
n4Per = n4Total*100;
n4PerTotal = n4Per/perTotal;
cout << "Please wait for calculation..." << endl << endl;
//Spinning Loading Line
//std::cout << '-' << std::flush;
//for(;;)
//{
//Sleep(100);
//std::cout << "\b\\" << std::flush;
//Sleep(100);
//std::cout << "\b|" << std::flush;
//Sleep(100);
//std::cout << "\b/" << std::flush;
//Sleep(100);
//std::cout << "\b-" << std::flush;
//}
//Sleeping Program
Sleep(1500); //1.5 secs
//Total Output
cout << "Candidate percentage:" << endl << endl;
//Converting To One Decimal Place
cout << fixed;
std::cout.precision(1);
//Vote Percentages
cout << name1 << " = " << n1PerTotal << "%" << endl;
cout << name2 << " = " << n2PerTotal << "%" << endl;
cout << name3 << " = " << n3PerTotal << "%" << endl;
cout << name4 << " = " << n4PerTotal << "%" << endl << endl;;
//Calculating Winnner
maxVote=n1PerTotal;
if (n2PerTotal>maxVote)
maxVote=n2PerTotal;
if (n3PerTotal>maxVote)
maxVote=n3PerTotal;
if (n4PerTotal>maxVote)
maxVote=n4PerTotal;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
//Sleeping Program
Sleep(1500); //1.5 secs
if(maxVote==n1PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name1 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n1PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n2PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name2 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n2PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n3PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name3 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n3PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
else if(maxVote==n4PerTotal)
{
cout << " ***********************************************************************" << endl;
cout << " " << name4 << " " << endl;
cout << " is the new president of The British Society of IT Professionals " << endl;
cout << " with " << n4PerTotal << "% of the vote " << endl;
cout << " ***********************************************************************" << endl << endl;;
//Separator
cout << "--------------------------------------------------------------------------------" << endl;
}
cout << "Press any key to exit..." << endl;
getch();
//system("pause");
}
I am using MS Visual Studio 2010 and any help would be great! Please note I am still very new to C++.
EDIT I would like to be able to see the output in CMD as well as have a separate txt file with the same output code. Everything that is displayed in the CMD window also copied into the txt file.
use an ofstream operator as such.
For some reason I can't seem to find the comment button or I would be involved in the discussion above... but I've edited my code below to reflect what seem to be some concerns of yours.
#include<fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("data.txt"); //Will create a new file if one is not already in existence
//You can put a static filename here or have them enter a string
//If you use a custom string your input will be "fout.open(STRING.c_str());
fout<<"Used exactly like cout from this point on";
fout.close(); //When you are done using it to close the file
}
I chose to name my ofstream operator fout because (while this is not always good to do) this way you can quickly change all of your cout's to fout's, or replicate them.
If the user enters a value and you want to spit it back out as well, you can use the ofstream operator after every cin.
You can find more information about ofstream here...
Hope this helped!
You can do this from outside of the application using tee. tee takes the output of a program and splits it into two streams (e.g. one to stdout and one to a file). Unfortunately, Windows doesn't come with a tee command but there are many implementations available.
If you want this to happen from within your program you can do the equivalent of tee using a custom ofstream. This article explains how.