MS Visual C++ Express 2008 problems

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;
}
You must include iostream.

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.
Last edited on
And....DON'T USE void main() EVER EVER EVER EVER!!!!!

Use int main()
I added iostream and it alleviated a lot of errors. I have no idea why it didn't reject it like before, but I'm glad it didn't. Thanks.

I'm also having another problem with creating the calculation for the variance and the standard deviation. Here is the new program and error messages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include "stdafx.h"
#include <cmath>

using namespace 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 ==========
Last edited on
I think I have everything else working except this stupid pow function, please help.

float var=(((a-ave)*pow((float) a-ave,2))+((b-ave)*pow((float) b-ave,2))+((c-ave)*pow((float) c-ave,2))+((d-ave)*pow((float) d-ave))+((e-ave)*pow((float) e-ave,2))+((f-ave)*pow((float) f-ave,2))+((g-ave)*pow((float) g-ave,2)))/6;
cout << "Varience:"<<var<<endl;

What is the correct statement? I'm using google and cannot find the answer.
well.. first of all main() have to return an integer, so:

1
2
3
4
5
main()
{
   ...
   return 0;
}


# pow() function, for the base, required a float/double value, so you have to declared a,b,c... as float/double. (the exponent can be an integer).

# Then, you have to declered "num" before you use it (declared it as float)

ps. why don't you use a "for" instead of writing lots of code?
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include "stdafx.h"
#include <cmath>

using namespace std;


int main ()
{
float 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 << "Summation:" << total << endl;
float ave = total/counter;
cout << "Mean:" << ave << endl;
float var=(((a-ave)*pow((float) a-ave,2))+((b-ave)*pow((float) b-ave,2))+((c-ave)*pow((float) c-ave,2))+((d-ave)*pow((float) d-ave,2))+((e-ave)*pow((float) e-ave,2))+((f-ave)*pow((float) f-ave,2))+((g-ave)*pow((float) g-ave,2)))/6;
cout << "Varience:" << var << endl;
float result=sqrt(var);
cout << "Standard Deviation:" << result << endl << endl;
cout << "End of program.";
return 0;
}
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/
closed account (z05DSL3A)
Keep the console open long enough to see your program's output
http://www.cplusplus.com/forum/articles/7312/

Why system() is evil
http://www.cplusplus.com/forum/articles/11153/
enter
1
2
3
  std::cout << "Press ENTER to continue... " << flush;
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  return 0;

at the bottom of your code,
make sure to #include <limits>
Thanks for the responses. What I did was:

1
2
3
cout << "Enter 999 to exit program.";
h=999;
cin >> h;


Those suggestions were much better though.

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.
h=999 isnt doing anything, just lettin u know
Topic archived. No new replies allowed.