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
  #11  
Old 14-May-2004, 11:47
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

C++


well i am kind of getting wht u say but what i cant understand ...... ??: but i am getting an error in void printResults arguments...is it the way i put in the sum and avg as arguments and why is sum and avg int and not double????????
  #12  
Old 14-May-2004, 11:51
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

ME again.....lol....


ws wondering why dont we need the outfile????? since we got to put the final results in the output file???
  #13  
Old 14-May-2004, 11:55
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
yes you are right, the average would certainly need to be double! when i copied the original program, i encountered some errors but they were due to misspellings and misplaced quotes... so i fixed those. like i said before, first get the program working without the file i/o, and then the file functionality to make it less confusing the classifyNumbers function is misspelled in its defnition, also you need to delete the quote right before zerocount in the printResults function. i think those are the only errors.
edit: ah yes when you call your printResults function, you are only passing it 3 parameters in main, when it should accept 5

Yes you need to have a file to output your results to, according to your program requirements, so we have declared ofstream outfile("avg.txt"); avg.txt will be the file taht will store the results. In addition to storing your results to the file, you also need to display them to the screen at the same time. so don't get rid of those cout statements. just leave them as they are, and create outfile<< statements without endl since the file doesnt need any new lines
  #14  
Old 14-May-2004, 12:06
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

This is wht i have so far....


After correcting the mistakes and doing what u asked me to this is what i have so far....
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

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

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

  ifstream infile("data.txt");
  ofstream outfile("avg.txt");
 
 while(!infile.eof())
   {
    can read from it.
     getNumber(number,infile);

     cout<<number<<" ";
     classifyNumber(number, zeros, odds, evens);
   }

   cout<<endl;
   printResults(zeros, odds, evens);

return 0;
}

void initialize(int& zeroCount, int& oddCount, int& evenCount)
  {
   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 printResults(int oddCount, int evenCount, double sumCount, double avgCount)
  {
   outFile<<"There are "<<evenCount<<" evens, ";
   outFile<<"Total number of odds are: "<<oddCount;
   outFile<<"The sum of the numbers are: "<<sumCount;
   outFile<<"The average of the numbers is: "<<avgCount<<endl;
  } 
  #15  
Old 14-May-2004, 12:12
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
change printResults(zeros, odds, evens);
to printResults(zeros,odds,evens,sum,avg,outfile); in main. it should accept 6 parameters. also you need to add a function that actually calculates average and sum.
printResults needs to receive the outfile as a parameter : ofstream &outf
also in printResults you don't need <<endl in outf<< statements since you are storing the data to file. also get rid of those strings inside the quotes in outf<< statement. all you need to do is put in data, thats it.

for example
outf<<oddcount<<evencount<<zerocount<<sumcount<<av gcount;
  #16  
Old 14-May-2004, 12:52
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

grrrrrrrrrrr


ahhhhhhhhhh please tell me how long did it take for u to learn c++...loooool.... coz it might make me feel better knowing it takes a longgggggggggg time to learn anything in c++ hun????

anyways i did change the parameters in printResults like u said and the outfile statements BUT......... it is giving me errors for printResults parameters in main..........I am sure ur having quite a laugh at me for my stupidity...well what can i say just started this c++ and i knw got a loooooong ways to go....heeeheee...

Quote:
Originally Posted by machinated
change printResults(zeros, odds, evens);
to printResults(zeros,odds,evens,sum,avg,outfile); in main. it should accept 6 parameters. also you need to add a function that actually calculates average and sum.
  #17  
Old 14-May-2004, 13:01
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
im still learning c++. i always will. i started out in high school which was 5 years ago. altho within a year i learned most of everything i know now so after high school my progress has been.... very slow.

post your fixed code again and lets see whats causing those errors.
  #18  
Old 14-May-2004, 13:09
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road
Question

lol.....


well i see its been a long time.... ur good but u see i just staerted abt a month ago...loooooooool....
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();
void printResults(int oddCount, int evenCount, ofstream &outfile);

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

  ifstream infile("data.txt");
  ofstream outfile("avg.txt");
 
 //eof() is a function which returns true when it detects end of file
//so we want to run the while loop till the file ends (while it hasn't ended).
 while(!infile.eof())
   {
     //along with the number, we want to pass the file to get number so it can read from it.
     getNumber(number,infile);

     cout<<number<<" ";
     classifyNumber(number, zeros, odds, evens);
     calculation();
   }

   cout<<endl;
   printResults(zeros, odds, evens, sum, avg, outfile);

return 0;
}

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

void getNumber(int& num, ifstream &infile)
  {
   infile>>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()
{
  sum = sum + number;
  avg = sum / number;
}
void printResults(int oddCount, int evenCount, outfile)
  {
   outfile<<<<oddcount<<evencount<<zerocount<<sumcount<<avgcount;
  } 

Quote:
Originally Posted by machinated
im still learning c++. i always will. i started out in high school which was 5 years ago. altho within a year i learned most of everything i know now so after high school my progress has been.... very slow.

post your fixed code again and lets see whats causing those errors.
  #19  
Old 14-May-2004, 13:38
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
there are two kinds of parameters: formal parameters, actual paramters

formal parameters are listed in function definitions and they should be accompanied with their proper data types:

void blah(int &a, int b)
{
body
}

actual paramters are passed to the function when it is called and they are passed without their data type:

blah(aa,bb);

it is recommended in c/c++ that we use different names for actual and formal parameters.

function declaration is just a declaration at the top of your file to tell the
rest of your file that the specified function is defined somewhere in your file:
void blah(int &a, int b);

on a side note: in your function declaration you don't actually need the names. all you need are types of your parameters.
void blah(int&, int);
although it never hurts to use the names as well to keep things less confusing.

but in your function definition you need the names as well as types:
void blah(int &a, int b)
{
body
}
------------------------------------------------------------------
your function call:
CPP / C++ / C Code:
printResults(zeroes,odds,evens,sum,avg,outfile);
doesnt match your function declaration:
CPP / C++ / C Code:
void printResults(int oddCount, int evenCount, ofstream &outfile);

furthermore, your declaration itself doesnt match your definition:
CPP / C++ / C Code:
void printResults(int oddCount, int evenCount, outfile) //wheres the type and reference for outfile?
  {
   outfile<<<<oddcount<<evencount<<zerocount<<sumcount<<avgcount;
  }

now lets make it all match
in your function call you are passing in 6 actual parameters, so your function declaration and definition should have 6 corresponding formal parameters:
CPP / C++ / C Code:
void printResults(int zeroCount,int oddCount, int evenCount,double sumCount, double avgCount, ofstream &outf);

void printResults(int zeroCount,int oddCount, int evenCount, double sumCount, double avgCount,ofstream &outf)
{
outf<<zeroCount<<" "<<oddCount<<" "<<evenCount<<" "<<sumCount<<" "<<avgCount;
}
my apologies! i completely forgot that you should put spaces in between different numbers in text file in order to differentiate one number from another. for example if you had 1 evens 2 odds and 3 zeroes, your function should store it in the text file in following format: 1 2 3
instead of 123 which is what will happen if we dont put in spaces
also you should change the formal parameter name in function getNumber() from infile to something else.
  #20  
Old 14-May-2004, 14:11
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

well well....


its giving me a parse error for
CPP / C++ / C Code:
void printResults(int zeroCount,int oddCount, int evenCount,double sumCount, double avgCount, ofstream &outf);
{
  outf<<zeroCount<<" "<<oddCount<<" "<<evenCount<<" "<<sumCount<<" "<<avgCount;
}
whatever that parse error means???...
 


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:20.


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