Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

  1. #11
    Join Date
    Apr 2008
    Location
    California Republic
    Beans
    2,657

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    Question about porting code from Visual Studio to Geany.

    We're doing functions now.

    I can make it all be one big file, but is there a way to have it as three separate files that reference each other?

    He wants to have it in three separate files: header file that describes functions, file that defines them, and main() file for reasons X, Y, and Z that I've no choice but to accept and that thus aren't particularly relevant.

    Snip from today's introductory example (ignore the 'math' in the filenames, mentally replace it with an appropriate name - I also haven't done data validation yet, so ignore that too):

    math_H.h

    Code:
    #include <iostream>
    using namespace std;
    
    #ifndef randomManip
    #define randomManip
    
    double employeeGrossPay (int hours, double wage);
    //Preconditions: both are non-negative numbers, hours is an intiger and wage is a decimal value.
    //Postconditions: Returns the gross pay of one employee, giving time and a half for hours past 40.
    
    double totalGrossPay (double currentTotal, double latestEmployee);
    //Preconditions: both are non-negative numbers, currentTotal is the running tally and latestEmployee
    // is to be added to that.
    //Postconditions: Running tally of total gross pay is updated by summing the two numbers.
    
    #endif
    math_Imp.cpp

    Code:
    #include <iostream>
    #include "math_H.h"
    using namespace std;
    
    #ifndef randomManipImp
    #define randomManipImp
    
    double employeeGrossPay (int hours, double wage)
    {
    	int hoursOver40 = 0;
    	double grossPay = 0.0;
    	if (hours > 40) // separate normal time from overtime hours
    	{
    		hoursOver40 += hours - 40;
    		hours = 40;
    	}
    	grossPay = (hours * wage) + (hoursOver40 * 1.5 * wage);
    	return grossPay;
    }
    
    double totalGrossPay (double currentTotal, double latestEmployee)
    {
    	return currentTotal + latestEmployee;
    }
    math_test.cpp

    Code:
    #include <iostream>
    #include <ctime>
    #include <iomanip>
    #include "math_H.h"
    using namespace std;
    
    int main()
    {
    	int hours = 0;
    	double wage = 0.0;
    	char more = 'y';
    	int counter = 0;
    	double allEmployeesTotal = 0;
    
    	do // main loop for entering employee info
    	{
    		counter++;
    		cout << "Enter hours for employee #" << counter << ":\t";
    		cin >> hours;
    		cout << "Enter wage for employee:\t$";
    		cin >> wage;
    		cout << "Gross Pay for that employee:\t$" << setprecision(2) << fixed 
    			<< employeeGrossPay(hours, wage) << endl;
    
    		//update running total for total pay for all employees.
    		allEmployeesTotal = totalGrossPay(allEmployeesTotal, employeeGrossPay(hours, wage)); 
    		cout << "More employees? (y/n)\t";
    		cin >> more;
    		more = tolower(more);
    	} while (more != 'n');
    
    	cout << "Total gross pay for all " << counter << " employees:\t$" << setprecision(2) << fixed 
    		<< allEmployeesTotal << "." << endl;
    	return 0;
    }
    Last edited by earthpigg; March 13th, 2012 at 08:17 AM.
    Semper Fi

    My Non-Ubuntu Blog.
    All posts by me are Public Domain.

  2. #12
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    What are you asking? Are you saying these 3 files moved to geany doesn't compile anymore?

  3. #13
    Join Date
    Apr 2008
    Location
    California Republic
    Beans
    2,657

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    If I put the three files in the same directory and then open them at once in geany in three different tabs, it doesn't seem to automatically recognize that they're related. Replacing #include "math_H.h" with #include "/path/to/math_H.h" doesn't seem to do it either.

    If I amalgamate them into one .cpp file, it works fine. But, trying to go along with the way we're being taught in class I'd like them in three.

    EDIT: In Visual Studio, we are taught to start off the lab time by creating a new "project", and through some wizardry any files just created or added are thus automatically associated and through some other magic I do not understand built/compiled together. I see a "project" menu in geany, but the paradigm stops there and I don't seem to see a "add file to project" dialog anywhere.

    Trying to stomp the vendor lock-in bugs as they pop up.
    Last edited by earthpigg; March 14th, 2012 at 12:31 AM.
    Semper Fi

    My Non-Ubuntu Blog.
    All posts by me are Public Domain.

  4. #14
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    I've not used geany i'm afraid (visual studio all the way for me), but yea there should be a project file somewhere.
    maybe create a blank project in geany with a few cpp and h files, then try and locate the project file created and use this as a basis for creating your own project file.

    In fact, even better. Create a new project with files called the same as yours and just copy your code into each of the files.

  5. #15
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    I think I'd go the route of getting the program to compile from the command line. Geany is a lightweight tool rather than a complete IDE and you'll probably need a makefile or at least modify Geany's build command for the project. Geany's project file is just a collection of open files and overall project preferences.

    Something like

    Code:
    g++ -o mathimp math_Imp.cpp math_test.cpp

  6. #16
    Join Date
    Apr 2008
    Location
    California Republic
    Beans
    2,657

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    OK thanks for the advice. I got it working the way we were taught in codeblocks (see screenshot).

    Would still be interested if someone is aware of a way to make geany do that.
    Attached Images Attached Images
    Semper Fi

    My Non-Ubuntu Blog.
    All posts by me are Public Domain.

  7. #17
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    Quote Originally Posted by earthpigg View Post
    OK thanks for the advice. I got it working the way we were taught in codeblocks (see screenshot).

    Would still be interested if someone is aware of a way to make geany do that.
    My $0.02... focus more on the quality of your code and not so much on the IDE.


    Here's some issues I saw in your code:

    1. Unnecessary header files included in the .h file

    2. Unwarranted "using namespace" directive in .h file; NEVER do this.

    3. Unnecessary header files and/or "using namespace" directive included in .cpp file(s).

    4. System header files should not be included before project header files.

    5. Redundant code.

  8. #18
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    Hi

    employeeGrossPay (int hours,
    Preconditions: both are non-negative numbers, hours is an integer
    If the above is so, why is hours a signed integer ?

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  9. #19
    Join Date
    Apr 2008
    Location
    California Republic
    Beans
    2,657

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    A lot of that is

    for reasons X, Y, and Z that I've no choice but to accept and that thus aren't particularly relevant.
    And some because he isn't being nit-picky, and why should I be? Once we're done with the lab assignment, we're done and can leave. The above is good enough for 30/30 on the lab assignment. On my own time out of school, I've at this point got a nifty little pong game up and running using SDL and OpenGL.
    Semper Fi

    My Non-Ubuntu Blog.
    All posts by me are Public Domain.

  10. #20
    Join Date
    Apr 2008
    Location
    California Republic
    Beans
    2,657

    Re: Taking C++ w/ MS Visual Studio, don't want to be damaged goods...

    The only reason I "worry" about the IDE is so I don't have to do the homework assignments in class and can do them at home. It's cool though, codeblocks solved it for me.
    Semper Fi

    My Non-Ubuntu Blog.
    All posts by me are Public Domain.

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •