i tried to make a program but didnt work :( help plz

i entered this code


#include <iostream>
using namespace std;
int main ()
{ int a,b,c,sum,avg ;

cout << "enter your marks"<< endl;
cin>> a >> b >> c ;
sum = a+b+c;
cout << " Sum" << sum=a+b+c<< endl;
avg=sum/3;
cout << "AVG"<< avg << endl ;
system ("pause")
return 0 ;
}



whats the problem..
please help me to learn as much as u can..
i'am just new on this
thanks
First, please use the code tags (it's the <> button under Format to the right when posting or editing) to post code. It makes it a lot easier to read and even adds line numbers.

First, when you cout sum you've already assigned the value of a+b+c to sum, so you can just output sum itself.

Second, you forgot the semi-colon after the system("pause"); and also forgot to include the <cstdlib> of which system is a part of.

Also, using system statements like this is kind of bad form and not recommended. A better line of code to pause the program so you can see the window is

cin.get()

This will wait until you hit enter before closing the window.

One last thing. Your average won't be quite as precise as it could be since you are using an int variable. That's prolly ok since this isn't a large program that really needs the precision of a float or a double.
Last edited on
closed account (ShpjE3v7)
Hi Tariqsal,
There is a lot of errors...:

- "system("pause");" needs "window.h";
- "sum = a+b+c;
cout << " Sum" << sum=a+b+c<< endl;"
...?
- "system ("pause")" where is ";"?

This code works very well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <windows.h>

using namespace std;

int a, b, c, sum, avg;
int main()
{
	cout << "Enter your marks: ";
	cin >> a >> b >> c;
	sum = a + b + c;
	cout << "- Sum: " << sum << endl;
	avg = sum / 3;
	cout << "- AVG: "<< avg << endl;
	system("PAUSE");
	return 0;
}


You can do this too for optimize your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <windows.h>

using namespace std;

int a, b, c;
int main()
{
	cout << "Enter your marks: ";
	cin >> a >> b >> c;
	cout << "- Sum: " << a + b + c << endl;
	cout << "- AVG: "<< (a + b + c) / 3 << endl;
	system("PAUSE");
	return 0;
}


BTW.: "system("PAUSE");" isn't a good solution.
Last edited on
Take the example of Bartek as a good one.
But, when you will be developing a serious program, don't use system(). It marks you as a bad programmer and your program will be marked as a virus by most antiviruses out there.
AVG did to me once. I mean, ONCE. Then I uninstalled it, lol.
Thanks guys for helpp..
iam just a little starter..

what can i use instead of Pause .. ??!

i own you guys
I know this is off topic but ...
AVG did to me once. I mean, ONCE. Then I uninstalled it, lol.

Lol. Best workaround ever.
Topic archived. No new replies allowed.