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;
}
Related
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
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.
So in my class I had to make a Numberwang simulation game. Everything works fine except for the fact that after 2 rounds the names don't correlate correctly. It supposed to say "Round 3, Player1 to play first." which it does however player2 comes up as the one to play first.
# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;
bool numberwang(int n)
{
if(n < 100 ){
return 1;
} else {
return 0;
}
}
int main()
{
string Firstplayer, Otherplayer;
int rounds;
int counter = 1;
int number;
int win = 18;
int lose= 1;
cout << "Hello, and welcome to Numberwang, the maths quiz that simply everyone is talking about!" << endl;
cout << "What is player 1's name? ";
cin >> Firstplayer;
cout << "What is player 2's name? ";
cin >> Otherplayer;
cout << "How many rounds? ";
cin >> rounds;
cout << "Well, if you're ready, lets play Numberwang!" << endl;
while(counter <= rounds){
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
while(true){
cout << Firstplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
}
}
return 0;
}
After your if-statement (line 61) you say 'Firstplayer' and then you output the 'Otherplayer'. The names do not match.
Blockquote
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;
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)
I'm trying to call userWeight into the double convert() function. How do I do this? I'm running into issues with it no cooperating in main.
#include <iostream>
#include <string>
using namespace std;
// health calc
string name()
{
cout << "Welcome ________ ... uhmmmm, what was your name again? ";
string name1;
cin >> name1;
cout << " " << endl;
cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
return name1;
}
int height(string name1) //(string name1) is what we are passing into this function
{
//feet and inches to inches
cout << " How tall are you, " << name1 <<"?"<< endl;
cout << " " << endl;
cout << " " << endl;
cout << " Enter feet: ";
int feet;
cin >> feet;
cout << " " << endl;
cout << " Enter inches: ";
int inches;
cin >> inches;
int inchesheight;
inchesheight = (feet * 12) + inches;
cout << " " << endl;
cout << " Your height is equal to " << inchesheight << " inches total." << endl;
if (inchesheight < 65 )
{
cout << " You are shorter than the average male." << endl;
}
else if (inchesheight > 66 && inchesheight < 72)
{
cout << " You are of average height." << endl;
}
else
{
cout << " You are taller than average." << endl;
}
}
double wieght()
{
cout << " How much do you weigh? (In pounds) " << endl;
double userWeight;
cin >> userWeight;
cout << " Ok so your weight in the Imperial System (lbs.), is " << userWeight << endl;
cout << " Would you like to know what your weight is in the Metric System? (kilograms) " << endl;
cout << " please answer as 'yes' or 'no;" << endl;
string response;
cin >> response;
if (response == "yes")
{
cout << " Alright! Let us start converting your weight! " << endl;
}
else if (response == "no")
{
cout << " Too bad! We are going to do it anyway! " << endl;
}
else
{
cout << " That was not a proper response! Way to follow directions!, as consequence, we will do it!" << endl;
}
return userWeight;
}
double convert(double userWeight)
{
cout << " Well since 1 kilogram is equal to 2.2046226218 pounds, we need to divide your weight by that repeating number." << endl;
cout << " Since that number is very long and ugly, we will use 2.2046 for the sake of clarity." << endl;
double kiloWeight = (userWeight / 2.2046);
cout << "Your weight in pounds is " << userWeight << "lbs, divided by 2.2046 gives us" << kiloWeight << "kgs! " << endl;
}
int main()
{
string name1 = name();
height(name1);
weight(userWeight);
convert();
return 0;
}
You are doing it wrong. You should read about function signature and passing arguments.
You have defined weight as a function that doesn't take any arguments
double weight() { //...}
but you are calling it with some parameter userWeight in main function
weight(userWeight);
and this parameter in addition is not defined. ( And no: you cannot call a function with argument being local argument on the stack of function being called from same scope - it is technically possible but this is not what you want).
This should be something like:
int main() {
double userWeight = weight();
double result = convert( userWeight);
// we can see here that local variable named userWeight was assigned value
// from a call to weight() and this result is now being passed to convert
// now you can use a result from calling convert
//...
return 0;
}