If statments are always executed - c++

Help
#include <iostream>
#include <math.h>
using namespace std;
const float pi = 3.14;
void Odabir(int);
int main(){
int choose;
cout << "Odaberite 1 2 ili 3" << endl;
cin >> choose;
Odabir(choose);
return 0;
}
void Odabir(int choose){
if (choose = 1){
float b, vb;
cout << "Unesite duljinu stranice b: " << endl;
cin >> b;
cout << "Unesite duljinu visine na stranicu b vb: " << endl;
cin >> vb;
cout << "Povrsina raznostranicnog trokuta je: " << ((b*vb) / 2) << endl;
}
if (choose = 2){
float r;
cout << "Unesite duljinu polumjera: " << endl;
cin >> r;
cout << "Povrsina kruga je: " << pow(r, 2)*pi << endl;
}
}
I have been trying to solve that for few hours and I can't get thru it, it looks like when I set choose value all three if clauses get executed and printed.

You are using the assignment operator ( = ) instead of the comparison operator ( == ) in the if statements in the function.
For example
if (choose = 1){
^^^
Write instead
if (choose == 1){
^^^^

You are assigning the variable choose instead of comparing it inside the Odabir function.
for instance, choose = 1 should be choose == 1

Related

how to use if else c++

How do you provide a condition in the if statement, if the variable is an integer data type then it will be displayed, and if the variable is any other data type then something else will be displayed?
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int main() {
char pilih,pilih2;
int p, l, a, t, r,ulang = 4;
float luas1, luas2, luas3, keliling1, keliling2, keliling3, phi = 3.14;
while (ulang > 0) {
pilihUlang:
cout << "Pilih jenis bangun datar berikut :\n 1. Persegi panjang\n 2. Segitiga sama sisi\n 3. Lingkaran" << endl;
cout << "Masukan pilihan anda [1/2/3] : ";
cin >> pilih;
system("cls");
switch (pilih) {
case '1':
system("color 07");
cout << "Luas Dan Keliling Persegi Panjang" << endl;
cout << "Masukan Panjang = ";
cin >> p;
if (?) { // <-- here
cout << "Masukan Lebar = ";
cin >> l;
system("cls");
cout << "Luas dan keliling Persegi Panjang dengan panjang " << p << " dan lebar " << l << ", yaitu :" << endl;
luas1 = p * l;
keliling1 = 2 * (p + l);
cout << "Luas = " << luas1 << endl;
cout << "Keliling = " << keliling1 << endl;
cout << "Sisa bisa memilih ulang " << ulang - 1 << " kali." << endl;
break;
}
else {
//...
}
From the istream::operator>> man page:
If extraction fails (e.g. if a letter was entered where a digit is
expected), zero is written to value and failbit is set.
So, your function could test the cin.good() method to see if the >> operation was successful, like this:
cin >> p;
if (cin.good()) {
cout << "The integer you typed was " << p << endl;
} else {
cout << "Hey, that wasn't an integer!" << endl;
}

How would I implement this maximumGrade function?

#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
#include "CourseGrade.h"
using namespace std;
CourseGrade* maximumGrade(CourseGrade* course0, CourseGrade* course1)
{
}
int main()
{
cout << "Course Grade App!" << endl;
cout << "--------------------------" << endl;
cout << endl;
//Prompting and creating CourseGrade objects and pointer values from inputs
int c1;
float g1;
cout << "Please enter the first course and its grade: ";
cin >> c1 >> g1;
CourseGrade Course0(c1, g1);
CourseGrade* ptrCourse0;
ptrCourse0 = &Course0;
int c2;
float g2;
cout << "Please enter the second course and its grade: ";
cin >> c2 >> g2;
CourseGrade Course1(c2, g2);
CourseGrade* ptrCourse1;
ptrCourse1 = &Course1;
int c3;
float g3;
cout << "Please enter the third course and its grade: ";
cin >> c3 >> g3;
CourseGrade Course2(c3, g3);
CourseGrade* ptrCourse2;
ptrCourse2 = &Course2;
cout << "-----------------------------------" << endl;
cout << "Course" << setw(10) << "Grade" << endl;
cout << "-----------------------------------" << endl;
cout << ptrCourse0->getCourse() << setw(10) << ptrCourse0->getGrade() << endl;
cout << ptrCourse1->getCourse() << setw(10) << ptrCourse1->getGrade() << endl;
cout << ptrCourse2->getCourse() << setw(10) << ptrCourse2->getGrade() << endl;
cout << "-----------------------------------" << endl;
cout << "The course with the maximum grade is: " << maximumGrade(ptrCourse0, ptrCourse1) << endl;
cout << "The average grade is: " << (ptrCourse0->getGrade() + ptrCourse1->getGrade() + ptrCourse2->getGrade()) / 3 << endl;
}
// End of main.cpp
void CourseGrade::setCourse(int c)
{
if (c >= 1000 && c <= 9999)
{
course = c;
}
}
void CourseGrade::setGrade(float g)
{
if (g >= 0.00 && g <= 100.00)
{
grade = g;
}
}
int CourseGrade::getCourse() const
{
return course;
}
float CourseGrade::getGrade() const
{
return grade;
}
CourseGrade::CourseGrade(int c, float g)
{
if (c >= 1000 && c <= 9999)
{
course = c;
}
else
{
course = 1000;
}
if (g >= 0.00 && g <= 100.00)
{
grade = g;
}
else
{
grade = 0.00;
}
}
Can you guys please help me out? I have three objects due to the prompt, but it only asks for two pointers? I am completely unaware of how to get the maximumGrade to show the course with the largest grade. I have tried using if statements to compare the grades of the two pointers showing the values of the grades. It HAS to use pointers to compare the course grades. Thank you guys!
I have three objects due to the prompt, but it only asks for two pointers?
The only way to make sense of two pointers passed to maximumGrade is to assume that these are the beginning and end of an array which contains the three objects.
CourseGrade courses[3] = { Course0, Course1, Course2 };
cout << "The course with the maximum grade is: "
<< maximumGrade(courses, courses+3)->getCourse() << endl;
The body of maximumGrade can then be e. g.
CourseGrade *c = NULL;
float g = 0; // current maximum
for (; course0 < course1; ++course0) if (g <= course0->getGrade())
g = (c = course0)->getGrade();
return c;

C++: Building a mulitfunctional calculator

I am trying to build a calculator in C++. I'm new to the program and have to do this for a school assignment, so sorry for my ignorance. English is also my second language so excuse me if I don't make much sense.
Let's say I have two integers A and B for which a user has to assign a value to either add, subtract, etc. How would I then be able add a third integer (let's say X) without all three showing up when I run the program? So instead of having to type a value for A, B, AND X, it only asks to type a value for X?
For example 4 + 5 = 9, but the calculator can also square numbers, so how do I get the option of a user just filling in 4 squared = 16, while still keeping the former code that lets me add and subtract two numbers?
Maybe seeing the code would help understand what I mean? Sorry if I'm confusing.
#include <iostream.h>
#include <conio.h>
int main ()
{
cout << "Calculator [v.1.0]" << endl;
cout << "(c) 2021 <Chayenne van der Erf>" << endl << endl;
cout << "Kies een bewerking en druk op Enter:" << endl;
cout << "1. Optellen 2. Aftrekken" << endl;
cout << "3. Vermenigvuldigen 4. Delen" <<endl;
cout << "5. Kwadraat 6. Worteltrekken" <<endl;
cout << "7. Reciproke 8. Logarithme" <<endl;
cout << "0. Exit" << endl << endl;
int Bewerking;
cout << "Bewerking: ";
cin >> Bewerking;
cout << "" << endl;
switch (Bewerking) {
case 1:
cout << "+";
break;
case 2:
cout << "-";
break;
case 3:
cout << "*";
break;
case 4:
cout << "/";
break;
default: "Invalid Number";
}
cout << "" << endl << endl;
double A, B;
cout << "Enter een waarde: ";
cin >> A;
cout << "Enter een waarde: ";
cin >> B;
int antwoord;
if (Bewerking == 1) {antwoord = A + B;}
else if (Bewerking == 2 ) {antwoord = A - B;}
else if (Bewerking == 3) {antwoord = A * B;}
else if (Bewerking == 4) {antwoord = A / B;}
cout << "" << endl;
cout << "= " << antwoord << endl;
getch();
return 0;
}
Make the variables, and the reading, conditional on the operation.
Example outline:
if (operation takes one input)
{
double x;
cin >> x;
Calculate result...
}
else if (operation takes two inputs)
{
double x, y;
cin >> x >> y;
Calculate result...
}
else if (operation takes three inputs)
{
double x, y, z;
cin >> x >> y >> z;
Calculate result...
}
Print result...

clearing console in cpp

system("cls") doesnt work, it finishes the program without printing anything.
the program works without it but doesnt look nice in the console without all the previous prints cleared. I've tried putting the command in different places but it doesnt matter where i put it the program finishes instantly.
#include <iostream>
#include <math.h>
#include <windows.h>
#include <stdlib.h>
using namespace std;
float c, temp, eq;
char odp = 't';
int main()
{
do
{
cout << "Choose from what value you'd like to convert." << endl;
cout << "Celsius = 1 ; Fahrenheit = 0" << endl;
cin >> c;
if (c==1)
{
cout << "Insert Celsius value: ";
cin >> temp;
eq = temp * 1.8 + 32;
cout << "It is " << eq << " degrees Fahrenheit." << endl;
cout << "If you want to continue converting please insert: t" << endl;
cin >> odp;
}
else if (c==0)
{
cout << "Insert Fahrenheit value: ";
cin >> temp;
eq = (temp * 1.8) - 32;
cout << "It is " << eq << " degrees Celsius." << endl;
cout << "If you want to continue converting please insert: t" << endl;
cin >> odp;
}
else
{
cout << "Please choose correctly" << endl;
}
}
while(odp = 't');
return 0;
}
how i put the command and it finishes the whole program instantly
#include <iostream>
#include <math.h>
#include <windows.h>
#include <stdlib.h>
using namespace std;
float c, temp, eq;
char odp = 't';
int main()
{
do
{
cout << "Choose from what value you'd like to convert." << endl;
cout << "Celsius = 1 ; Fahrenheit = 0" << endl;
cin >> c;
if (c==1)
{ system("cls") //here
cout << "Insert Celsius value: ";
cin >> temp;
eq = temp * 1.8 + 32;
cout << "It is " << eq << " degrees Fahrenheit." << endl;
cout << "If you want to continue converting please insert: t" << endl;
cin >> odp;
}
else if (c==0)
{
cout << "Insert Fahrenheit value: ";
cin >> temp;
eq = (temp * 1.8) - 32;
cout << "It is " << eq << " degrees Celsius." << endl;
cout << "If you want to continue converting please insert: t" << endl;
cin >> odp;
}
else
{
cout << "Please choose correctly" << endl;
}
}
while(odp = 't');
return 0;
}
i really dont know, thanks for the replies

I have made a calculator with C++, but my request for the "type of calculator" loops twice. Can someone help point out my mistake?

I am new to c++, just recently picked it up and I thought I would try making something to see the results of my studies. I plan on making a calculator with multiple functions, (hopefully) which eventually may include functions such as graphing equations and more difficult calculations.
Here is my code:
#include "stdafx.h"
#include <math.h>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
//CALCULATOR
//Functions
int chooseCal();
float getOp();
float getOpAd();
float userInput();
float normCal(float x, float ops, float y);
float getOp() //ask for operator input
{
cout << "Enter an operator: " << endl;
cout << "1. + (Addition) \n";
cout << "2. - (Subtraction) \n";
cout << "3. * (Multiplication) \n";
cout << "4. / (Division) \n";
cout << "5. ^ (Indices) \n";
cout << "6. -^ (Root) \n";
cout << "7. ! (Factorial) \n";
float ops;
cin >> ops;
return ops;
}
float getOpAd()//ask for operator (Advanced Cal)
{
cout << "Enter an operator: " << endl;
cout << "1. Denery ==> Binary" << endl;
cout << "2. Binary ==> Denery" << endl;
cout << "3. Denery ==> Hexadecimal" << endl;
cout << "4. Hexadecimal ==> Denery" << endl;
float ops;
cin >> ops;
return ops;
}
int chooseCal()
{
cout << "Which calculator would you like to use? " << endl;
cout << "1. Normal \n";
cout << "2. Computational \n";
int chooseC;
cin >> chooseC;
return chooseC;
}
float userInput() //ask for number input
{
cout << "Enter a number: ";
float userInput;
cin >> userInput;
return userInput;
}
//Calculators
float normCal(float x, float ops, float y) //Basic
{
if (ops == 1)
return x + y;
if (ops == 2)
return x - y;
if (ops == 3)
return x * y;
if (ops == 4)
return x / y;
if (ops == 5)
return pow(x, y);
if (ops == 6)
return pow(x, 1 / y);
}
//prints result
void result(float result)
{
cout << "= " << result << endl;
}
//main()
int main()
{
int chooseC = chooseCal();
float ops;
if (chooseCal() == 1)
ops = getOp();
else
ops = getOpAd();
float input1 = userInput();
float input2 = userInput();
float results = normCal(input1, ops, input2);
result(results);
return 0;
}
I have not yet coded for the "Computational" part of my calculator, but when I test the "Normal" calculator the request for either "Normal" or "Computational" repeats itself twice before moving on to showing the options to choose the operator. Can someone help point out where I have went wrong?
The first call to the chooseCal() function is in the following statement:
int chooseC = chooseCal();
The second is in the following if statement:
if (chooseCal() == 1)
Just because the function is used in a condition doesn't mean it doesn't get executed. It does. Here you probably meant:
if (chooseC == 1)