# need help on C++ exercise

This topic is 5134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am using the Dev-C++ compiler and I am trying to do the following: Write a program that gets three scores from the user then display the average. Can someone show me the code so I can learn how to do this. I swear I am a major newb and I'm trying to figure this out. A list of the actual code and what it means would be greatly appreciated.

##### Share on other sites
www.gametutorials.com

Excellent resource. I'm a newb as well, and this site is helpful to get you started. I suggest picking up a good book on C++ as well.

##### Share on other sites
Quote:
 Original post by DanishpxI am using the Dev-C++ compiler and I am trying to do the following:Write a program that gets three scores from the user then display the average. Can someone show me the code so I can learn how to do this. I swear I am a major newb and I'm trying to figure this out. A list of the actual code and what it means would be greatly appreciated.

Lots of ways to do this.

#include <iostream>using namespace std;int main(){	float x,y,z;	cout << "Enter three numbers: ";	cin >> x >> y >> z;	cout << "average: " << (x+y+z)/3 << endl;	return 0;}

##### Share on other sites
and you can do this way and is the better way to learn object oriented:

mathematics.h file:

#ifndef mathematics_h
#define mathematics_h

class mathematics
{
private:

float value;

public:

mathematics();
~mathematics();

float average();
};

#endif

mathematics.cpp file:

#include "mathematics.h"
#include <iostream.h>

mathematics::mathematics()
{
}
mathematics::~~mathematics()
{
}
float mathematics::average()
{
int num;
float total;
float sum;

cout<<"How mutch numbers?"<<endl;
cin>>num;
for(i=0;i<num;i++)
{
cout<<"write the number:"<<endl;
cin>>value;
sum=sum+value;
}
total=sum/num;
cout<<"Result:"<<total<<endl;
}

main.cpp file:

#include "mathematics.h"
#include <iostream.h>

void main()
{
mathematics m;

m.average()
}

The other way is right too but this way is more object oriented.
I didnt test it in the compiler.

##### Share on other sites
Thanks alot for yer help both of you. I just have one more question. What does it mean when you put float before x, y, and z?

##### Share on other sites
Dude, thats why books and tutorials are for! Dont ask questions like that anymore :) but still, float means its a floating point number - for you - a number that can have digits after the comma: like 1.5 and 8.6

int a; //means a can only hold values that are whole numbers: 1,2,3 etc

float a;//means a can hold values like 1, 1.1, 1.2, 1.4364576 and stuff :)

##### Share on other sites
float is a decimal number :)

##### Share on other sites
Quote:
 Original post by DanishpxWhat does it mean when you put float before x, y, and z?

The way your problem statement is written:

Quote:
 I am using the Dev-C++ compiler and I am trying to do the following:Write a program that gets three scores from the user then display the average.

very, very clearly indicates a copy-and-paste from some outside source (either a teacher or a book). This indicates that you have either a teacher to ask (or pay attention to in class) or a book to read (up until the exercise section at the end of the chapter). Yes? :s

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633310
• Total Posts
3011304
• ### Who's Online (See full list)

There are no registered users currently online

×