PDA

View Full Version : C++ Monkeys--Help


Hotwheelz
07-10-2009, 04:27 PM
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:

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

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>

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;
}

tsiguy96
07-10-2009, 04:36 PM
it appears you didnt carry the 3.

Hotwheelz
07-10-2009, 05:10 PM
it appears you didnt carry the 3.

...

Rock Chalk
07-10-2009, 06:12 PM
...

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.

// [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;

Hotwheelz
07-10-2009, 06:29 PM
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.

// [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.

Rock Chalk
07-10-2009, 06:33 PM
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.

Hotwheelz
07-10-2009, 06:41 PM
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:


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.

summerdenver
07-10-2009, 06:45 PM
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.

BroncoBuff
07-10-2009, 06:47 PM
You computer programmers really impress me.


I'm impressed that you're nerds.

Hotwheelz
07-10-2009, 06:56 PM
[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.

BroncoDoug
07-10-2009, 07:31 PM
oh that all brings me back to college! to bad i've forgotten most if it now :(

atomicbloke
07-10-2009, 08:38 PM
Programming is like sex.

Sure, it might have some useful purpose, but that's not why we do it....

watermock
07-10-2009, 09:08 PM
I was climbing the Pyamid of Tetcuachahn whan I should of been studying Algebra, luckily, I flunked out of Fortran too.

Rock Chalk
07-10-2009, 09:33 PM
Programming is like sex.

Sure, it might have some useful purpose, but that's not why we do it....

+1

ØrangeÇrush
07-10-2009, 10:16 PM
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();
}



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:

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

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>

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;
}

summerdenver
07-10-2009, 10:16 PM
[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.

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";

kmonty
07-10-2009, 10:17 PM
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++.

ØrangeÇrush
07-10-2009, 10:34 PM
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.

kmonty
07-10-2009, 10:46 PM
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.

ØrangeÇrush
07-10-2009, 10:52 PM
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++;
}

Karenin
07-11-2009, 11:04 AM
I have an idea for OP: do your own homework.

ghwk
07-11-2009, 12:49 PM
Careful this is a North Korean exploit plot.

worm
07-11-2009, 01:13 PM
summer school sucks

Mr. Elway
07-11-2009, 01:29 PM
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. :clown:

worm
07-11-2009, 01:52 PM
...and predictably, there goes the only joke that any non-programmer in history has ever made about any programmer. :clown:

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 :clown:

In which case, Buffs impression is even more applicable.

atomicbloke
07-11-2009, 01:56 PM
3) have given up working with any real language in favor of the soup du jours such as PHP :clown:



And what do you mean by a real language?

worm
07-11-2009, 02:02 PM
And what do you mean by a real language?

LOL Don't mean to invalidate your sense of worth because you use markup languages. I am sure you lead a very fulfilling life.

I was actually just pulling AR's chain. Hence the clown.

atomicbloke
07-11-2009, 02:06 PM
LOL Don't mean to invalidate your sense of worth because you use markup languages. I am sure you lead a very fulfilling life.

I was actually just pulling AR's chain. Hence the clown.

Actually, I am using Lisp now :)

And pray tell me that if for some applications, markup languages do the job best, would you still massage your ego doing C? then you should go all the way and only code in assembly, shouldn't you?

I smell a programming Nazi :)

worm
07-11-2009, 02:27 PM
Actually, I am using Lisp now :)

And pray tell me that if for some applications, markup languages do the job best, would you still massage your ego doing C? then you should go all the way and only code in assembly, shouldn't you?

I smell a programming Nazi :)

If Alec was coding in C++ instead of PHP, I would have still made fun of him.

I don't believe in religious wars. I believe in harassment.

But yeah. I also believe there is the right tool for the job and it isn't always going to be the same.

atomicbloke
07-11-2009, 03:21 PM
If Alec was coding in C++ instead of PHP, I would have still made fun of him.

I don't believe in religious wars. I believe in harassment.

But yeah. I also believe there is the right tool for the job and it isn't always going to be the same.

I was harassing you too, chil :)

ØrangeÇrush
07-16-2009, 05:41 PM
for anything i need done fast, Python is the way to go. If I have a request for an app, and the user is a GUI monkey, Iron Python comes to the rescue. I don't know if anyone else uses it, but its Python in a Visual Studio IDE. Lovely!

http://www.codeplex.com/IronPythonStudio