Need Help with my programm

Hi i need help wirh my c++ Code, he saied, that m_sqrt is not decleared in the scope and no match for the operator

here is the code for that:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <math.h>


using namespace std;


int main()
{
// Deklaration Variablen
int zahl = 0;
int summe = 0;
int anz = 1;
double avg;
int kl = 1000000;
int gr = 0;
const int MAX = 10000;
int klam;
double s;



// Dateiaufruf
ifstream randomdata;
randomdata.open ("random_uniform.dat");
if (!randomdata)
{
cerr << "Konnte Datei nicht auslesen."<<endl;
randomdata.close();
return 1;
}


// Schleife-Dateneinlese

while (randomdata>>zahl)
{
if (anz>=MAX){
cerr << "Feld zu klein!";
break;
}
randomdata [anz] = zahl
anz = ++;
}
cout << anz << " Werte gelesen.";

// Berechnung Summe, kleinste Zahl, größte Zahl
if (anz<MAX)
{
summe+=zahl;
if (kl>zahl)
{
kl=zahl;
}
if (gr<zahl)

gr=zahl;
}


// Berechnung Mittelwert
avg = static_cast <double> (summe) / (anz-1);



//Klammerwert
klam = pow ((summe-avg),2);

// Berechnung Standardabweichung
s= M_SQRT(klam/anz);


// Ausgabe Ergebnisse
cout << fixed << setprecision (10)
<< setw(8) << "Ergebnis:" << endl
<< setw(8) << "Anzahl n: "
<< setw(8) << anz << endl
<< setw(8) << "Kleinstes Element: "
<< setw(8) << kl << endl
<< setw(8) << "Groesstes Element: "
<< setw(8) << gr << endl
<< setw(8) << "Mittelwert: "
<< setw(8) << avg << endl
<< setw(8) << "Standartabweichung: "
<< setw(8) << s << endl;

return 0;
}


You can't use functions that don't exist. M_SQRT doesn't exist in your code.
is M_SQRT not in <cmath>?
ok thx
can you say me, what i must change that it will work?

I'm a beginner in c++
s= M_SQRT(klam/anz);

Are you trying to find the square root of klam/anz?

First, don't make klam and anz int values. klam/anz is an int divided by an int which gives an int. For example, in C++, 10/6 is 1.

If you're trying to get the square root of something, use the function sqrt - https://en.cppreference.com/w/cpp/numeric/math/sqrt
my task is, to write an to the formel http://kjb1190.blogspot.com/2018/07/formel-info.html

that's why i used klam/anz
Clearly there's some language disconnect here. We know why you're using klam/anz. We're saying it's incorrect. Change them to double instead of int.
1
2
double anz = 1;
double klam;


Once you do that, do sqrt(klam/anz), not M_SQRT.
Last edited on
when i do that, what you say, then it the same problem, that it say
no math for the 'operator []


and that SQRT was not declared in this scope
Make sure that you use sqrt and not SQRT. Case matters in c++.

randomdata is a filestream, not an array, so you can't use the [] operator with it. I don't follow what you were trying to do there.

Post your latest code, in code tags, please. There are a lot of other things in your code that will prevent it compiling.
Last edited on
ah okay, thx so it's a different, when you write SQRT and sqrt


#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <math.h>


using namespace std;


int main() {
// Deklaration Variablen
int zahl = 0;
int summe = 0;
double anz = 1;
double avg;
int kl = 1000000;
int gr = 0;
const int MAX = 10000;
double klam;
double s;




// Dateiaufruf
ifstream randomdata;
randomdata.open ("random_uniform.dat");
if (!randomdata) {
cerr << "Konnte Datei nicht auslesen."<<endl;
randomdata.close();
return 1;
}


// Schleife-Dateneinlese

while (randomdata>>zahl) {
if (anz>=MAX) {
cerr << "Feld zu klein!";
break;
}
randomdata [anz] = zahl
hier is the problem now

anz = ++;
}
cout << anz << " Werte gelesen.";

// Berechnung Summe, kleinste Zahl, größte Zahl
if (anz<MAX) {
summe+=zahl;
if (kl>zahl) {
kl=zahl;
}
if (gr<zahl)

gr=zahl;
}


// Berechnung Mittelwert
avg = static_cast <double> (summe) / (anz-1);



//Klammerwert
klam = pow ((summe-avg),2);

// Berechnung Standardabweichung
s= sqrt(klam/anz);


// Ausgabe Ergebnisse
cout << fixed << setprecision (10)
<< setw(8) << "Ergebnis:" << endl
<< setw(8) << "Anzahl n: "
<< setw(8) << anz << endl
<< setw(8) << "Kleinstes Element: "
<< setw(8) << kl << endl
<< setw(8) << "Groesstes Element: "
<< setw(8) << gr << endl
<< setw(8) << "Mittelwert: "
<< setw(8) << avg << endl
<< setw(8) << "Standartabweichung: "
<< setw(8) << s << endl;

return 0;
}

@kev1990

Please READ previous posts.

Put your code in code tags so that we can run it in c++ shell and so that it shows the original indentation of the code. This makes it much easier to follow code structure.


You have made randomdata a filestream:
ifstream randomdata;
which means that you can't use it like an array:
randomdata [anz] = zahl
... as said in my last post. Please READ it.


The following line is also nonsense:
anz = ++;


Last edited on
The anz++, is mandatory from my professor


here is the task, maybe that helps you to understand, but it is in german, so i hope you can read it

http://kjb1190.blogspot.com/2018/07/info-aufgabe.html
Yes,
anz++
is fine, but
anz = ++;
is not. Please be careful with the difference.

You can either create a separate array to read your data into, or use separate variables to hold sum(X) and sum(X2). Either way would allow you to calculate mean and standard deviation. You need to do one or the other. But you need to be aware of what all your variables are, not try to store data in a filestream, for example. Best to understand your own code, not take snippets from elsewhere and hope that they fit.

Sorry, I can read English and French, but not German.
Last edited on
okay, with anz++ you are right xD

i skip that

the thing is, that my programm must read the file, what it must open and when it is not there, then it must say that the data not exist

when he read the data, then it must calculate that
Topic archived. No new replies allowed.