GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #21  
Old 14-May-2004, 14:18
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

thats fine....


well if igured out what the parse error was...thanz

[quote=homz]its giving me a parse error for
  #22  
Old 14-May-2004, 15:11
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
now create a text file in the same directory as your program source file and name it data.txt. put in numbers in this format:

15 25 23 17 18 0 5 0 0

make sure not to hit enter after you are done inputting numbers.
  #23  
Old 14-May-2004, 16:22
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

file going hay wire


well i did and when i compile and run it.... goes nutz...... like infinite loop.... .....

Quote:
Originally Posted by machinated
now create a text file in the same directory as your program source file and name it data.txt. put in numbers in this format:

15 25 23 17 18 0 5 0 0

make sure not to hit enter after you are done inputting numbers.
  #24  
Old 14-May-2004, 16:35
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 168
dabigmooish will become famous soon enough
hey just a little FYI when you run your program it counts 0 as both an even number and a zero. I.E. evenCount is upped one and so is zeroCount.

instead of :
CPP / C++ / C Code:
switch(num % 2)
    {
    case 0: evenCount++;
    if(num == 0) zeroCount++;
                      break;
    case 1: 
    case -1: oddCount++;
    }

use:

CPP / C++ / C Code:
switch(num % 2)
    {
    case 0: 
		if(num == 0){
			zeroCount++;
		}
		else
			evenCount++;
		break;
    case 1: 
    case -1: oddCount++;
    }

also don't sweat all the problems your running into. C++ is one of the hardest languages to learn. I've been learning it for 3 years and I still run into trouble all the time.
  #25  
Old 14-May-2004, 17:33
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by dabigmooish
hey just a little FYI when you run your program it counts 0 as both an even number and a zero. I.E. evenCount is upped one and so is zeroCount.

instead of :
CPP / C++ / C Code:
switch(num % 2)
    {
    case 0: evenCount++;
    if(num == 0) zeroCount++;
                      break;
    case 1: 
    case -1: oddCount++;
    }

use:

that's precisely the point. 0 IS an even number. theres nothing wrong with this switch.
Homz your data.txt file is not correctly placed. if your program can't find it, it will go berserk since we don't have any error checking in place. if you want, right before the while(!infile.eof()) loop, you can put:
CPP / C++ / C Code:
if(!infile)
{
   cout<<"file doesnt exist";
   exit(1);
}
this will end your program abruptly if the file doesnt exist.

also just place your data.txt file in c:\ and make the following changes in your program:

CPP / C++ / C Code:
ifstream infile("c:\\data.txt");
ofstream outfile("c:\\avg.txt");
this way your input and output files will reside in c:\ and you won't have to guess where your program will read the file from.

also i just noticed that your calculation() function is incorrect.
CPP / C++ / C Code:
void calculation()
{
  sum = sum + number;
  avg = sum / number;
}

you need to pass it the parameters in order for it to actually use them.
also average wouldnt be sum/number it would be sum / how many numbers there are

you need to sum all the numbers within the while loop in main and then pass it to calculation along with odds, evens, and avg.
CPP / C++ / C Code:
void calculate(double &avgCount, double sumCount,int oddCount,int evenCount)
{
  avgCount = sumCount/(oddCount+evenCount);
}
Last edited by machinated : 14-May-2004 at 18:31.
  #26  
Old 15-May-2004, 11:18
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

doesnt do it????


well machinated its still not doing it with all these changes..... it wont even read or output anything in the files????????? and still going hay wire... thanks dabigmooish for your input too in this problem sloving.... i hope one day i can reach where you all are...lol..right now it looks glim chances though..heeheee

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void initialize(int& zeroCount, int& oddCount, int& evenCount, double& sumCount, double& avgCount);
void getNumber(int& num, ifstream &inf);
void classifyNumber(int num, int& zeroCount, int& oddCount, int& evenCount);
void calculation(double &avgCount, double sumCount,int oddCount,int evenCount);
void printResults(int zeroCount,int oddCount, int evenCount,double sumCount, double avgCount, ofstream &outf);

int main()
{
  int counter;
  int number;
  int zeros;
  int odds;
  int evens;
  double sum;
  double avg;
  
  initialize(zeros, odds, evens, sum,avg);

  ifstream infile("c:\\data.txt");
  ofstream outfile("c:\\avg.txt");
  
   if(!infile)
{
   cout<<"file doesnt exist";
   exit(1);
   
}

// Do While not EOF
 while(!infile.eof())
   {
     getNumber(number,infile);

     cout<<number<<" ";
     classifyNumber(number, zeros, odds, evens);
     calculation(avg, sum, odds, evens);
   }
  
   cout<<endl;
   printResults(zeros, odds, evens, sum, avg, outfile);
   
   system("pause");
   return 0;
}

void initialize(int& zeroCount, int& oddCount, int& evenCount, double& sumCount, double& avgCount)
  {
   zeroCount = 0;
   oddCount = 0;
   evenCount = 0;
  }

void getNumber(int& num, ifstream &inf)
  {
   inf>>num;
  }

void classifyNumber(int num, int& zeroCount, int& oddCount, int& evenCount)
  {
  switch(num % 2)
    {
    case 0: evenCount++;
    if(num == 0) zeroCount++;
                      break;
    case 1: 
    case -1: oddCount++;
    }
  }
  
void calculation(double &avgCount, double sumCount,int oddCount,int evenCount)
{
  avgCount = sumCount/(oddCount+evenCount);
}

void printResults(int zeroCount,int oddCount, int evenCount,double sumCount, double avgCount, ofstream &outf)
{
  outf<<zeroCount<<" "<<oddCount<<" "<<evenCount<<" "<<sumCount<<" "<<avgCount;
} 
  #27  
Old 15-May-2004, 11:43
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
First problem I see is your concept of returning values. For example:
CPP / C++ / C Code:
void calculation(double &avgCount, double sumCount,int oddCount,int evenCount)
{
  avgCount = sumCount/(oddCount+evenCount);
}
This routine returns a double and should be written:
CPP / C++ / C Code:
double calculation(double sumCount,int oddCount,int evenCount)
{
    double avgCount;
    avgCount = sumCount/(oddCount+evenCount);
    return avgCount;
}
or more simply:
CPP / C++ / C Code:
double calculation(double sumCount,int oddCount,int evenCount)
{
    return sumCount/(oddCount+evenCount);
}

Don't pass variables back thru parameters unless absolutely necessary.

Make a short version of your program to see what's happening to your input, something like:
CPP / C++ / C Code:
int main()
{
    int number;
    char dummy;  // temp variable
  
    ifstream infile("c:\\data.txt");
  
    if(!infile)
    {
        cout<<"file doesnt exist";
        exit(1);
    }

// Do While not EOF
    while(!infile.eof())
    {
        number = getNumber(infile);
        cout<<number<<" ";
    }
    cin >> dummy;  // don't use system("pause"); 
    return 0;
}

int getNumber(ifstream &inf)
{
    int num;
    inf >> num;
    return num;
}
If you need to, output the value of num immediately after you input it in getNumber()
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #28  
Old 15-May-2004, 14:14
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
homz, i just ran your code and it works perfectly except for two things:

your calculation function should not be in the while loop, it should be outside the while loop.
also inside the while loop, you need to add this statement:
sum+= number;
i'm posting the fixed code:
CPP / C++ / C Code:
while(!infile.eof())
   {
     getNumber(number,infile);

     cout<<number<<" ";
     classifyNumber(number, zeros, odds, evens);
     sum+=number;
   }
  calculation(avg, sum, odds, evens);
   cout<<endl;
   printResults(zeros, odds, evens, sum, avg, outfile);

your initialize function needs to initialize sum and avg as well:
CPP / C++ / C Code:
void initialize(int& zeroCount, int& oddCount, int& evenCount, double& sumCount, double& avgCount)
  {
   zeroCount = 0;
   oddCount = 0;
   evenCount = 0;
   sumCount = 0;
   avgCount = 0;
  }

with these changes your program should work as expected. if it's going haywire it's because of your file placement, and nothing else.
im sending you my data file, just paste it in c:\ and then run the program. AFter you run the program, you'll see another file in c:\ named avg.txt with these values: 2 5 10 525 35
thats it.

also heres my printResults function if you wanna output the results to screen as well:
CPP / C++ / C Code:
void printResults(double avgCount,double sumCount,int zeroCount, int oddCount, int evenCount,ofstream &outf)
{
   double numCount = evenCount + oddCount;
	outf<<zeroCount<<" "<<oddCount<<" "<<evenCount<<" "<<numCount<<" "<<sumCount<<" "<<avgCount;

	cout<<"There are "<<evenCount<<" evens, "<<"which also includes "
       <<zeroCount<<" zeros"<<endl;
   cout<<"Total number of odds are: "<<oddCount<<endl;
   cout<<"Total numbers are: "<<numCount<<endl;
   cout<<"Sum of all the numbers is: "<<sumCount<<endl;
   cout<<"Average of all the numbers is: "<<avgCount<<endl;
}
this function will actually output 6 values to avg.txt: 2 5 10 15 525 35
Attached Files
File Type: txt data.txt (42 Bytes, 23 views)
  #29  
Old 15-May-2004, 18:27
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road
Smile

lol...


hey machinated i tried compiling and then run it....it makes the avg.txt file but its empty....loooool...looks for some reason it does not like me .....hahahaaa... i think like you all are fed up with me by now so am i with this problem...lol...thanx though for your patience and kindness....

Quote:
Originally Posted by machinated
homz, i just ran your code and it works perfectly except for two things:

  #30  
Old 15-May-2004, 21:01
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by homz
hey machinated i tried compiling and then run it....it makes the avg.txt file but its empty....loooool...looks for some reason it does not like me .....hahahaaa... i think like you all are fed up with me by now so am i with this problem...lol...thanx though for your patience and kindness....
Did you try any of my suggestions? They were meant to pinpoint specific problems you may be having.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:23.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.