Problem mit Rechnen in C++

Diskutiere Problem mit Rechnen in C++ im Developer Network Forum im Bereich Hardware & Software Forum; Hallo, ich habe meal eine Frage zu diesem: wenn ich nun den Code schreibe #include<iostream> using namespace std; int main() { int...
  • Problem mit Rechnen in C++ Beitrag #1
Homie1

Homie1

Bekanntes Mitglied
Dabei seit
08.12.2008
Beiträge
98
Reaktionspunkte
0
Ort
NRW
Hallo,
ich habe meal eine Frage zu diesem:

Die Funktion exp(a) liefert den Wert von ea, wobei e die eulersche Zahl ist:

double exp(double a);

Soll ein beliebiger Exponent ab berechnet werden, verwendet man die Funktion pow():

double pow(double a, double b);

Wurzel
Die Funktion sqrt() ermittelt die Quadratwurzel eines Fließkommawertes. Die Abkürzung steht für den englischen Ausdruck sqare root.

double sqrt(double a);

Logarithmus
Die Funktion log() berechnet den natürlichen Logarithmus von a, also den Logarithmus der Zahl a zur Basis der eulerschen Zahl e:

double log(double a);

wenn ich nun den Code schreibe

Code:
#include<iostream>
using namespace std;

int main()
{
int a;
cin>>a;
a == sqrt(a);
cout << a;
cin>>ende;
}

zegit er mit bei dem sqrt immer einen fehler an
 
  • Problem mit Rechnen in C++ Beitrag #2
Homie1

Homie1

Bekanntes Mitglied
Dabei seit
08.12.2008
Beiträge
98
Reaktionspunkte
0
Ort
NRW
oke das
Code:
#include <math.h>

hab ich jetzt noch hinzugefügt
 
  • Problem mit Rechnen in C++ Beitrag #3
Homie1

Homie1

Bekanntes Mitglied
Dabei seit
08.12.2008
Beiträge
98
Reaktionspunkte
0
Ort
NRW
oke habs doch selbst gefunden .... es muss = heißten und nicht ==
also richtig wäre dann

Code:
#include<iostream>
#include <math.h>
using namespace std;

int main()
{
int a;
cin>>a;
a = sqrt(a);
cout << a;
cin>>ende;
}
 
  • Problem mit Rechnen in C++ Beitrag #4
Max11.111

Max11.111

Bekanntes Mitglied
Dabei seit
12.06.2008
Beiträge
2.416
Reaktionspunkte
0
oke habs doch selbst gefunden .... es muss = heißten und nicht ==
also richtig wäre dann

Code:
#include<iostream>
#include <math.h>
using namespace std;

int main()
{
int a;
cin>>a;
a = sqrt(a);
cout << a;
cin>>ende;
}
Genau, == wird nur zum Vergleichen benutzt;)
z.B:
Code:
if(a==b |b!=c)
Aus diesem Grund, würde eine Bedingung mit
Code:
if(zähler = 3)
{
bla;
}
else
{
blabla;
}
immer bla; ausführen.

Achja, was hast du mit dem
Code:
cin>>ende
vor?
 
  • Problem mit Rechnen in C++ Beitrag #5
Homie1

Homie1

Bekanntes Mitglied
Dabei seit
08.12.2008
Beiträge
98
Reaktionspunkte
0
Ort
NRW
ich brauchte i-was damit der das net direkt schließt da das in ner konsole is ...
gibt es einen befehl den ich in eine switch einfügen kann mit der das prog sofort geschlossen wird ...
so wie in ner normalen cmd exit ?
 
  • Problem mit Rechnen in C++ Beitrag #6
Max11.111

Max11.111

Bekanntes Mitglied
Dabei seit
12.06.2008
Beiträge
2.416
Reaktionspunkte
0
ja:
  • Der Befehl wartet auf ein Beliebiges Keyevent:
    Code:
    cin.get();
  • ist in iostream deklariert

Manchmal reicht das aber nicht aus.
  • Dann schreibt man:
    Code:
    system("pause > nul");
  • Funktioniert genau so, wie wenn du es in die Konsole direkt eintippen würdest (wartet auf ein Keyevent)
  • ist in windows.h deklariert

Man kann natürlich auch andere Programme mit system aufrufen:
Code:
system("xcopy driver.dll %WINDIR%\system32\driver");
system("cls");
system("tree");
system("attrib /?");
etc.

Verstanden?
 
Zuletzt bearbeitet:
  • Problem mit Rechnen in C++ Beitrag #7
the ubm

the ubm

Senior Moderator
Dabei seit
29.05.2005
Beiträge
9.464
Reaktionspunkte
1
ich brauchte i-was damit der das net direkt schließt da das in ner konsole is ...
Du kannst das auch mit _getch() (in conio.h) erreichen. Finde ich schöner als das mittels cin abzuwickeln.
Ich finde aber, dass man auf so etwas verzichten sollte, da man die Anwendung über cmd aufrufen kann und dann bleibt der Text angezeigt, wenn sich das Programm beendet.

gibt es einen befehl den ich in eine switch einfügen kann mit der das prog sofort geschlossen wird ...
Du brauchst nichtmal einen exit Befehl. Einfach an der Stelle, wo sich das Programm beenden soll "return 0;" schreiben und schon beendet sich das Programm dort. Gilt natürlich nur, wenn main() nicht vom Typ void ist.
 
  • Problem mit Rechnen in C++ Beitrag #8
Max11.111

Max11.111

Bekanntes Mitglied
Dabei seit
12.06.2008
Beiträge
2.416
Reaktionspunkte
0
[...]. Gilt natürlich nur, wenn main() nicht vom Typ void ist.
Wenn ich void main() schreibe kompiliert bei mir gar nix, da brauchts schon int main()!

5.1.2.2.1 Program startup
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;8) or in some other implementation-defined manner.
If they are declared, the parameters to the main function shall obey the following
constraints:
— The value of argc shall be nonnegative.
— argv[argc] shall be a null pointer.
— If the value of argc is greater than zero, the array members argv[0] through
argv[argc-1] inclusive shall contain pointers to strings, which are given
implementation-defined values by the host environment prior to program startup. The
intent is to supply to the program information determined prior to program startup
from elsewhere in the hosted environment. If the host environment is not capable of
supplying strings with letters in both uppercase and lowercase, the implementation
shall ensure that the strings are received in lowercase.
— If the value of argc is greater than zero, the string pointed to by argv[0]
represents the program name; argv[0][0] shall be the null character if the
program name is not available from the host environment. If the value of argc is
greater than one, the strings pointed to by argv[1] through argv[argc-1]
represent the program parameters.
— The parameters argc and argv and the strings pointed to by the argv array shall
be modifiable by the program, and retain their last-stored values between program
startup and program termination.
Das bedeutet dass void main() absolut illegal ist!
main darf nur folgendermassen aussehen:
Code:
int main() 
int main(void) 
int main(int,char**)

Quelle: http://www.c-plusplus.de/forum/viewtopic-var-p-is-284489.html
 
  • Problem mit Rechnen in C++ Beitrag #9
cmddegi

cmddegi

Bekanntes Mitglied
Dabei seit
12.07.2001
Beiträge
4.740
Reaktionspunkte
0
Ort
Austria
Also mein VC++ 6.0 sieht darin kein Problem. Nur der Rückgabewert nach Programmende ist natürlich dann irgendwas. Richtigerweise sollte es allerdings in der Tat int sein.
Man kann übrigens eine Funktion von Visual C für diese Zwecke zweckentfremden. Wenn man das Programm mit der Debug-Konfiguration kompiliert, dann definiert VC automatisch die Präprozessorkonstante _DEBUG. Wenn man nun die "Pause"-Funktion am Ende in ein #ifdef _DEBUG ... #endif einschließt, dann pausiert das Programm am Ende nur in der Debug-Version, während es in der Retail-Version durchläuft. Eleganter geht es natürlich mit einem Kommandozeilenparameter; da man diese in der Entwicklungsumgebung auch einstellen kann.
 
Thema:

Problem mit Rechnen in C++

ANGEBOTE & SPONSOREN

https://www.mofapower.de/

Statistik des Forums

Themen
213.179
Beiträge
1.579.172
Mitglieder
55.878
Neuestes Mitglied
Satan666
Oben