Undeclared identifiers???

This is a discussion on Undeclared identifiers??? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get the hang of calling functions correctly and everything. But when I compile this, it gives me ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    75

    Question Undeclared identifiers???

    I'm trying to get the hang of calling functions correctly and everything. But when I compile this, it gives me "undeclared identifers" errors for the variables in the functions. But I've identified them in main. What am I doing wrong???

    Code:
    #include <iostream>
    
    using namespace std;
    
    double growthRate(double);
    int estimatedPopulation(double);
    
    int main()
    {
    	int P;
    	double B;
    	double D;
    	int n;
    	double sub;
    	double total;
    
    	cout << "Enter current population: ";
    	cin >> P;
    	if (P < 2)
    	{
    		cout << "Current population must be greater than 2";
    		cout << "\nEnter current population: ";
    		cin >> P;
    	}
    		cout << endl;
    
    	cout << "Enter birth rate: ";
    	cin >> B;
    	if (B < 0)
    	{
    		cout << "Birth rate must be greater than or equal to 0";
    		cout << "\nEnter birth rate: ";
    		cin >> B;
    	}
    	cout << endl;
    
    	cout << "Enter death rate: ";
    	cin >> D;
    	if (D < 0)
    	{
    		cout << "Death rate must be greater than or equal to 0";
    		cout << "\nEnter death rate: ";
    		cin >> D;
    	}
    	cout << endl;
    
    	cout << "Enter the number of years for the projected population: ";
    	cin >> n;
    	cout << endl;
    
    	cout << "Growth rate = " << growthRate(sub) << " %" << endl;
    	cout << "Projected population after " << n << " years = ";
    	cout << estimatedPopulation(total) << endl;
    
    	return 0;
    }
    double growthRate(double sub)
    {
    	sub = B - D;
    	return sub;
    }
    
    int estimatedPopulation(double total)
    {
    	total = n * (P + ((P / 100) * growthRate(sub)));
    	return total;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    This is called scope. Variables that are defined in a function are not usable by another function because they are not in the scope of that function. To use a variable in another function you have to either A. Make it global. Meaning that anything can access it(Not ussually the best choice) or B. Pass it to the other function. Meaning pass it as a parameter either by value or reference(Better Choice).
    1000 posts yay
    Woop?

  3. #3
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    In addition to that, your program is too long, and that's unneccesary. You can compound declarations of the same type like so:
    Code:
    	int P, n;
    	double B, D, sub, total;
    By the way, "P,n,B, and D" are not particularly meaningful names for variables.

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Variables are local to the function that you define them in:

    I.e. a variable declared in main() can not be used in any other function except main()

    So, you can do something like
    Code:
    void function1(void)
    {
    	int x = 0;
    }
    
    void function2(void)
    {
    	int x = 1;
    }
    But not:
    Code:
    void function1(void)
    {
    	int x;
    }
    
    void function2(void)
    {
    	x = 2;
    }
    Your B, D, n, and P variables are not defined in their functions.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Krak
    In addition to that, your program is too long, and that's unneccesary. You can compound declarations of the same type like so:
    Code:
    	int P, n;
    	double B, D, sub, total;
    By the way, "P,n,B, and D" are not particularly meaningful names for variables.
    there's nothing wrong with declaring them on seperate lines like that... that's usually how I declare things (on seperate lines).
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    Thanks. I have one more question. Say you have a number like 1235.67...... How would you round that number off to the tens spot? I think it has something to do with the ceil(x) function... but that just rounds to the nearest whole number, doesn't it?

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    multiply by 10 then use either ceil() or floor() depending on what the number is... remember, ceil(x) returns the smallest integer not less than x, while floor(x) returns the biggest integer no more than x.

    basically, multiply by 10 and then check to see what's in the tenths spot. if it's less than 5, floor() the number and divide by 10, if it's more, ceil() the number and divide by 10.

    alternatively, instead of using floor(), you can just simply truncate the decimal by casting it to an integer - for example:
    Code:
    double a=3.141592654;
    a=(static_cast<int>(a*100)/100.0);
    for reference: http://www.cppreference.com/stdmath/
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    OR...

    Code:
    #include <iomanip>
    Then you can do this to round to the 10nths place
    Code:
    cout << setprecision(1);
    // then each time you print a number it will round to the 10nths place

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    don't forget that setprecision only works on output--it won't help if you need to do math with it.
    Last edited by major_small; 07-10-2005 at 08:26 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    *this
    Join Date
    Mar 2005
    Posts
    498
    Ya for output, but it does round...
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main () {
       cout << setiosflags (ios::fixed);
       cout << setprecision(1);
       cout << 100.55 << " " << 100.54 << endl;
       cin.get();
    }
    Try that.

    Plus you shouldnt round within a problem, so my advice is only use it on output.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I know... I caught that and edited it out :/
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    75
    I'm not trying to round to the tenths place... I'm trying to round to the TENS place. For example, with the number 1235.67.... I want to be able to round that to 1240.00 Suggestions?

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    do what I said before... except divide then multiply...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21