Hi, I'm new to C++ and I'm taking a course in college to learn it. It's quite frustrating, especially when you're professor doesn't know anything about most of the versions. I'm trying to do a homework assignment which is to create a program that simply calculates the mean, varience, and standard deviation of 7 user input numbers. When I try to compile the program, I keep getting an administrative error saying that cout and cin are unidentifiable. I tried using #include <iostream> with using namespace std; and it said that it was neglected because it's trying to find a precompiler. I used stdafx.h and it's just not working. Please help. Thanks.
#include "stdafx.h"
#include <cmath>
using namespace std;
void main ()
{
int total = 0, counter = 0, a, b, c, d, e, f, g;
cout << "Enter a number:";
cin >> a; counter++;
total+=a;
cout << endl;
cout << "Enter a second number:";
cin >> b; counter++;
total+=b;
cout<< endl;
cout<<"Enter a third number:";
cin >> c; counter++;
total+=c;
cout << endl;
cout << "Enter a fourth number:";
cin >> d counter++;
total+=d;
cout << endl;
cout << "Enter a fifth number:";
cin >> e; counter++;
total+=e;
cout << endl;
cout << "Enter a sixth number:";
cin >> f; counter++;
total+=f;
cout << endl;
cout << "Enter a seventh number:";
cin >> g; counter++;
total+=g;
cout << endl;
cout << "Summation:"<<total<<endl;
int ave = total/counter;
cout << "Mean:"<<ave<<endl;
int result; result=[(a-ave)pow(a-ave)+(b-ave)pow(b-ave)+(c-ave)pow(c-ave)+(d-ave)pow(d-ave)+(e-ave)pow(e-ave)+(f-ave)pow(f-ave)+(g-ave)pow(g-ave)]/6;
cout << "Varience:"<<result<<endl;
int result; result=sqrt(result);
cout<<"Standard Deviation:"<<result<<endl;
}
EDIT: Now that I look more closely, there are a number of errors. You can't use square brackets like they're parentheses; change them to parentheses. You can't multiply numbers like this (a-ave)pow(...); you must use the asterisk like this (a-ave)*pow(...). Also, pow takes 2 arguments, not one.
#include <iostream>
#include "stdafx.h"
#include <cmath>
usingnamespace std;
int main ()
{
int total = 0, counter = 0, a, b, c, d, e, f, g;
cout << "Enter a number:";
cin >> a; counter++;
total+=a;
cout << endl;
cout << "Enter a second number:";
cin >> b; counter++;
total+=b;
cout<< endl;
cout<<"Enter a third number:";
cin >> c; counter++;
total+=c;
cout << endl;
cout << "Enter a fourth number:";
cin >> d, counter++;
total+=d;
cout << endl;
cout << "Enter a fifth number:";
cin >> e; counter++;
total+=e;
cout << endl;
cout << "Enter a sixth number:";
cin >> f; counter++;
total+=f;
cout << endl;
cout << "Enter a seventh number:";
cin >> g; counter++;
total+=g;
cout << endl;
cout << "Summation:"<<total<<endl;
float ave = total/counter;
cout << "Mean:"<<ave<<endl;
float result; result=(((a-ave)*pow(a-ave,2))+((b-ave)*pow(b-ave,2))
+((c-ave)*pow(c-ave,2))+((d-ave)*pow(d-ave))
+((e-ave)*pow(e-ave,2))+((f-ave)*pow(f-ave,2))+((g-ave)*pow(g-ave,2)))/6;
cout << "Varience:"<<result<<endl;
float result; result=sqrt(num);
cout<<"Standard Deviation:"<<result<<endl;
}
1 2 3 4 5 6 7 8 9 10
1
1>Compiling...
1>pppp.cpp
1>c:\users\cameron\documents\visual studio 2008\projects\pppp\pppp\pppp.cpp(44) : warning C4244: 'initializing' : conversion from 'int' to 'float', possible loss of data
1>c:\users\cameron\documents\visual studio 2008\projects\pppp\pppp\pppp.cpp(46) : error C2661: 'pow' : no overloaded function takes 1 arguments
1>c:\users\cameron\documents\visual studio 2008\projects\pppp\pppp\pppp.cpp(48) : error C2086: 'float result' : redefinition
1> c:\users\cameron\documents\visual studio 2008\projects\pppp\pppp\pppp.cpp(46) : see declaration of 'result'
1>c:\users\cameron\documents\visual studio 2008\projects\pppp\pppp\pppp.cpp(48) : error C2065: 'num' : undeclared identifier
1>Build log was saved at "file://c:\Users\Cameron\Documents\Visual Studio 2008\Projects\pppp\pppp\Debug\BuildLog.htm"
1>pppp - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This was homework assignment 1 and he wants us to be able to understand the basics before we start using loops. It's quite annoying. Yea, I pretty much got it to finally run, but when I get the seventh number input, the program ends and it shows the statistics for a split second. How do you keep the statistics on screen? Here is my new code.
Take a look at this posting on how to keep the console open. There are many that will blast you if you use system("pause"); and demand something cross platform, even if you have absolutely no intention on leaving windows. Use what makes sense to you for now since you are new to C++. You should never use code you do not FULLY understand. http://www.cplusplus.com/forum/beginner/1988/
Everything seems good except that the variance and standard deviation compute incorrect figures. I either get 0 values or negative values, or even undefined. I'm using standard numbers like 5-35. I cannot figure out what's wrong with the variance and standard deviation statements.