If I run my program, it doesn't compare the value entered after "Bitte Funktion waehlen" to sin, cos or tan. Instead, it always jumps directly to the else statement.
#include <iostream>
#include <string>
using namespace::std;
int funktions_wahl(char funktion, int genauigkeit)
{
char sin;
char cos;
char tan;
int x;
if (funktion==sin) {
cout << "sin von " << x << " =" << 2*x*genauigkeit; //Wie sin als fkt hier benutzen? 2x vorübergehend.
}
else if (funktion==cos) {
cout << "cos von " << x << " =" << 4*x*genauigkeit; //Gleiches Spiel wie oben.
}
else if (funktion==tan) {
cout << "tan von " << x << " =" << 8*x*genauigkeit; //Gleiches Spiel wie oben;
}
else {
cout << "Keine gültige Variable eingegeben\r";
}
return 0;
}
int main() {
char f;
int g=0;
cout << "Taschenrechner 1.0\r" << "Bitte Funktion eingeben: ";
cin >> f;
cout << "Genauigkeit wählen: ";
cin >> g;
funktions_wahl(f, g);
}
I don't understand why. Does someone has an advice for me?
char sin;
char cos;
char tan;
int x;
if (funktion==sin) {
cout << "sin von " << x << " =" << 2*x*genauigkeit;
You forgot to initialize sin, cos, tan and x and are therefore reading uninitialized memory.
You probably intended something like char sin = 's'; and so on.
The problem is that you didn't initialize the variables sin, cos and tan (which should be constants actually), which leads to undefined behavior:
For a debug build, the compiler may indeed execute step-by-step your program, and thus compare uninitialized, random numbers;
For an optimized build, the compiler is likely to optimize away these comparisons between uninitialized local variables and only implement the error case (cout << "Keine gültige Variable eingegeben\r";).
To correct this, you should instead define and initialize these constants as follows:
const char sin = 's';
const char cos = 'c';
const char tan = 't';
UPDATE
To answer your updated question (how to consume "sin" rather than 's'), you simply need to change the types of your variables from char to string, and change the literals from char literals (between simple quotes) to string literals (between double quotes), as exemplified below:
string f;
...
const string sin = "sin";
const string cos = "cos";
const string tan = "tan";
Thanks for your answers.
I changed it as you suggested. But still isn´t it possible to enter e.G sin instead of s ?
#include <iostream>
#include <string>
using namespace::std;
int funktions_wahl(char funktion, int genauigkeit, int x)
{
char sin='s';
char cos='c';
char tan='t';
if (funktion==sin) {
cout << "sin von " << x << " =" << 2*x*genauigkeit << "\r"; //Wie sin als fkt hier benutzen? 2x vorübergehend.
}
else if (funktion==cos) {
cout << "cos von " << x << " =" << 4*x*genauigkeit << "\r"; //Gleiches Spiel wie oben.
}
else if (funktion==tan) {
cout << "tan von " << x << " =" << 8*x*genauigkeit << "\r"; //Gleiches Spiel wie oben;
}
else {
cout << "Keine gültige Variable eingegeben\r";
}
return 0;
}
int main() {
char f;
int g=0;
int x=0;
cout << "Taschenrechner 1.0\r" << "Bitte Funktion eingeben: ";
cin >> f;
cout << "Genauigkeit wählen: ";
cin >> g;
cout << "x wählen: ";
cin >> x;
funktions_wahl(f, g, x);
Related
Im having trouble with this recursion code. Basically I want the computer to "guess" in as little steps as possible the number that I am thinking of. However, everything works except the final output. The bounds are fine, and it narrows down the guess until it asks me if the number im thinking of is say 16, if I input "=" it should output 16 instead it always outputs 50. Could anyone help me locate the error?
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
unsigned int search (unsigned int boundInf, unsigned int boundSup);
int main ()
{
int b;
b = search (1, 100);
cout << "Your number must be : " << b << endl;
}
unsigned int search (unsigned int boundInf, unsigned int boundSup)
{
string magnitude;
int b;
b = (boundSup + boundInf) / 2;
cout << "Is your number <, > or = to " << b << "? ";
cin >> magnitude;
if (magnitude == "<") {
cout << "Between " << boundInf << " and " << b << endl;
search (boundInf, b);
}
else if (magnitude == ">") {
cout << "Between " << b << " and " << boundSup << endl;
search (b, boundSup);
}
return b;
}
You forgot to change the value of b when going deeper into the recursive function, this can be easily fixed by changing the search function like so:
unsigned int search(unsigned int boundInf, unsigned int boundSup)
{
string magnitude;
int b;
b = (boundSup + boundInf) / 2;
cout << "Is your number <, > or = to " << b << "? ";
cin >> magnitude;
if (magnitude == "<")
{
cout << "Between " << boundInf << " and " << b << endl;
b = search(boundInf, b);
}
else if (magnitude == ">")
{
cout << "Between " << b << " and " << boundSup << endl;
b = search(b, boundSup);
}
return b;
}
I have a little calculation.
Right now I calculate p_o and the value I get is correct, but somehow p_TR, where it gets the value from, is shown wrong.
The values I get are:
p_o= 0.3067666187328126 (right)
p_TR= 6.94807050163253e-310 (wrong)
Like you can see close to the end of the code, it says:
p_o = p_TR
I compile the code on Opensuse in the shell withe the g++ command.
I read that floating point numbers vary in decimal points, but this is not the same I would say.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
//Header der angezeigt wird bei Pragrammstart
cout << "----------------------------------------------------------------------\n";
cout << "**********************************************************************\n";
cout << "----------------------------------------------------------------------\n";
cout << "===>Exakter\n";
cout << "======>Riemann\n";
cout << "=========>Loeser" << endl;
cout.flush();
//physikalische Groessen
const float k = 1.4; //isentropen Exponent
const float M = 28.9; //Molare Masse [g/kmol] des Fluids
const float R = 8314.51; //universelle Gaskonstante [J/(kmol*K)]
cout << "\nStoff-/Physikalische Groessen\n" << "k=" << k << "; M=" << M << "; R=" << R << endl;
//Anfangswerte Links
const float rho_L = 1; //Dichte [kg/m^3]
const float u_L = 0; //Geschwindigkeit [m/s]
const float p_L = 1; //Druck [Pa]
const double T_L = (p_L*M)/(R*rho_L); //Temperatur[K]
const double RmT_L = p_L/rho_L; //Zwischen Groesse fuer a_L
const double a_L = pow((k*(R/M)*T_L),(0.5)); //Schallgeschwindigkeit [m/s]
cout << "\nWerte auf der rechten Seite:\n" << "rho_L=" << rho_L << "; u_L=" << u_L << "; p_L=" << p_L << "; T_L=" << T_L << "; a_L=" << a_L << "; RmT_L=" << RmT_L << endl;
//Anfangswerte Rechts
const float rho_R = 0.125;
const int u_R = 0;
const float p_R = 0.1;
const double T_R = (p_R*M)/(R*rho_R);
const double RmT_R = p_R/rho_R; //Zwischen Groesse fuer a_R
const double a_R = pow((k*(R/M)*T_R),(0.5));
cout << "\nWerte auf der linken Seite:\n" << "rho_R=" << rho_R << "; u_R=" << u_R << "; p_R=" << p_R << "; T_R=" << T_R << "; a_R=" << a_R << "; RmT_R=" << RmT_R << endl;
//Allgemeine Anfangswerte
const float du = u_R-u_L; //Geschwindigkeitsdifferenz [m/s]
const double du_krit = ((2*a_L)/(k-1)) + ((2*a_R)/(k-1)); //kritische Geschwindigkeitsdifferenz [m/s] (positive Druck Bedingung)
const double TOL = 1e-6; //Toleranz fuer p*
cout << "\nWeitere Groessen:\n" << "du=" << du << "; du_krit=" << du_krit << "; TOL=" << TOL << endl;
cout.flush();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Variablen Iteration p*
//Berechnung des Schätzwertes
double p_o;
double p_TR;
if (p_L>0 & p_R>0)
{
const double p_TR = pow(((a_L + a_R - 0.5*(k-1)*du)/((a_L/(pow(p_L,((k-1)/(2*k)))))+(a_R/(pow(p_R,((k-1)/(2*k))))))),((2*k)/(k-1))); //Schaetzwert fuer p0[Pa] als annahme das man 2 Verduennungswellen hat
//const double p_o = max(TOL,p_TR); //erster Schaetzwert , genommen ist Druck fuer 2 Verduennungen
p_o = p_TR;
cout << "\nkein Vakuum am Anfang ===> p_TR" << endl;
}
else
{
const double p_o = 0.5*(p_L+p_R);
cout << "\n Vakuum am Anfang ===> arithmitische Mittel fuer p_o" << endl;
}
//Variablen fuer Iteration
int n = 1;
double p_n = p_o; //p fuer Iterationen
float CHA = 1;
//Hilfsvariablen zum einfacheren rechnen
const double AL = 2/((k+1)*rho_L);
const double AR = 2/((k+1)*rho_R);
const double BL = ((k-1)*p_L)/(k+1);
const double BR = ((k-1)*p_R)/(k+1);
cout << "\np_TR=" << std::setprecision(16)<< p_TR;
cout << "\nWerte fuer Iteration:" << "\np_o=" << std::setprecision(16) << p_o << "\nAL=" << AL << "; BL=" << BL << "\nAR=" << AR << "; BR=" << BR << endl;
cout.flush();
Because you are declaring p_TR two times in different scopes, so you basically have two different variables in two different scopes having the same name p_TR.
One is instanced in the main scope, the other is instanced in your if/else block.
Same problem with p_o in the else block.
Just remove the "const double" inside your if/else block, it should do the trick.
double p_o;
double p_TR;
if (p_L>0 & p_R>0)
{
p_TR = pow(((a_L + a_R - 0.5*(k-1)*du)/((a_L/(pow(p_L,((k-1)/(2*k)))))+(a_R/(pow(p_R,((k-1)/(2*k))))))),((2*k)/(k-1))); //Schaetzwert fuer p0[Pa] als annahme das man 2 Verduennungswellen hat
//const double p_o = max(TOL,p_TR); //erster Schaetzwert , genommen ist Druck fuer 2 Verduennungen
p_o = p_TR;
cout << "\nkein Vakuum am Anfang ===> p_TR" << endl;
}
else
{
p_o = 0.5*(p_L+p_R);
cout << "\n Vakuum am Anfang ===> arithmitische Mittel fuer p_o" << endl;
}
I am getting an error saying the operand "<<" (right before times3(x) in the main function ) does not match the operand types being outputted in that line. What am I doing wrong? I searched for errors similar to it and found that its an inclusion error but i thought having would fix it. Also, countdown(seconds) in the main function is not being recognized and giving me an error. Why is that? The problems keep occurring when working with void.
'
#include <iostream>
#include <string>
#include <cstdlib>
#include <limits>
using namespace std;
bool die(const string & msg);
double triple(double x);
double times9(double x);
void triple(double & result, double x);
void times3(double & x);
void countdown(unsigned seconds);
bool restore();
int main(){
double x;
cout << "x: " << endl;
cin >> x;
cout << "The triple of " << x << " is " << triple(x) << endl;
cout << "9 times of " << x << " is " << times9(x) << endl;
cout << "3 times of " << x << " is " << times3(x) << endl;
unsigned seconds;
cout << "seconds: " << endl;
cin >> seconds;
cout << countdown(seconds) << endl;
}
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
double triple(double x){
return 3 * x;
}
double times9(double x){
return 3 * triple(x);
}
void triple(double & result, double x){
x = 3 * x;
}
void times3(double & x){
x = triple(x);
}
void countdown(unsigned & seconds){
unsigned count = seconds;
cin >> seconds || die("input failure");
for (unsigned i = seconds; i <= size; i--){
cout << i << endl;
}
cout << "Blast off! " << endl;
}
bool resotre(){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return cin.good();
}'
As mentioned in earlier answer, you need to change the return type of your function from void to the data type of variable your trying to print.
Another issue in your code is with function void countdown(unsigned & seconds)
Declaration and definition of the functions are different.
You have declared it as void countdown(unsigned seconds); but at the time of defining it you are using void countdown(unsigned & seconds). In declaration you are declaring it to take arguments by value but in definition you are making it to take arguments by reference.
Also in the for loop of the function countdown you have written
for (unsigned i = seconds; i <= 0; i--), this won't print any output, since your condition is i<=0, i think you tried to type i >= 0. :)
times3 returns void. Try:
times3(x);
cout << "3 times of " << x << " is " << x << endl;
Or have times3() return double instead of passing by reference.
double times3(double x);
I am having trouble with a program that uses void functions. I have never used them before so I'm a bit lost. My program has a 3 sets of cities. It is supposed to get the three cities in one of the sets and figure out how long the flight is. My problem is that I keep getting the error that my variables are undefined. This is the first program I have tried using void functions. I have tried initializing every variable by itself but I don't think that is the correct way to do it or is it? Any help would be appreciated. Here is my code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void readFile (int wall1, double &lat1, double &lon1, string &city1,
double &lat2, double &lon2, string &city2,
double &lat3, double &lon3, string &city3);
void intro();
void askDataSet(int &w);
//--------------------------------------------------
int main()
{
intro();
askDataSet(int &w);
string name;
int lat1, lat2, lat3, , lon1, lon2, lon3, beginLat, beginLon, beginCity, midLat, midLon, midCity, endLat, endLon, endCity;
string city1, city2, city3;
readFile (beginLat, beginLon, beginCity, midLat, midLon, midCity, endLat, endLon, endCity);
cout << "The First City at coordinates " << beginLat << " and " << lon1 << " is: " << city1 << endl;
cout << "The Second City is at coordinates " << beginLat << " is " << lon2 << ": " << city2 << endl;
cout << "The Third City is at coordinates " << beginLat << " is " << lon3 << ": " << city3 << endl;
leg1 = dist( beginLat, beginLon, midLat, midLon);
leg2 = dist( midLat, midLon, endLat, endLon);
nonstop = dist( leg1-leg2 );
cout << "It is " << dist << "fewer miles for non-stop" << endl;
system("pause");
return 0;
}
void readFile (int &wall1, double &lat1, double &lon1, string &city1,
double &lat2, double &lon2, string &city2,
double &lat3, double &lon3, string &city3)
{
ifstream dataIn;
dataIn.open("cities.txt");
if(dataIn.fail())
{
cout << "Error. File does not exist. " << endl;
exit(1);
}
dataIn >> lat1;
dataIn >> lon1;
dataIn.get();
getline(dataIn, city1);
dataIn >> lat2;
dataIn >> lon2;
dataIn.get();
getline(dataIn, city2);
dataIn >> lat3;
dataIn >> lon3;
dataIn.get();
getline(dataIn, city3);
}
void intro()
{
cout << "In this lab we will try to figure out how much shorter it is to fly non-stop compared to one-stop." << endl;
cout << endl;
}
void askDataSet(int &w)
{
cout << "Which set of cities would you like to figure the distances for? " << endl;
cin >> w;
}
To give back a value from a void function via a reference-parameter, a variable has to exist in the caller's scope which is given as the parameter.
So in your case, for example to call askDataSet, you first have to declare an int to hold the result:
int w;
askDataSet(w);
Then askDataSet will write into your integer variable and you can use it after the call.
Further points I noticed with the code:
For the readFile call, the variables passed must have the same type as the parameters (double, not int).
There is an extra comma in the declarations before the readData call: "lat3, , lon1"
Maybe you'll want to replace beginLat, midLon etc. with lat1, lon2 (or the other way around).
readFile seems to have an unused first argument int wall1, which is missing in the readFile-call in main.
The dist function is not defined and at the end of main you try to print it.
To use system and exit, you should #include <cstdlib>.
I have a problem with my code. I tried to use reference to take my variables from method to main() function and i get this error : "overloaded member function not found". Please Help! :)
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class Tworzenie_postaci {
public:
Tworzenie_postaci();
string Nazwa_Postaci();
int Wiek_Postaci();
int Staty_Postaci(int Final_Postac_Sila, int Final_Postac_Inteligencja);
int Staty_Postaci();
private:
};
Tworzenie_postaci::Tworzenie_postaci() {
}
string Tworzenie_postaci::Nazwa_Postaci()
{
string wpisz_nazwa;
cout << "Wybierz imie dla swojej postaci:\n";
cin >> wpisz_nazwa;
cout << "Nazwa twojej postaci to:\n" << wpisz_nazwa<< "\n";
return wpisz_nazwa;
}
int Tworzenie_postaci::Wiek_Postaci()
{
int wiek;
cout << "Ile twoja postac ma lat?\n";
cin >> wiek;
cout << "Wiec twoja postac ma " << wiek << " lat\n";
return wiek;
}
void Tworzenie_postaci::Staty_Postaci(int& _Postac_Sila, int& _Postac_Inteligencja) {
int Postac_Sila;
int Postac_Inteligencja;
cout << "Ile twoja postac ma sily? :\n";
cin >> Postac_Sila;
cout << "Twoja postac ma " << Postac_Sila << " sily \n";
cout << "Ile twoja postac ma inteligencji? : \n";
cin >> Postac_Inteligencja;
cout << "Twoja postac ma "<< Postac_Inteligencja << " inteligencji \n";
_Postac_Sila = Postac_Sila;
_Postac_Inteligencja = Postac_Inteligencja;
}
int main()
{
Tworzenie_postaci Postac;
string Final_Imie;
int Final_Wiek;
int Final_Postac_Sila;
int Final_Postac_Inteligencja;
Final_Imie = Postac.Nazwa_Postaci();
Final_Wiek = Postac.Wiek_Postaci();
Postac.Staty_Postaci(Final_Postac_Sila, Final_Postac_Inteligencja);
cout << "\n " << Final_Postac_Sila;
return 0;
}
Sorry for polish names of variables or functions but it is easier for me that way :P
The function is declared as:
int Staty_Postaci(int Final_Postac_Sila,
int Final_Postac_Inteligencja);
It is defined as:
void Tworzenie_postaci::Staty_Postaci(int& _Postac_Sila,
int& _Postac_Inteligencja)
{
...
}
As you can see, the types in the declaration and the definition don't match.
Change one of them to match the other.
Also, the return types must match.