For loop with different inputs - c++

please see my code below and let me know what I'm not seeing. I've tried almost everything to make it work and nothing yet.
Thanks in advance for your time and help.
for (double c = c1; c <= c2; c = c + i)
{
cout << "Enter the lowest temperature: " << endl;
cin >> c1;
cout << "Enter the highest temperature: " << endl;
cin >> c2;
cout << "Enter the desired increment in temperature: " << endl;
cin >> i;
f = ((9 / 5) * c) + 32;
cout << "When C is " << c << " degrees Celsius, the temperature in Fahrenheit will be: " << f << " degrees." << endl;
cin.get();
}
return 0;
}

You take the input in the wrong part of the code.
c1 and c2 were probably never assigned before the loop, and your logic as reflected in the codedoesn't seem to match your intentions as it changes the loop limit every time.
Also, note using 9.0 instead of 9 to get a double result and not an integer.
See full example here https://ideone.com/Uj974z.
int main() {
double c1, c2, i, f;
cout << "Enter the lowest temperature: " << endl;
cin >> c1;
cout << "Enter the highest temperature: " << endl;
cin >> c2;
cout << "Enter the desired increment in temperature: " << endl;
cin >> i;
for (double c = c1; c <= c2; c = c + i)
{
f = ((9.0 / 5) * c) + 32;
cout << "When C is " << c << " degrees Celsius, the temperature in Fahrenheit will be: " << f << " degrees." << endl;
}
return 0;
}

The problem is that 9/5 returns 1. You should write 9.0/5.

Related

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;

How can we rectify nan while using trigonometric functions in c++

cout << "Please enter the A vector's magnitude \n";
cin >> A_v;
cout << "Please enter the B vector's magnitude \n";
cin >> B_v;
cout << "The general form to find the direction of the resultant vector = \n" << "tan^-1 X " << UL << " [B X sin] \n" << UL_
<< " [A + B X cos]\n";
cout << "Please enter the angle value \n";
cin >> ct;
cost = cos(ct);
sint = sin(ct);
itan = atan(ct);
al = itan*((B*sint)/(A+(B*cost)));
ald = al*(180/3.141562);
cout << "The direction of the vector is inclined at " << ald << " degrees \n";
This is a program to find the inclination/direction of a vector(in physics)

My Quadratic equation code terminates when I enter a non integer value, how can I get it to loop properly?

So I'm trying to get my Quadratic equation solution code to loop unless "0" is entered as any 1 of the quadratic coefficients.
It works fine up until entering a non integer value, in which the program terminates.
I'd like the code to spit out a message prompting the user to enter a numerical value, and continue the loop as normal.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
for ( ; ; ){
float a, b, c, D, x1, x2, real, im;
cout << "Please enter the Quadratic Coefficients" << endl;
cin >> a >> b >> c;
if (cin.fail()){
cout << "Error, please enter numerical values!" << endl;
cin >> a >> b >> c;
}
if ((a == 0) || (b == 0) || (c == 0)){
break;
}
D = b*b - 4*a*c;
if (D < 0) {
real = -b/(2*a);
im = sqrt(-D)/(2*a);
cout << "Roots are Complex" << endl;
cout << "x1 = " << real << "+" << im << "i" << endl;
cout << "x2 = " << real << "-" << im << "i" << endl;
}
else if (D == 0) {
x1 = (-b + sqrt(D)) / (2*a);
cout << "Real and Repeated Roots" << endl;
cout << "x1 = " << x1 << endl;
}
else if (D > 0)
{
x1 = (-b + sqrt(D)) / (2*a);
x2 = (-b - sqrt(D)) / (2*a);
cout << "Real and Distinct Roots" << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
} }
return 0;
}
This solution here should help.
cin.fail() set the input stream into a failed state, and you need to reset it manually to get it to do any further work. When you call cin again, it will notice its failed state and just go on, otherwise.
cin >> a >> b >> c;
if (cin.fail()){
cin.clear(); //removes error flags
cin.ignore(); //ignores last input
cout << "Error, please enter numerical values!" << endl;
cin >> a >> b >> c;
}

C++ First program - Calculator

Hello I just started learning C++ and Im trying to make a calculator, right now having a fue problems that I simply dont know how to fix in C++.
#include <iostream>
using namespace std;
int main() {
int a, b, c, d;
cout << "1. Saskaitiissana(+)" << endl;
cout << "2. atnnemssana(-)" << endl;
cout << "3. daliissana(/)" << endl;
cout << "4. reizinaassana(*)" << endl;
cin >> d;
switch(d){
case 1 :
cout << "ievadiet a un b lai saskaitiitu(+)" << endl;
cin >> a;
cin >> b;
c = a + b;
cout << "The sum of number 1 and number 2 is " << c << "\n" <<endl;
break;
case 2 :
cout << "ievadiet a un b lai atnnemtu(-)" << endl;
cin >> a;
cin >> b;
c = a - b;
cout << c << endl;
break;
case 3 :
cout << "ievadiet a un b lai reizinaatu(*)" << endl;
cin >> a;
cin >> b;
c = a * b;
cout << c << endl;
break;
case 4 :
cout << "ievadiet a un b lai dal'itu(/)" << endl;
cin >> a;
cin >> b;
if(b==0)
{
cout<<"Nulle neder! start over."<<endl;
}
c = a/b;
cout << c << endl;
break;
}
return 0;
}
The things I still have to do.
Find the most easy way for the program to use numbers only. Also when I type in a number it can not be "empty space".
Also how can I make the case after it finish and gives you the result, go back to the begining of start menu? and if I want to exit a program I press esc or 5?
Also with the exit option I was thinking of useing do while "5" is pressed, can that work in c++?
Right now Im most interested on how to check program to use numbers only and have no empty space when adding numbers.
Thank you for your time :)
For ignoring non-numeric input you can this piece of code:
std::cin >> d;
while(std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Bad entry. Enter a NUMBER: ";
std::cin >> d;
}
or in C-style:
while(scanf("%i",&d)!=1)
{
fseek(stdin,0,SEEK_END);
}
You can also put your whole bunch of code in a while statement to re-run the calculator after one operation.
Taking into account the safe input:
//----------------------------------------------------------------------------
#include <iostream>
using namespace std;
//----------------------------------------------------------------------------
void SafeDouble (double &d)
{
while (!(cin >> d))
{ cin.clear();
while (cin.get() != '\n');
cout << "\tIncorrect. Try again\n\t";
}
cin.sync();
}
//----------------------------------------------------------------------------
int main()
{
cout << "The simpliest calculator\n";
double a = 0.,b = 0.;
cout << "\na = ";
SafeDouble (a);
cout << "b = ";
SafeDouble (b);
cout << "\nEnter operation sign: +, -, * or /\n";
char op;
cin >> op;
cin.sync();
switch (op)
{
case '+': cout << a << " + " << b << " = " << a + b;
break;
case '-': cout << a << " - " << b << " = " << a - b;
break;
case '*': cout << a << " - " << b << " = " << a * b;
break;
case '/': if (b == 0.0)
cout << "Division by zero";
else
cout << a << " / " << b << " = " << a / b;
break;
default: cout << "Incorrect operation sign";
}
cin.get();
return 0;
}
//-----------------------------------------------------------------------------

C++ celsius to fahrenheit 3 times

I'm learning C++, and to do that I have created myself a problem which is to convert Celsius to Fahrenheit three times in the console. The user will input the Celsius degree.
I also want the output to be displayed like this:
Celsius: Fahrenheit:
cel1 fahr1
cel2 fahr2
cel3 fahr3
The code I have tried so far is:
double cel1, cel2, cel3;
double fahr1, fahr2, fahr3;
cout << "Celsius degree one: ";
cin >> cel1;
cout << "Celsius degree two: ";
cin >> cel2;
cout << "Celsius degree three: ";
cin >> cel3;
fahr1 = (cel1 * 9) / 5 + 32;
fahr2 = (cel2 * 9) / 5 + 32;
fahr3 = (cel3 * 9) / 5 + 32;
// messy like this to display like I want to
cout << endl <<
"Celsius: " << "Fahrenheit:" << endl <<
cel1 << " " << fahr1 << endl <<
cel2 << " " << fahr2 << endl <<
cel3 << " " << fahr3 << endl << endl;
which will display like I want to, but I feel this could have been achieved in a simpler way, so I tried something like this with a loop, but I couldn't figure out how to do it properly:
double celsius;
for (int times = 0; times != 3; ++times){
cout << "Celsius degree: ";
cin >> celsius;
double fahrenheit = (celsius * 9) / 5 + 32;
cout << "Fahrenheit degree: " << fahrenheit << endl;
cin.clear();
}
This code is less then the previous one, gives the correct answer and will convert three times, but I couldn't figure out how to display it like I want to.
My question is what is the best way to do this?
I suggest to split the code into smaller functions:
The one to compute the conversion
double celsius_to_fahrenheit(double celsius)
{
return (celsius * 9.0) / 5.0 + 32.0;
}
The one to get the input, I choose to use std::vector as container.
you may choose std::array<double, 3> since the array have fixed size,
but std::vector is a good default choice.
std::vector<double> get_input_celsius(std::size_t size)
{
std::vector<double> celsius(size);
for (std::size_t i = 0; i != celsius.size(); ++i) {
std::cout << "Celsius degree " << (i + 1) << ": ";
std::cin >> celsius[i];
}
return celsius;
}
The method to display the result. I choose to not store the conversion in a new std::vector since it is not used afterward:
void display_celsius_and_fahrenheit(const std::vector<double>& celsius)
{
std::cout << std::endl << "Celsius: " << "Fahrenheit:" << std::endl;
for (auto c : celsius) { // for range since C++11
std::cout << c << " " << celsius_to_fahrenheit(c) << std::endl;
}
}
And finally the main function:
int main()
{
std::vector<double> celsius = get_input_celsius(3);
display_celsius_and_fahrenheit(celsius);
return 0;
}
Live example
Create arrays to store the temperatues.
Instead of
double cel1, cel2, cel3;
double fahr1, fahr2, fahr3;
use
double celsius[3];
double fahrenheit[3];
Change the input gathering loop to use the arrays.
for (int times = 0; times != 3; ++times){
cout << "Celsius degree: ";
cin >> celsius[times];
fahrenheit[times] = (celsius[times] * 9) / 5 + 32;
cin.clear();
}
Use a loop to create the output:
cout << endl << "Celsius: " << "Fahrenheit:" << endl <<
for (int times = 0; times != 3; ++times){
cout << celsius[times] << " " << fahrenheit[times] << endl;
}
You could get away with not creating the array fahrenheit too if you compute it only during output.
In that case, change the input gathering loop to:
for (int times = 0; times != 3; ++times){
cout << "Celsius degree: ";
cin >> celsius[times];
cin.clear();
}
Change the output loop to:
cout << endl << "Celsius: " << "Fahrenheit:" << endl <<
for (int times = 0; times != 3; ++times){
double fahrenheit = (celsius[times] * 9) / 5 + 32;
cout << celsius[times] << " " << fahrenheit << endl;
}