Very basic calculator issues - c++

I'm a fairly new to c++. I'm trying to create a very basic calculator and the results I'm getting are completely wrong. I've come to a standstill after 2 hours of trying everything in my knowledge. What am I doing wrong?
#include <iostream>
using namespace std;
int main()
{
int a = 0.0;
int b = 0.0;
float sum = 'a' + 'b';
float diff = 'a' - 'b';
float prod = 'a' * 'b';
float quot = 'a' / 'b';
float rem = 'a' % 'b';
//Input
cout << "Enter a number: " << endl;
cin >> a;
cout << "Enter another number: " << endl;
cin >> b;
cout << a << " + " << b << " = " << sum <<endl;
cout << a << " - " << b << " = " << diff <<endl;
cout << a << " / " << b << " = " << quot <<endl;
cout << a << " * " << b << " = " << prod <<endl;
cout << a << " % " << b << " = " << rem <<endl;
return 0;
}

you are calculating with character literals. 'a' is not the same as a here.
remove the quotes when calculating, but add them when you print the actual literal "a"
float sum = 'a' + 'b';
You are calculating the ASCII value of the character "a" (which is 65) with the ASCII value of "b" (which is 66)
It should be
float sum = a + b;
instead.
When you print the values, you did the reverse:
cout << a << " + " << b << " = " << sum <<endl;
You want it to be
cout << "a" << " + " << "b" << " = " << sum <<endl;
instead. You want to print characters for the equation and only a number for the result.
You also calculate the values of a and b before they have an actual value.
You should put the calculation after you enter them.

Fixed, by moving the calculations after the input; and by using variables, not literals.
#include <iostream>
using namespace std;
int main()
{
int a = 0.0;
int b = 0.0;
//Input
cout << "Enter a number: " << endl;
cin >> a;
cout << "Enter another number: " << endl;
cin >> b;
float sum = a + b;
float diff = a - b;
float prod = a * b;
float quot = a / b;
float rem = a % b;
cout << a << " + " << b << " = " << sum <<endl;
cout << a << " - " << b << " = " << diff <<endl;
cout << a << " / " << b << " = " << quot <<endl;
cout << a << " * " << b << " = " << prod <<endl;
cout << a << " % " << b << " = " << rem <<endl;
return 0;
}

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

How is this BTC Code validating the user input?

I have this CODE to take user input and validate it to increase the user BTC in User wallet.
I need some Explanation of this CODE how it works.
The Code:
#include <iostream>
using namespace std;
int main()
{
int btc_input, result_btc, btc_validation, btc_validation_1_input, btc_validation_2_input;
result_btc = 0;
btc_validation = 0;
btc_validation_1_input = 0;
btc_validation_2_input = 0;
for (; btc_validation < 3; btc_validation++)
{
cout << "Enter the Value " << (btc_validation + 1) << " + " ;
cin >> btc_input;
cout << "Enter the Value " << (btc_validation + 1) << " + " ;
cin >> btc_input;
btc_validation_1_input == btc_validation + btc_input;
cout << "Enter the Value " << (btc_validation_1_input + 1) << " + " ;
cin >> btc_validation_2_input;
result_btc = btc_validation_1_input + btc_validation_2_input;
result_btc *= btc_validation_2_input;
break;
}
cout << result_btc << '\n';
cout << "Your BTC Wallet is Increased by " << result_btc << " coins " << '\n';
}
Your Code is not an API in any case but a code, and be aware with scammers. The code you have is working the same way as I will explain by editing and converting your code in a minimum form for your better understanding.
#include <iostream>
using namespace std;
int main()
{
int btc_input, result_btc, btc_validation;
result_btc = 0;
btc_validation = 0;
for (; btc_validation < 3; btc_validation++)
{
cout << "Enter the number >> [" << btc_validation + 1 << "] " ;
cin >> btc_input;
result_btc += btc_input;
}
cout << (result_btc *= btc_input) << '\n';
cout << "You entered " << result_btc << " numbers " << '\n';
}

Football tournament with matrix

I'm trying to make a football tournament in C++, in which the user inputs the name of the teams(8 teams) and then each team has to play with the other one 1 time. Firstly, I don't know how to read the team names, I mean I tried to use .getline or just cin of a char array, but then I need to put the teams into the matrix and after the final game my program should print the table. So there's the first question: how to read the names and basically make the program think they are numbers or does it work with just with names, no need to use int? And then the users inputs the result for every game, but here comes the hard part. After all the results have been introduced, the matrix rotates cyclic and then the result stored in the variables(you will see in code victory/losses) overwrites themselves, so at the end, I cannot print the right table. So that's the second question: How can I make them store to the right 'team' while they rotate? Sorry if I didn't quite explain very well how it works, hope you understand it. Cheers!
// FOOTBALL TOURNAMENT
int map[2][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << "map[" << i << "][" << j << "]= ";
cin >> map[i][j];
}
}
cout << "The map looks like this:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
map[0][0] = 1;
int temp = 0, temp2 = 0, temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0;
int a, b, c, d, e, f, g, h, round = 0;
int victory_m00(0), losses_m10(0), victory_m10(0), losses_m00(0), victory_m01(0), losses_m11(0), victory_m11(0), losses_m01(0);
int victory_m02(0), losses_m12(0), victory_m12(0), losses_m02(0), victory_m03(0), losses_m13(0), victory_m13(0), losses_m03(0);
do
{
// Insert result for every game
cout << "Enter the result of the first game between " << map[0][0] << " vs. " << map[1][0] << endl;
cin >> a >> b;
if (a > b) {
victory_m00++;
losses_m10++;
}
else if (a < b)
{
victory_m10++;
losses_m00++;
}
cout << "Enter the result of the first game between: " << map[0][1] << " vs. " << map[1][1] << endl;
cin >> c >> d;
if (c > d) {
victory_m01++;
losses_m11++;
}
else if (c < d)
{
victory_m11++;
losses_m01++;
}
cout << "Enter the result of the first game between: " << map[0][2] << " vs. " << map[1][2] << endl;
cin >> e >> f;
if (e > f) {
victory_m02++;
losses_m12++;
}
else if (e < f)
{
victory_m12++;
losses_m02++;
}
cout << "Enter the result of the first game between: " << map[0][3] << " vs. " << map[1][3] << endl;
cin >> g >> h;
if (g > h) {
victory_m03++;
losses_m13++;
}
else if (g < h)
{
victory_m13++;
losses_m03++;
}
round++;
// Map switching
temp = map[1][0];
map[1][0] = map[0][1];
temp2 = map[1][1];
map[1][1] = temp;
temp3 = map[1][2];
map[1][2] = temp2;
temp4 = map[1][3];
map[1][3] = temp3;
temp5 = map[0][3];
map[0][3] = temp4;
temp6 = map[0][2];
map[0][2] = temp5;
map[0][1] = temp6;
// Table calculating and printing ~ also this has to be outside the loop (but at first i wanted to print the table after every 'round'
cout << "This is how the table looks like after the " << round << " round: \n";
cout << map[0][0] << " has: " << victory_m00 << " victory(-ies) and " << losses_m00 << " loss-es!\n";
cout << map[0][1] << " has: " << victory_m01 << " victory(-ies) and " << losses_m01 << " loss-es!\n";
cout << map[0][2] << " has: " << victory_m02 << " victory(-ies) and " << losses_m02 << " loss-es!\n";
cout << map[0][3] << " has: " << victory_m03 << " victory(-ies) and " << losses_m03 << " loss-es!\n";
cout << map[1][0] << " has: " << victory_m10 << " victory(-ies) and " << losses_m10 << " loss-es!\n";
cout << map[1][1] << " has: " << victory_m11 << " victory(-ies) and " << losses_m11 << " loss-es!\n";
cout << map[1][2] << " has: " << victory_m12 << " victory(-ies) and " << losses_m12 << " loss-es!\n";
cout << map[1][3] << " has: " << victory_m13 << " victory(-ies) and " << losses_m13 << " loss-es!\n";
cout << endl;
cout << endl;
} while (map[0][1] != 2);
```

trying to order 3 values from least to greatest C++

I have a program that takes numbers that a person enters and sums it.
This happens 3 times, so I have 3 totals. The problem I am having is that I need to order them from greatest to least no matter what the sums come out to be.(this isnt the full code assume the sums are calculated and are declared)
#include <iostream>
#include <string>
using namespace std;
string firstName1, lastName1; // input and output for the users names
string firstName2, lastName2;
string firstName3, lastName3;
// from greatest to least
if ( sum > sum_2 > sum_3 )
{
cout << "Total for" << " " << firstName1 << " " << lastName1 << " " << "$" << sum << ".00" << endl;
cout << "Total for" << " " << firstName2 << " " << lastName2 << " " << "$" << sum_2 << ".00" << endl;
cout << "Total for" << " " << firstName3 << " " << lastName3 << " " << "$" << sum_3 << ".00" << endl;
}
In c++, the syntax sum > sum_2 > sum_3 won't evaluate as you're assuming. It's equivalent to (sum > sum_2) > sum_3.
In the case where sum is greater than sum_2, sum > sum_2 will evaluate to true. Then, this boolean value will be converted to an integer, 1 and compared with sum_3.
To do what you're trying to accomplish try:
if (sum > sum_2 && sum_2 > sum_3)
Use a helper swap function:
void swap( int *a, int *b )
{
int temp = *a;
*a = *b;
*b = temp;
}
And bubble sort it:
int sums[3] = { sum, sum_2, sum_3 };
for ( int i = 0; i < 3; ++i )
for ( int j = 0; j < i; ++j )
if ( sums[j] < sums[i] )
swap( &sums[j], &sums[i] );
cout << "Total for" << " " << firstName1 << " " << lastName1 << " " << "$" << sums[0] << ".00" << endl;
cout << "Total for" << " " << firstName2 << " " << lastName2 << " " << "$" << sums[1] << ".00" << endl;
cout << "Total for" << " " << firstName3 << " " << lastName3 << " " << "$" << sums[2] << ".00" << endl;

Comapring two vector elements and get EXC_BAD_ACCESS(code=1, address = 0x0) error

I am writing a program that needs to compare two elements at a time (of a 9 element vector) to find the lowest element. To do this I created a Sort function that acts like a Bubblesort and at the end of the sort I will take the first element of the vector (Which will be the lowest). My problem is that Xcode is telling me my equation is pointing to NULL. Any ideas?
void ParkingLot::GateA(){
vector<int> AvailSpots (9);
AvailSpots[0] = 40;
AvailSpots[1] = 20;
AvailSpots[2] = 50;
AvailSpots[3] = 30;
AvailSpots[4] = 55;
AvailSpots[5] = 15;
AvailSpots[6] = 20;
AvailSpots[7] = 33;
AvailSpots[8] = 27;
vector<string> Lot (9);
Lot[0] = "A";
Lot[1] = "B";
Lot[2] = "C";
Lot[3] = "D";
Lot[4] = "E";
Lot[5] = "F";
Lot[6] = "G";
Lot[7] = "H";
Lot[8] = "I";
vector<int> Cost (9);
Cost[0] = 25.00;
Cost[1] = 22.50;
Cost[2] = 20.00;
Cost[3] = 22.50;
Cost[4] = 20.00;
Cost[5] = 17.50;
Cost[6] = 20.00;
Cost[7] = 17.50;
Cost[8] = 15.00;
vector<int> Distance (9);
Distance[0] = 10;
Distance[1] = 20;
Distance[2] = 30;
Distance[3] = 20;
Distance[4] = 30;
Distance[5] = 40;
Distance[6] = 30;
Distance[7] = 40;
Distance[8] = 50;
int choice2;
cout << "-------------------------------------------------------------------------" << endl;
cout << setw(50) << "GATE A: Lot Information" << endl;
cout << "-------------------------------------------------------------------------" << endl;
cout << "LOT ID : MAX CAPACITY : AVAILABLE SPOTS : COST($DD.CC) : DISTANCE" << endl;
cout << "-------------------------------------------------------------------------" << endl;
cout << Lot[0] << ": 40 : "<<AvailSpots[0]<<": "<<setw(20)<<" "<<Cost[0]<<" : " <<Distance[0] << endl;
cout << Lot[1] << ": 20 : "<<AvailSpots[1]<<": "<<setw(20)<<" "<<Cost[1]<<" : " <<Distance[1] << endl;
cout << Lot[2] << ": 50 : "<<AvailSpots[2]<<": "<<setw(20)<<" "<<Cost[2]<<" : " <<Distance[2] << endl;
cout << Lot[3] << ": 30 : "<<AvailSpots[3]<<": "<<setw(20)<<" "<<Cost[3]<<" : " <<Distance[3] << endl;
cout << Lot[4] << ": 55 : "<<AvailSpots[4]<<": "<<setw(20)<<" "<<Cost[4]<<" : " <<Distance[4] <<endl;
cout << Lot[5] << ": 15 : "<<AvailSpots[5]<<": "<<setw(20)<<" "<<Cost[5]<<" : " <<Distance[5] <<endl;
cout << Lot[6] << ": 20 : "<<AvailSpots[6]<<": "<<setw(20)<<" "<<Cost[6]<<" : " <<Distance[6] << endl;
cout << Lot[7] << ": 33 : "<<AvailSpots[7]<<": "<<setw(20)<<" "<<Cost[7]<<" : " <<Distance[7] << endl;
cout << Lot[8] << ": 27 : "<<AvailSpots[8]<<": "<<setw(20)<<" "<<Cost[8]<<" : " <<Distance[8] << endl;
cout << " Total : 290 : "<<TotalAvailSpots<<":" << endl;
cout << "Select a criteria to allot a parking lot :" << endl;
cout << "1. Based on Cost - Cheapest Parking Lot" << endl;
cout << "2. Based on Distance - Closest to Stadium" << endl;
cout << "0. EXIT" << endl;
cout << "Enter Option (1-2): " << endl;
cin >> choice2;
if (choice2 == 1) {
ParkingLot::Sort();
}
}
int ParkingLot::Sort(){
int cost;
for (int i = 0; i < 8; i++){
if (Cost[i] > Cost[i + 1]) { /////////////ERROR APPEARS HERE Thread1:EXC_BAD_ACCESS(code=1, address=(0x0)
ParkingLot::Swap(i, i+1);
}
}
cout << "Cost of parking lot " << Cost.front() << endl;
cin >> cost;
return cost;
}
void ParkingLot::Swap(int a, int b){
int tmp = Cost[a];
Cost[a] = Cost[b];
Cost[b] = tmp;
}
The variables you define in GateA are local to the function and hiding any member variables with the same names.
So the Cost vector you're using in Sort and Swap is empty because it is a different one from the one you built in GateA.
Remove the line
vector<int> Cost (9);
from GateA and instead call Cost.resize(9) before assigning the elements, or build the vector using push_back rather than direct indexing.