View Full Version : Need some C++ help !
Hey, I have to make a program for school and dont know how to make my program take all of the numbers i've imputted and show the average of all of them. could someone give me a hand with this? thanks..
~Kyle
EDIT: I use macs at school. Just letting you know =p
R A I D
12-27-2008, 07:00 PM
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n;
ifstream Input;
ofstream Output;
Input.open("num.txt");
if(Input.fail( ))
{
cout << "error opening input file"<<endl;
return 1;
}
Output.open("average.txt");
if(Output.fail( ))
{
cout << "error opening output file"<<endl;
return 0;
}
int total=0;
int count=0;
while(!Input.eof())
{
Input>>n;
total=total+n;
count++;
}
float average=0;
average=float(total)/float(count);
cout<<"sum "<<total<<" count "<<count<<" average "<<average;
Output<<average;
Input.close();
Output.close();
return 0;
}
Thanks, still doesn't work, but that goes to show how much i fail. Lol.
~Kyle
Jamestf347
01-28-2009, 03:50 PM
I had this program in the beginning of the year.
//James
//Period 1
//Average
//User inputs three numbers and it calculates the average of them both
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
main()
{
//clear screen at startup
clrscr();
//colors
textcolor(YELLOW);
textbackground(RED);
//clrear screen for color
clrscr();
//variables
float num1, num2, num3, avg;
//user input 1
cout<<"Please Enter The First Number and Press Enter: ";
cin>>num1;
//clear screen after number 1
clrscr();
//user input 2
cout<<"Please Enter The Second Number and Press Enter: ";
cin>>num2;
//clear screen after number 2
clrscr();
//user input 3
cout<<"Please Enter The Third Number and Press Enter: ";
cin>>num3;
//clear screen after number 3
clrscr();
//confirmation
cout<<"The Numbers are "<<num1<<", "<<num2<<", and " <<num3<<"\n\n";
cout<<"If these are correct Press Enter, If Not Restart Program.\n\n";
getch();
//calculation on screen
clrscr();
cout<<"Calculating Average...";
delay(800);
clrscr();
//calculation
avg=(num1+num2+num3)/3;
//output
cout<<"The Average of is "<<avg<<"\n\n";
cout<<"To Close The Program Press Enter";
getch();
return 0;
}
Thiefmn6092
02-03-2009, 09:47 AM
I had this program in the beginning of the year.
//James
//Period 1
//Average
//User inputs three numbers and it calculates the average of them both
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
main()
{
//clear screen at startup
clrscr();
//colors
textcolor(YELLOW);
textbackground(RED);
//clrear screen for color
clrscr();
//variables
float num1, num2, num3, avg;
//user input 1
cout<<"Please Enter The First Number and Press Enter: ";
cin>>num1;
//clear screen after number 1
clrscr();
//user input 2
cout<<"Please Enter The Second Number and Press Enter: ";
cin>>num2;
//clear screen after number 2
clrscr();
//user input 3
cout<<"Please Enter The Third Number and Press Enter: ";
cin>>num3;
//clear screen after number 3
clrscr();
//confirmation
cout<<"The Numbers are "<<num1<<", "<<num2<<", and " <<num3<<"\n\n";
cout<<"If these are correct Press Enter, If Not Restart Program.\n\n";
getch();
//calculation on screen
clrscr();
cout<<"Calculating Average...";
delay(800);
clrscr();
//calculation
avg=(num1+num2+num3)/3;
//output
cout<<"The Average of is "<<avg<<"\n\n";
cout<<"To Close The Program Press Enter";
getch();
return 0;
}
damn, which platform were you on? MSDOS? iostream.h is very old school.
Jamestf347
02-03-2009, 07:20 PM
damn, which platform were you on? MSDOS? iostream.h is very old school.
I luled... dude basic c++ is in a Dos window... every program is coded this way it just all depends on your includes... iostream.h is used everywhere just look harder... it may be called something else but its one of the most important files in a program (makes outputs/inputs work) most of the new compilers do not include the .h but the compiler we use in my sophmore highschool class does.
kthxbai
Kperrylov3
02-03-2009, 07:55 PM
LOL so just go to the link I don't understand?
Jamestf347
02-03-2009, 08:05 PM
What link are you talking about?
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.