Help with Files and Structures

This is a discussion on Help with Files and Structures within the C++ Programming forums, part of the General Programming Boards category; Hey I'm having a really hard time with an assignment that's due Tuesday at midnight. If anyone out there is ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    16

    Help with Files and Structures

    Hey I'm having a really hard time with an assignment that's due Tuesday at midnight. If anyone out there is willing to help me I would greatly appreciated it!

    Please help!!!!

    Instructions: http://itech.fgcu.edu/faculty/zalews...sign3-2011.pdf

    And the code I was able to produce before I got completely lost

    Main file named assign3

    Code:
    #include <iostream>
    #include <cmath>
    #include <fstream>
    #include <cstdlib>
    #include "funcs3.cpp"
    using namespace std;
    
    // A calculator that solves multiple quadratic equations.
    
    void readCoeffs(double& coeffs);
    struct calcRoots(double coeffs[], double roots[]);
    void displayvalues(double coeffs[], double roots[]);
    void toFile(double coeffs[]. double roots[], ofstream outp);
    
    main(int Nc, char* Nv[]) {
    
       ofstream outp;   // declaring an output file stream
       ifstream inp;   // input file stream
    
       double coeffs[3], roots[2];
    
       if (Nc !=2) {
    
          for (int count = 0; count < Nc; count++) {
             void readCoeffs(double coeffs[]);
             struct calcRoots(double coeffs[], double roots[]);
             void displayValues(double coeffs[], double roots[]);
             void toFile(double coeffs[], double roots[], ofstream outp);
          }
       }
    }




    File containing my functions:

    Code:
    // Functions for a quadratic equation calculator
    
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    
    void readCoeffs(double coeffs[]) {
    
       cout << "Enter coefficient a: " << endl;
       cin >> coeffs[0];
       cout << "Enter coefficient b: " << endl;
       cin >> coeffs[1];
       cout << "Enter coefficient c: " << endl;
       cin >> coeffs[2];
       cout << endl;
    }
    
    struct calcRoots(double coeffs[], double  roots[]) {
       double  root1 = ((-coeffs[1]+sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])))/(2*coeffs[0]));
       double root2 = ((-coeffs[1]-sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])))/(2*coeffs[0]));
       roots[0] = root1;
       roots[1] = root2;
    
       double dscr();
    };
    
    double dscr(double coeffs[]) {
       double dscrResult = ((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2]));
       return dscrResult;
    }
    
    void displayValues(double coeffs[], double roots[]) {
       if (sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])) >=  0) {
          cout << "The solutions of a quadratic equation with coefficients:  " << endl;
          cout << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
          cout << "are as follows " << endl;
          cout << "Root 1: " << roots[0] << endl;
          cout << "Root 2: " << roots[1] << endl;
          cout << endl;
       }
       else {
          cout << "The solution of a quadratic equation with coefficients: " << endl;
          cout << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
          cout << "does not exist in the real domain. " << endl;
       }
    }
    
    void toFile(double a, double b, double c, double root1, double root2, ofstream outp) {
       outp.open("results.dat", ios::app);
       outp << "a = " << a << " b = " << b << " c = " << c << endl;
       outp << "Root1 = " << root1 << " Root2 = " << root2 << endl;
       outp << endl;
       outp.close();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    > void readCoeffs(double& coeffs);
    First, this needs to match the implementation in the other file.


    Then do something like this
    Code:
          for (int count = 0; count < Nc; count++) {
             readCoeffs(coeffs);
    //         struct calcRoots(double coeffs[], double roots[]);
    //         void displayValues(double coeffs[], double roots[]);
    //         void toFile(double coeffs[], double roots[], ofstream outp);
          }
    Compile and RUN this.
    It should just prompt you for 3 coefficients, for whatever number of times you require.

    When you're happy with this, then uncomment the next line, fix the function call (see what I did), and then retest.

    And so on, until you're done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Thanks for the help!
    Is the structure fine? I doesn't seem to me like I did it completely right. Any advice on that part?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    > #include "funcs3.cpp"
    Well this should be
    #include "funcs3.h"

    The 4 function prototypes which follow should be in funcs3.h

    And funcs3.cpp should also have
    #include "funcs3.h"

    In your IDE project settings, make sure funcs3.cpp is also listed as a source file, otherwise you end up with a handful of "unresolved symbol" errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    I'm sorry but what's IDE? I'm using putty to program on bash on linux.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    Oh, in which case, it's

    g++ prog.cpp funcs3.cpp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    So I have to include the header file inside my compilation statement?

    What I've used in the past for a single file is g++ -o prog prog.cpp
    and can run it then by say ./prog


    edit: I believe I am compiling it correct with the 2 files, but I'd like clarification if possible.....Also! I am getting the first part to run because of your marvelous advice When I try to compile with the structure added though it's coming up with errors, leading me to believe that I am way off with how to make my calcRoots function into a structure.
    Last edited by PenguinTux; 03-22-2011 at 03:32 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    You would need to post your latest code and error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Ok here it is.

    Main Program (assign3.cpp):
    Code:
    #include <iostream>
    #include <cmath>
    #include <fstream>
    #include <cstdlib>
    #include "funcs3.h"
    using namespace std;
    
    // A calculator that solves multiple quadratic equations.
    
    void readCoeffs(double coeffs[]);
    struct calcRoots(double coeffs[], double roots[]);
    void displayvalues(double coeffs[], double roots[]);
    void toFile(double coeffs[], double roots[], ofstream outp);
    
    main(int Nc, char* Nv[]) {
    
       ofstream outp;   // declaring an output file stream
       ifstream inp;   // input file stream
    
       double coeffs[3], roots[2];
    
     // if (Nc != 2) {
    
          for (int count = 0; count <=  Nc; count++) {
             readCoeffs(coeffs);
             struct calcRoots(coeffs, roots);
             void displayValues(coeffs, roots);
             void toFile(coeffs, roots, ofstream outp);
          }
    }
    funcs3.cpp :
    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    using namespace std;
    
    void readCoeffs(double coeffs[]);
    struct calcRoots(double coeffs[], double roots[]);
    void displayvalues(double coeffs[], double roots[]);
    void toFile(double coeffs[], double roots[], ofstream outp);
    
    
    void readCoeffs(double coeffs[]) {
    
       cout << "Enter coefficient a: " << endl;
       cin >> coeffs[0];
       cout << "Enter coefficient b: " << endl;
       cin >> coeffs[1];
       cout << "Enter coefficient c: " << endl;
       cin >> coeffs[2];
       cout << endl;
    }
    
    struct calcRoots(double coeffs[], double  roots[]) {
       double  root1 = ((-coeffs[1]+sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])))/(2*coeffs[0]));
       double root2 = ((-coeffs[1]-sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])))/(2*coeffs[0]));
       roots[0] = root1;
       roots[1] = root2;
    
       double dscr();
    }
    
    double dscr(double coeffs[]) {
       double dscrResult = ((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2]));
       return dscrResult;
    }
    
    void displayValues(double coeffs[], double roots[]) {
       if (sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])) >=  0) {
          cout << "The solutions of a quadratic equation with coefficients:  " << endl;
          cout << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
          cout << "are as follows " << endl;
          cout << "Root 1: " << roots[0] << endl;
          cout << "Root 2: " << roots[1] << endl;
          cout << endl;
       }
       else {
          cout << "The solution of a quadratic equation with coefficients: " << endl;
          cout << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
          cout << "does not exist in the real domain. " << endl;
          cout << endl;
       }
    }
    
    
    void toFile(double a, double b, double c, double root1, double root2, ofstream outp) {
       outp.open("results.dat", ios::app);
       outp << "a = " << a << " b = " << b << " c = " << c << endl;
       outp << "Root1 = " << root1 << " Root2 = " << root2 << endl;
       outp << endl;
       outp.close();
    }
    and the error messages while compiling :
    In file included from assign3.cpp:7:
    funcs3.h:11: error: expected unqualified-id before ‚double‚
    funcs3.h:11: error: expected `)' before ‚double‚
    funcs3.h:27: error: expected unqualified-id before ‚double‚
    funcs3.h:27: error: expected `)' before ‚double‚
    assign3.cpp:13: error: expected unqualified-id before ‚double‚
    assign3.cpp:13: error: expected `)' before ‚double‚
    assign3.cpp: In function ‚int main(int, char**)‚:
    assign3.cpp:28: error: expected primary-expression before ‚struct‚
    assign3.cpp:28: error: expected `;' before ‚struct‚
    assign3.cpp:29: error: variable or field ‚displayValues‚ declared void
    assign3.cpp:30: error: variable or field ‚toFile‚ declared void
    assign3.cpp:30: error: expected primary-expression before ‚outp‚
    funcs3.h:11: error: expected unqualified-id before ‚double‚
    funcs3.h:11: error: expected `)' before ‚double‚
    funcs3.h:27: error: expected unqualified-id before ‚double‚
    funcs3.h:27: error: expected `)' before ‚double‚
    funcs3.h: In function ‚void toFile(double, double, double, double, double, std:fstream)‚:
    funcs3.h:58: error: ‚outp‚ has incomplete type
    /usr/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/iosfwd:89: error: declaration of ‚struct std:fstream‚

    The smilies are a : and O right next to each other.
    Last edited by PenguinTux; 03-22-2011 at 03:47 PM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    So what do you want in your struct?

    A struct is something like
    Code:
    struct point {
       int x;
       int y;
    };
    A 'struct', by itself is meaningless.

    Also,
    - you didn't post your header file.
    - you didn't include your header file in funcs3.cpp
    - you didn't delete the prototypes from main.cpp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    I'm sorry funcs3.cpp has the same contents as funcs3.h and im using the latter. Sorry for the confusion there. I take it im suppose to use both files then and not just the header from the way your worded it? Or is it ok to just modify the headerfile and main program file and leave funcs3.cpp out completely?

    The instructions for whats needed in the structure are as follows:
    Second, having the coefficients stored within the program in previous step, a function
    to calculate the roots shall be called, taking coefficients in an array as parameters, and
    passing to the caller the calculated roots via its name as a structure (not as global
    variables or via an array in the parameters list). Name this function calcRoots().

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    Well a struct would be something like
    Code:
    struct foo {
      double coeffs[3];
      double roots[2];
    };
    A function call might be
    Code:
    // prototype
    void func ( foo &params );
    // main
    foo params;
    func(params);
    The function itself would be
    Code:
    void func ( foo &params ) {
      if ( params.coeffs[0] > 0 ) /// and so on
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and header files problem
    By Quinn90 in forum Windows Programming
    Replies: 1
    Last Post: 01-27-2011, 09:35 AM
  2. Structures and Files
    By nuddy in forum C Programming
    Replies: 2
    Last Post: 03-31-2010, 01:15 PM
  3. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 08:53 AM
  4. Random access files and structures
    By DLR in forum C Programming
    Replies: 8
    Last Post: 04-21-2006, 04:24 PM
  5. structures and files
    By eth0 in forum C++ Programming
    Replies: 9
    Last Post: 01-06-2004, 06:48 PM

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