Menu

undeclared (first use this function)

2007-10-17
2012-09-26
  • 2bitmarksman

    2bitmarksman - 2007-10-17

    I get this error when I try to execute my program (taken from the Compiler log):

    F:\coins.cpp: In function void computerAndDisplay(int)': F:\coins.cpp:44: error:amountLeft' undeclared (first use this function)
    F:\coins.cpp:44: error: (Each undeclared identifier is reported only once for each function it appears in.)

    Execution terminated

    Here's my source code:

    include <iostream>

    using namespace std;

    void showInstructions();
    int getCents();
    void computeAndDisplay(int cents);
    void computeCoins(int denomination, int& number, int& amountLeft);
    bool again();

    int main() {
    int cents;

    showInstructions();
    do {
    cents = getCents();
    computeAndDisplay(cents); }
    while (again());

    system("pause");
    return 0;
    }

    void showInstructions() {
    cout << "This program is intended to take 1-99 cents and tell you\n"
    << "how many Half-dollars, quarters, dimes, nickles and pennies\n"
    << "you will have.\nType in the number of cents you have: ";
    }
    int getCents() {
    int cents;
    cout << "Please enter your number of cents: ";
    cin >> cents;
    return cents;
    }
    void computeCoins(int denomination, int& number, int& amountLeft) {
    number = amountLeft/denomination;
    amountLeft = amountLeft%denomination;
    }
    void computerAndDisplay(int cents) {
    cout << "The number of half-dollars you have is: ";
    cout << computeCoins(50, cents, amountLeft); // <-- Here's where the error comes up.
    cout << "\nThe number of quarters you then have is: ";
    cout << computeCoins(25, cents, amountLeft); // <-- will come up here too.
    cout << "\nThe number of dimes you have is: ";
    cout << computeCoins(10, cents, amountLeft); // <--
    cout << "\nThe number of nickles you have is: ";
    cout << computeCoins(5, cents, amountLeft); // <--
    cout << "\nThe number of pennies you have is: ";
    cout << computeCoins(1, cents, amountLeft); // <--
    }
    bool again() {
    char c;
    cout << "Would you like to repeat? (Y/N)";
    cin >> c;
    while (c == 'y'||c == 'Y');
    }

    I've spent 3 hours trying to figure out WTF is wrong, and my teachers an ass and won't help at all. Thanks for the support guys :|

     
    • Osito

      Osito - 2007-10-17

      In your computerAndDisplay, you can access cents because it was passed as an argument but you don't have amountLeft declared anywhere. The compiler log does a pretty good job of pointing you to the error. It says to look in void computerAndDisplay(int) and that amountLeft is undeclared. You really spent three hours looking at that? Scary.

      In all my years of programming, I can honestly say I've never seen an argument in a function written as "int& number". If that's an alternate way of passing a pointer, then your calculations in computeCoins won't be right.

       
      • Osito

        Osito - 2007-10-17

        I tried your program out with the int& number and it does work. I'm surprised I've never seen that format before. I guess you learn something new every day.

        Your while() loop in bool again() can be infinite. You should instead use an if statement and return a 1 or 0 based on the result.

         
        • Anonymous

          Anonymous - 2007-10-18

          You have never come across C++ reference variables or parameters "In all [your] years of programming"!? I guess you have largely restricted yourself to C then?

          http://www.cprogramming.com/tutorial/references.html

           
          • Osito

            Osito - 2007-10-18

            Yeah pretty much. When I went to school we learned Fortran. :) I taught myself C but I never found the need to learn C++. Most of the work-related programming I do is for embedded processors and the compilers are always C.

            Thanks for the link. Maybe it's time to learn a new language.

             
            • Anonymous

              Anonymous - 2007-10-18

              I've been doing embedded systems work for 18 years, and I use C++ (for the last 5) - Admittedly on ARM targets with sever Mb of RAM, but IAR support C++ on AVR as does the GNU GCC-AVR compiler. That's not to say I'd necessarily recommend it however - although maybe just for the stricter compilation C code compiled by a C++ compiler usually carries no penalty and many C++ features such as references also carry no penalty and are safer than pointers - the safety is applied at compile time so there is no runtime overhead. You should consider using C++ if only as a better C.

              I've even come across a C++ compiler for PIC but that's just daft! ;-)

               
              • Osito

                Osito - 2007-10-18

                LOL, C++ for a PIC? We use assembly for that. I can't imagine it was actually worth someone's time to come up with a C++ compiler for PIC.

                 
                • Anonymous

                  Anonymous - 2007-10-18

                  Well I always assumed that this http://www.picant.com/c2cpp/cpp.html was just experimental to see if it were possible, but it seems it was so successful it spawned a successor: http://www.sourceboost.com/Products/BoostCpp/Overview.html. If gets worse - there's Pascal too!

                   
    • kip2847

      kip2847 - 2007-10-18

      What the problem seems to me is your trying to pass a value that has limit scope. what you need to do is place the variable into a heap so that when the end life of thefunction is finished your variable still existe. you will need the 'new', and 'delete' operator for this.

      another way you can tackle this is by using gobal variables but the problem with gobal variables is you might not want to keep the variable for the life of the program.

       
      • Osito

        Osito - 2007-10-18

        Actually the problem isn't that the variable doesn't exist at the end of the function - it's just never declared in the first place. He doesn't need a global, just an int amountLeft; line in compute[r]AndDisplay.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.