The Orange Mane -  a Denver Broncos Fan Community  

Go Back   The Orange Mane - a Denver Broncos Fan Community > Orange Mane Discussion > Orange Mane Central Discussion
Register FAQ Members List Calendar Chat Room Mark Forums Read



Reply
 
Thread Tools Display Modes
Old 07-10-2009, 04:27 PM   #1
Hotwheelz
Perennial Pro-bowler
 
Hotwheelz's Avatar
 

Join Date: Mar 2006
Posts: 841
Default C++ Monkeys--Help

This program is supposed to read numbers from a .txt file, add, average and display the highest and lowest value. BUT LOOK AT THIS ****.

Here's the code:

Code:
// Assignment 5b Number Analysis.cpp : main project file.

#include "stdafx.h"
#include 
#include 
#include 

using namespace std;

int main()
{
	const int SIZE = 12;
	int count;
	double numbers[SIZE];
	double avg;
	double total;
	double highest;
	double lowest;
	ifstream inputFile;

	inputFile.open("test.txt");

	for (count = 0; count < SIZE; count++)
		inputFile >> numbers[count];


	inputFile.close();
	cout << "The numbers are: ";
	for (count = 0; count < SIZE; count++)
		cout << numbers[count] << " \n";

	for (count = 0; count < SIZE; count++) // calculates total 
		total += numbers[count];
		
	
	for (count = 0; count < SIZE; count++) // calculates average 
		total += numbers[count];
		avg = total / SIZE;

	for (count = 1; count < SIZE; count++) // calculates highest
	{
		if (numbers[count] > highest)
			highest = numbers[count];
	}
	
	for (count = 1; count < SIZE; count++)// calculates lowest
	{
		if (numbers[count] < lowest)
			lowest = numbers[count];
	}

	cout << "The total sum of the numbers is " << total << "\n";
	cout << "The average of the numbers is " << avg << "\n";
	cout << "The highest number is " << highest << "\n";
	cout << "The lowest number is " << lowest << "\n";





    return 0;
}
Attached Images
 
Hotwheelz is offline   Reply With Quote
Sponsored Links
Old 07-10-2009, 04:36 PM   #2
tsiguy96
Ring of Famer
 
tsiguy96's Avatar
 
New to the Forum

Join Date: Jan 2007
Posts: 12,700

Adopt-a-Bronco:
None
Default

it appears you didnt carry the 3.
tsiguy96 is offline   Reply With Quote
Old 07-10-2009, 05:10 PM   #3
Hotwheelz
Perennial Pro-bowler
 
Hotwheelz's Avatar
 

Join Date: Mar 2006
Posts: 841
Default

Quote:
Originally Posted by tsiguy96 View Post
it appears you didnt carry the 3.
...
Hotwheelz is offline   Reply With Quote
Old 07-10-2009, 06:12 PM   #4
Rock Chalk
Cheeky Bastards
 
Rock Chalk's Avatar
 
Laus Deo

Join Date: Oct 2001
Location: The Backside of the Internet
Posts: 29,750

Adopt-a-Bronco:
Chris Harris
Default

Quote:
Originally Posted by Hotwheelz View Post
...
I havent used C++ for years but you are missing some information needed to help you.

For instance, in test.txt, how are the numbers positioned? What is there delimiter? Is it 1,2,3,4,5,6,7,8 or something like 1 2 3 4 5 6 7 8 or this
1
2
3
4
5
6
7
8

Im more of a PHP guy which is loosely based on C++ structure and this is what I would do (logic only, not the includes of file open code)

Find corresponding C++ functions to accomplish this.
Code:
// [delimiter] being whatever delimites your numbers from one another
$numbers[] = explode("[delimiter]", $fileText)
$lowestNumber = min($numbers);
$highestNumber = max($numbers);
foreach($numbers as $number)
{
	$total += $number;
	$count++;
}
$average = $total/$count;
echo "Average: ".$average
echo "Total: ".$total;
echo "Lowest Number: ".$lowestNumber;
echo "Highest Number: ".$highestNumber;
Rock Chalk is offline   Reply With Quote
Old 07-10-2009, 06:29 PM   #5
Hotwheelz
Perennial Pro-bowler
 
Hotwheelz's Avatar
 

Join Date: Mar 2006
Posts: 841
Default

Quote:
Originally Posted by Rock Chalk View Post
I havent used C++ for years but you are missing some information needed to help you.

For instance, in test.txt, how are the numbers positioned? What is there delimiter? Is it 1,2,3,4,5,6,7,8 or something like 1 2 3 4 5 6 7 8 or this
1
2
3
4
5
6
7
8

Im more of a PHP guy which is loosely based on C++ structure and this is what I would do (logic only, not the includes of file open code)

Find corresponding C++ functions to accomplish this.
Code:
// [delimiter] being whatever delimites your numbers from one another
$numbers[] = explode("[delimiter]", $fileText)
$lowestNumber = min($numbers);
$highestNumber = max($numbers);
foreach($numbers as $number)
{
	$total += $number;
	$count++;
}
$average = $total/$count;
echo "Average: ".$average
echo "Total: ".$total;
echo "Lowest Number: ".$lowestNumber;
echo "Highest Number: ".$highestNumber;
It's
1
2
3
4
5
6
7
8
9
10
11
12

It seems to be printing their memory address instead of the numbers.
Hotwheelz is offline   Reply With Quote
Old 07-10-2009, 06:33 PM   #6
Rock Chalk
Cheeky Bastards
 
Rock Chalk's Avatar
 
Laus Deo

Join Date: Oct 2001
Location: The Backside of the Internet
Posts: 29,750

Adopt-a-Bronco:
Chris Harris
Default

Do you know how to read from a file and put the data from each line on an array?

Its easier that way. I cant tell you how to do it in C++ but the logic of it is shorter.

C++ surely has a min and max value for array functions too. It is afterall, one of the most powerful upper-level languages ever created.
Rock Chalk is offline   Reply With Quote
Old 07-10-2009, 06:41 PM   #7
Hotwheelz
Perennial Pro-bowler
 
Hotwheelz's Avatar
 

Join Date: Mar 2006
Posts: 841
Default

Quote:
Originally Posted by Rock Chalk View Post
Do you know how to read from a file and put the data from each line on an array?

Its easier that way. I cant tell you how to do it in C++ but the logic of it is shorter.

C++ surely has a min and max value for array functions too. It is afterall, one of the most powerful upper-level languages ever created.
Yes, that's what this is for:

Code:
	inputFile.open("test.txt");

	for (count = 0; count < 12; count++)
		inputFile >> numbers[count];


	inputFile.close();
	cout << "The numbers are: ";
	for (count = 0; count < 12; count++)
		cout << numbers[count] << " \n";
The other functions are taken from another program where they're dandy.
Hotwheelz is offline   Reply With Quote
Old 07-10-2009, 06:45 PM   #8
summerdenver
Ring of Famer
 
summerdenver's Avatar
 
Rebuilding both offense and defense

Join Date: Aug 2006
Location: Seattle
Posts: 1,813

Adopt-a-Bronco:
Peyton Hillis
Default

The data in the file will character but you are readin directly as a double. Also as Rock pointed out you are not readin each number separately and I don't think it will work.

If each line has a new number you can try something like this.

(watchout for compilation errors as i typed it directly)

char value[10];

bool first = true;

int count = 0;
double sum, min, max = 0;

while(ifs.good())
{
count++;

ifs.getline(value,10);
double temp = atof(value);

total += temp;

if(first)
{
first = false;
min = temp;
max = temp;
}
else
{
if (min < temp)
min = temp;

if (temp > max)
max = temp;
}

}

cout >> "average:" >> (total/count)

*Test your program with negative and postive numbers, same number repeated, all zeroes etc.
summerdenver is offline   Reply With Quote
Old 07-10-2009, 06:47 PM   #9
BroncoBuff
***************
 
BroncoBuff's Avatar
 

Join Date: Aug 2005
Location: Seattle
Posts: 25,834

Adopt-a-Bronco:
MALIK+QUANTERUS
Default

You computer programmers really impress me.


I'm impressed that you're nerds.
BroncoBuff is offline   Reply With Quote
Old 07-10-2009, 06:56 PM   #10
Hotwheelz
Perennial Pro-bowler
 
Hotwheelz's Avatar
 

Join Date: Mar 2006
Posts: 841
Default

[QUOTE=summerdenver;2468855]The data in the file will character but you are readin directly as a double. Also as Rock pointed out you are not readin each number separately and I don't think it will work.


It compiles and reads them fine. The output is the problem.
Hotwheelz is offline   Reply With Quote
Old 07-10-2009, 07:31 PM   #11
BroncoDoug
Ring of Famer
 
BroncoDoug's Avatar
 
R.I.P. D-Will and D-Nash

Join Date: Oct 2007
Location: Moline, IL
Posts: 2,819

Adopt-a-Bronco:
Von Miller
Default

oh that all brings me back to college! to bad i've forgotten most if it now
BroncoDoug is offline   Reply With Quote
Old 07-10-2009, 08:38 PM   #12
atomicbloke
Ring of Famer
 
atomicbloke's Avatar
 
La 12 Boca Juniors

Join Date: Nov 2005
Location: Albuquerque, NM
Posts: 2,341

Adopt-a-Bronco:
Offensive Line
Default

Programming is like sex.

Sure, it might have some useful purpose, but that's not why we do it....
atomicbloke is offline   Reply With Quote
Old 07-10-2009, 09:08 PM   #13
watermock
"Hoodie Jr"
 
watermock's Avatar
 
"Hug me!"

Join Date: Jul 2001
Location: Hot Springs, Ouachitah
Posts: 76,813
Default

I was climbing the Pyamid of Tetcuachahn whan I should of been studying Algebra, luckily, I flunked out of Fortran too.
watermock is offline   Reply With Quote
Old 07-10-2009, 09:33 PM   #14
Rock Chalk
Cheeky Bastards
 
Rock Chalk's Avatar
 
Laus Deo

Join Date: Oct 2001
Location: The Backside of the Internet
Posts: 29,750

Adopt-a-Bronco:
Chris Harris
Default

Quote:
Originally Posted by atomicbloke View Post
Programming is like sex.

Sure, it might have some useful purpose, but that's not why we do it....
+1
Rock Chalk is offline   Reply With Quote
Old 07-10-2009, 10:16 PM   #15
ØrangeÇrush
Aaaayh!!!
 

Join Date: Mar 2004
Posts: 2,360
Default

you open the input file, but you don't read the lines. you just add the memory address to the array.


file.open("test.txt");

something like

int count = 0;

foreach(string line in file.readline())
{

number[count] += [int]line; //casting string to int.

}

or

for (int i = 0; i < file.size; i++)

{
number[i] = [int]file.readline();
}



Quote:
Originally Posted by Hotwheelz View Post
This program is supposed to read numbers from a .txt file, add, average and display the highest and lowest value. BUT LOOK AT THIS ****.

Here's the code:

Code:
// Assignment 5b Number Analysis.cpp : main project file.

#include "stdafx.h"
#include 
#include 
#include 

using namespace std;

int main()
{
	const int SIZE = 12;
	int count;
	double numbers[SIZE];
	double avg;
	double total;
	double highest;
	double lowest;
	ifstream inputFile;

	inputFile.open("test.txt");

	for (count = 0; count < SIZE; count++)
		inputFile >> numbers[count];


	inputFile.close();
	cout << "The numbers are: ";
	for (count = 0; count < SIZE; count++)
		cout << numbers[count] << " \n";

	for (count = 0; count < SIZE; count++) // calculates total 
		total += numbers[count];
		
	
	for (count = 0; count < SIZE; count++) // calculates average 
		total += numbers[count];
		avg = total / SIZE;

	for (count = 1; count < SIZE; count++) // calculates highest
	{
		if (numbers[count] > highest)
			highest = numbers[count];
	}
	
	for (count = 1; count < SIZE; count++)// calculates lowest
	{
		if (numbers[count] < lowest)
			lowest = numbers[count];
	}

	cout << "The total sum of the numbers is " << total << "\n";
	cout << "The average of the numbers is " << avg << "\n";
	cout << "The highest number is " << highest << "\n";
	cout << "The lowest number is " << lowest << "\n";





    return 0;
}
ØrangeÇrush is offline   Reply With Quote
Old 07-10-2009, 10:16 PM   #16
summerdenver
Ring of Famer
 
summerdenver's Avatar
 
Rebuilding both offense and defense

Join Date: Aug 2006
Location: Seattle
Posts: 1,813

Adopt-a-Bronco:
Peyton Hillis
Default

[QUOTE=Hotwheelz;2468864]
Quote:
Originally Posted by summerdenver View Post
The data in the file will character but you are readin directly as a double. Also as Rock pointed out you are not readin each number separately and I don't think it will work.


It compiles and reads them fine. The output is the problem.
ok. I checked that >> is overloaded so the parsing should work. I think the file path is incorrect or inaccessible and since you are not doing any error checking we don't know if it is working. Try this instead -

** Remember to give the full path of the file.


int count;
double numbers[SIZE];
double avg;
double total;
double highest;
double lowest;
ifstream inputFile;

inputFile.open("c:\\Mydirectory\\test.txt", ifstream::in);

count = 0;
while (inputFile.good() && count < SIZE)
{
inputFile >> numbers[count];
count++;
}


inputFile.close();
cout << "The numbers are: ";
for (count = 0; count < SIZE; count++)
cout << numbers[count] << " \n";
summerdenver is offline   Reply With Quote
Old 07-10-2009, 10:17 PM   #17
kmonty
BT Blogger
 
kmonty's Avatar
 

Join Date: Jun 2007
Location: Flagstaff, AZ
Posts: 1,112

Adopt-a-Bronco:
Sweet Feet
Default

Quote:
Originally Posted by Hotwheelz View Post
It's
1
2
3
4
5
6
7
8
9
10
11
12

It seems to be printing their memory address instead of the numbers.
If that's the case, in your first loop, dereference it using an asterisk.

*inputFile >> numbers[count]

Something like that... not sure if that will work, pointers (&) and dereferencing (*) were why I never fully got C++.
kmonty is offline   Reply With Quote
Old 07-10-2009, 10:34 PM   #18
ØrangeÇrush
Aaaayh!!!
 

Join Date: Mar 2004
Posts: 2,360
Default

the ifstream uses the method

getline();


so it would look like

int counter = 0;

while(!inputFile.eof())
{

numbers[counter] = (int) inputFile.getline();
counter++;
}

that should read the numbers into separate parts of your array.

Last edited by ØrangeÇrush; 07-10-2009 at 10:40 PM..
ØrangeÇrush is offline   Reply With Quote
Old 07-10-2009, 10:46 PM   #19
kmonty
BT Blogger
 
kmonty's Avatar
 

Join Date: Jun 2007
Location: Flagstaff, AZ
Posts: 1,112

Adopt-a-Bronco:
Sweet Feet
Default

Quote:
Originally Posted by ØrangeÇrush View Post
the ifstream uses the method

getline();


so it would look like

int counter = 0;

while(!inputFile.eof())
{

numbers[counter] = (int) inputFile.getline();
counter++;
}

that should read the numbers into separate parts of your array.
Definitely on the right track I think, except getline() returns a c-string, and you'll probably have to use atoi(str) to get an int.
kmonty is offline   Reply With Quote
Old 07-10-2009, 10:52 PM   #20
ØrangeÇrush
Aaaayh!!!
 

Join Date: Mar 2004
Posts: 2,360
Default

Quote:
Originally Posted by kmonty View Post
Definitely on the right track I think, except getline() returns a c-string, and you'll probably have to use atoi(str) to get an int.
good call!

while(!inputFile.eof())
{

numbers[counter] = atoi(inputFile.getline());
counter++;
}
ØrangeÇrush is offline   Reply With Quote
Old 07-11-2009, 11:04 AM   #21
Karenin
Perennial Pro-bowler
 
New to the Forum

Join Date: Oct 2007
Posts: 868

Adopt-a-Bronco:
None
Default

I have an idea for OP: do your own homework.
Karenin is offline   Reply With Quote
Old 07-11-2009, 12:49 PM   #22
ghwk
Ring of Famer
 
ghwk's Avatar
 
Survivor survivor!

Join Date: Jul 2008
Posts: 4,641

Adopt-a-Bronco:
Peyton's C3-C5
Default

Careful this is a North Korean exploit plot.
ghwk is offline   Reply With Quote
Old 07-11-2009, 01:13 PM   #23
worm
Great moments are born...
 
worm's Avatar
 
...from great opportunity

Join Date: Dec 2002
Posts: 3,902

Adopt-a-Bronco:
#20
Default

summer school sucks
worm is offline   Reply With Quote
Old 07-11-2009, 01:29 PM   #24
Mr. Elway
cakn patna
 

Join Date: Jul 2009
Posts: 964
Default

Quote:
Originally Posted by BroncoBuff View Post
You computer programmers really impress me.


I'm impressed that you're nerds.
...and predictably, there goes the only joke that any non-programmer in history has ever made about any programmer.
Mr. Elway is offline   Reply With Quote
Old 07-11-2009, 01:52 PM   #25
worm
Great moments are born...
 
worm's Avatar
 
...from great opportunity

Join Date: Dec 2002
Posts: 3,902

Adopt-a-Bronco:
#20
Default

Quote:
Originally Posted by Mr. Elway View Post
...and predictably, there goes the only joke that any non-programmer in history has ever made about any programmer.
Are there any real programmers here?

The ones I can see in this thread are people that either:
1) have taken the class recently in school
2) took a philosophy 101 class once and think they can apply it to anything
3) have given up working with any real language in favor of the soup du jours such as PHP

In which case, Buffs impression is even more applicable.
worm is offline   Reply With Quote
Reply

Thread Tools
Display Modes



Forum Jump


All times are GMT -7. The time now is 02:00 AM.


Denver Broncos