Thread: C++ Program with Class

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    19

    C++ Program with Class

    I'm trying to run a C++ program with a class, but when I try to compile it, I get a message that says that no such file or directory exists. Here's the code I have so far:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "savingsAccount.h"
    #include <conio.h>
    
    
    using namespace std;
    
    
    
    
    int main()
    {
        SavingsAccount saver1(2000.00);
        SavingsAccount saver2(3000.00);
    
    
        SavingsAccount::modifyIntererestRate(0.03);                                     // sets the annualInterestRate to 3%
    
    
        cout << "First month with interest balance:\n\n";                               // calculates the monthly interest and print the new balances 
        saver1.calculateMonthlyInterest();
        cout << "Saver 1 Savings Balance: $" << saver1.GetBalance() << "\n";
        
        saver2.calculateMonthlyInterest();
        cout << "Saver 2 Savings Balance: $" << saver2.GetBalance() << "\n";
    
    
        cout << endl;
    
    
        SavingsAccount::modifyIntererestRate(0.04);                                     // sets the annualInterestRate to 4%
    
    
        cout<< "Second month with interest balance:\n\n";                               // and calculate the next month's interest and print the new balances
        saver1.calculateMonthlyInterest();
        cout << "Saver 1 Savings Balance: $" << saver1.GetBalance() << "\n";
        
        saver2.calculateMonthlyInterest();
        cout << "Saver 2 Savings Balance: $" << saver2.GetBalance() << "\n";
    
    
        cout << endl;
        getch();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Could it be the conio.h? Try commenting out that include and also the getch() call at the end.
    Or it could be savingsAccount.h.
    Or depending on exactly where you are seeing this error, it could be that you are trying to compile your file from the wrong directory.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    For some IDEs, if you have the wrong file extension as in ".c" instead of ".cpp" it tries to compile the code as C code instead of C++ code.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    The issue is with savingsAccount.h. I have the class source file in the same directory though, so I'm not sure why it isn't compiling.

  5. #5
    Guest
    Guest
    What are you using to compile?

    btw, you can use:
    Code:
    std::cin.get();
    instead of getch() and thus get rid of the non-standard conio.h.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by pianoman\ View Post
    The issue is with savingsAccount.h. I have the class source file in the same directory though, so I'm not sure why it isn't compiling.
    If you're using an IDE then you may need to add the .h file to the project. Try right-clicking on the file list of the project and see if it lets you add a file from there.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    Quote Originally Posted by john.c View Post
    If you're using an IDE then you may need to add the .h file to the project. Try right-clicking on the file list of the project and see if it lets you add a file from there.
    I made sure the .h file was in the project file, and the first error I was getting went away. Now I'm getting another compiler error though: "ld returned 1 exit status"

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by pianoman\ View Post
    I made sure the .h file was in the project file, and the first error I was getting went away. Now I'm getting another compiler error though: "ld returned 1 exit status"
    So you have the code file and the header file listed in the project but it won't link. I'm not sure.
    I would still get rid of conio.h and do what Adrian said above. The main difference is that you have to hit enter instead of any key.

    What IDE are you using?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    Quote Originally Posted by john.c View Post
    So you have the code file and the header file listed in the project but it won't link. I'm not sure.
    I would still get rid of conio.h and do what Adrian said above. The main difference is that you have to hit enter instead of any key.

    What IDE are you using?
    I got rid of the conio.h. I'm using Dev-C++.

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Do you have a separate code file for the savings account code? That needs to be added to the project as well.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  11. #11
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    Quote Originally Posted by john.c View Post
    Do you have a separate code file for the savings account code? That needs to be added to the project as well.
    Yes, the separate code file for the savingsAccount class is in the project.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with a program for my class!
    By seandugan890 in forum C Programming
    Replies: 4
    Last Post: 11-12-2009, 02:42 AM
  2. Help with a program for class...
    By currysauce in forum C Programming
    Replies: 1
    Last Post: 08-07-2006, 07:40 AM
  3. help with my program for class, PLEASE!!!
    By acegillss in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2003, 07:37 PM
  4. Need help with program for class
    By zakelua in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2003, 03:39 PM
  5. Gas/Oil program for class
    By RoD in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2002, 05:35 PM

Tags for this Thread