LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2002, 11:10 PM   #1
PenguinMan
LQ Newbie
 
Registered: Aug 2002
Posts: 5

Rep: Reputation: 0
Unhappy Why does it return zero when counting?


The program should see if you entered an odd /even or a zero and display how many of each you inputed. On the code here i dont have a loop yet.

Here is the code and the output.
#include <iostream.h>

int classifynumber (int number, int zerocount, int oddcount, int evencount);


int main () {


int zerocount;
zerocount=0;

int oddcount;
oddcount=0;

int evencount;
evencount=0;

cout<<"Enter 1 number"<<endl;
int number;
cin>>number;




int classifynumber (int number, int zeros, int odds, int evens);

cout <<"the number you entered "<< number <<" there are evecount: "<< evencount <<" oddcount " <<oddcount <<" zerocount: "<< zerocount<<endl;
return 0;

}



int classifynumber(int number, int zerocount, int oddcount, int evencount)
{

switch(number%2)
{
case 0:
evencount++;
if (number==0)
zerocount++;
break;
case 1:
case -1: oddcount++;
}

return number;
}

===================================================
Enter 1 number
3
the number you entered 3 there are evecount: 0 oddcount 0 zerocount: 0

----------------------------------------------
Program exited successfully with errcode (0)
Press the Enter key to close this terminal ...


what am i doing wrong?
 
Old 10-24-2002, 03:49 AM   #2
ferrous
LQ Newbie
 
Registered: Dec 2001
Location: Atlanta
Distribution: Slackware
Posts: 16

Rep: Reputation: 0
Answer: C++ defaults to pass-by-value parameters. You want to use pass-by-reference for your classifynumber function.

Simple answer: You want to put ampersands in front of zerocount, oddcount, and evencount in the declaration (and prototype) of classifynumber.
 
Old 10-24-2002, 09:56 AM   #3
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Re: Why does it return zero when counting?

Based on what you provided, here is a running program.

Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void classifynumber (const int number, int &zerocount, int &oddcount, int &evencount);


int main () {
    
  int zerocount = 0;
  int oddcount = 0;
  int evencount = 0;
  int number;

  cout << "Enter a number: ";
  cin >> number;
	
  classifynumber (number, zerocount, oddcount, evencount);

  cout <<"the number you entered "<< number <<" there are evencount: "<< evencount <<" oddcount " <<oddcount <<" zerocount: "<< zerocount<<endl;
  
  return 0;
	
}



void classifynumber(const int number, int &zerocount, int &oddcount, int &evencount)
{
	
  switch(number % 2)
    {
    case 0:
      { 
	evencount++;
	if ( number == 0)
	  zerocount++;
	break;
      }
    default:
      {
	oddcount++;
	break;
      }
    }
			
}
 
Old 12-20-2017, 01:00 PM   #4
theheapWalker
LQ Newbie
 
Registered: Dec 2017
Posts: 21

Rep: Reputation: Disabled
your code was copied and an attempt was made to compile it.

gcc didn't recognise iostream.h as a headerfile.

What other header file (that is equivalent to iostream.h) will gcc recognise?
 
Old 12-20-2017, 01:18 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
@theheapWalker:

You continue to exhibit a pattern of seeking out old posts and replying to them without providing any useful information or questions of your own.

If you have questions of your own please post them to your own thread with proper context. Please see the Site FAQ for guidance in participation on this forum.

If this pattern continues your posting privileges may be suspended.
 
Old 12-21-2017, 12:12 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@theheapWalker: It is C++, so compile it with g++ But you should know that C++ is still in the experimental phase, so it keeps changing: new compilers are allowed not to compile old code, and vice versa.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
4 days and counting tomank Linux - Newbie 7 04-06-2004 10:22 AM
And counting... MasterC General 18 09-24-2003 05:07 AM
counting dummyagain Programming 3 09-24-2003 02:28 AM
counting tool stand Linux - Software 3 08-23-2003 12:54 AM
counting characters Snake007uk Programming 13 05-10-2002 03:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration