Filling the class from values taken from user input


 
Thread Tools Search this Thread
Top Forums Programming Filling the class from values taken from user input
# 1  
Old 05-09-2012
Filling the class from values taken from user input

I have a program that accepts user input. For example I have mdacc that the user sets the value. I then have a class which stores the value set by the user. I use set_param to set the values in the class. I pass through it the list of user defines arguments from argv. What would be the opinion on this way of programming? I was thinking of getting all arguments in the main program rather than as below

So my question is: How can a use classes and user input in the main program?

Code:
int  main(
  int  argc,
  char*  argv[] ) {

  if (argc < 2) {                                    // Print documentation
      FILE*  pipe;
      const char**  ptr = DOC;
      pipe = popen("more", "w");
      while (*ptr) { (void) fprintf(pipe, "%s\n", *ptr++); }
      pclose(pipe);
      exit(1);
  }

  String  S;
  Parsing  Pc(argc, argv);


// -- Set Output Verbosity Level -------------------------------------------------------------------
  Verbose  Vrb;
  if (Pc.GetString("VRB", S)) {
      S.ToUpper();
      if ( !GetVerbose(S, Vrb) ) { error("Bad VRB parameter"); }
  } else {
      Vrb = none;
  }

  bool  Bayes = true;
  if ( Pc.GetString( "INVTYPE", S ) ) {
    S.ToUpper();
    if ( S == "Std" ) {
        Bayes = false;
    } else if ( S == "Bayes" ) {
        Bayes = true;
    } else  {
        error ( "Bad INVTYPE parameter" );
    }
    if ( Vrb >= medium ) { cerr << "Tdarwin.cc, InvType = " << S << endl; }
  }

  ModMisfit  M;
  M.SetBayes(Bayes);
  M.SetParam(Pc);


...

Code:
#ifndef __MODMISFIT_HH__
#define __MODMISFIT_HH__
#include "source.hh"
#include "velmod.hh"
#include "objfunc.hh"
#include "parsing.hh"
#include "verbose.hh"
#include "common.hh"

class ModMisfit {

protected:
  static const Real    dtmin_default;
  static const Real    sigma_default;
  static const Real    beta_default;
  static const double  dang_default;
  static const bool    search_headwaves_default;

  int     nphases;
  int     nsources;
  double  dangsh;
  int     itermax;
  Real    mdacc;
  Real    mindist;
  Real    dtmin;
  Real    sigma;
  Real    beta;
  bool    search_headwaves;
  bool    extrap;
  Source**     sources;
  List<int>    ds;
  List<int>    dp;
  List<Real>   x;
  List<Real>   t1;
  List<Real>   t2;
  List<Phase>  ph;
  Verbose      vblevel;

public:

 ModMisfit():                         //
    mindist( -1 ),
    mdacc( -1 ),
    itermax( -1 ),
    dangsh( dang_default ),
    search_headwaves( search_headwaves_default ),
    extrap( true ),
    dtmin( dtmin_default ),
    sigma( sigma_default ),
    beta( beta_default ),
    vblevel( none ) { }

   void  set_param(Parsing&  P );
};

void ModMisfit::set_param (
  Parsing&  P ) {

  String  s;
  if ( P.GetString ( "vb", s ) ) {
      if ( !GetVerbose ( s, vblevel ) ) { error( "Bad vb parameter" ); }
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, --vb = " << s << endl;
  }

  Real  dtau;
  if ( P.GetReal ( "dtau", dtau ) ) {
      set_dtau ( dtau );
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, dtau = " << dtau << endl;
  }

  Integration  intg;
  if ( P.GetString ( "intg", s ) ) {
      if ( !GetIntegration ( s, intg ) ) { error("Bad int paramater"); }
      SetIntegration ( intg );
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, intg = " << s << endl;
  }

  if ( P.GetDouble ( "dang", dang ) ) {
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, dang = " << dang << endl;
  }

  if ( P.GetString ( "headwaves", s ) ) {
      if ( S == "on" ) {
          set_search_headwaves( true );
      } else if ( s == "off" ) {
          set_search_headwaves( false );
      } else  {
          error( "Bad headwaves parameter" );
      }
      if ( vblevel >= medium ) { cerr << "ModMisfit::SetParam, HW = " << S << endl; }
  }

  if ( P.GetString ( "mdacc", s ) ) {
      if ( s == String( "always" ) ) {
          mdacc = -1;
      } else {
          s >> mdacc;
      }
  }

  if ( P.GetReal ( "mindist", mindist ) ) {
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, mindist = " << mindist << endl;
  }

  if ( P.GetInt ( "itermax", itermax ) ) {
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, itermax = " << itermax << endl;
  }

  if ( P.GetString ( "extrap", s ) ) {
      if ( s == String ( "on" ) ) {
          Extrap = true;
      } else if ( s == String ( "off" ) ) {
          Extrap = false;
      } else {
          error( "extrap not 'on' or 'off'" );
      }
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, Extrap = " << s << endl;
  }

  if ( P.GetReal ( "mindt", mindt ) ) {
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, mindt = " << mindt << endl;
  }

  if ( P.GetReal ( "sigma", sigma ) ) {
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, Sigma0 = " << Sigma0 << endl;
  }

  if ( P.GetReal ( "beta", beta ) ) {
      if ( vblevel >= medium ) cerr << "ModMisfit::SetParam, Beta = " << beta << endl;
  }
  cerr << endl;
}


Last edited by kristinu; 05-09-2012 at 01:55 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Printing values from a class

I have a class and want to print values in MOD using L = new Layer* ; How can I print the values in MOD using this object L??? class Layer { public : Model* MODP; Model* MODS; (1 Reply)
Discussion started by: kristinu
1 Replies

2. Shell Programming and Scripting

Capturing multiple values from user input

Hello, I need to capture multiple values from user input and do not know how to do it. I'm writing a script to delete old files, but want to give the option to keep some by asking the user. This is what my output looks like... Old files to be deleted... 1 file1 2 file2 Then this bit of... (3 Replies)
Discussion started by: jrymer
3 Replies

3. UNIX for Dummies Questions & Answers

Merging two text files by a column and filling in the missing values

Hi, I have to text files that I want to merge by the first column. The values in the first column pretty much match for the first part. However there are some values that are present in column 1 and not present in column 2 or vice versa. For such values I would like to substitute X for the... (9 Replies)
Discussion started by: evelibertine
9 Replies

4. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

5. Programming

Help with mapping two tables and filling the null values

Hi All , I have two tables. I will provide sample data in the tables below. table1: dept_id dept_name rep_id admin_lastname 10 dept of int.medicine Paul 10 dept of int.medicine Frank 20 dept of chemistry Young 20 dept of chemistry Bill 30 school of denistry kaufmann 40... (3 Replies)
Discussion started by: megha2525
3 Replies

6. UNIX for Dummies Questions & Answers

Unix user login class

Hello - Could anyone please explain what is login class in unix..? is it supported by Linux, AIX, HP-UX, Solaris? Also how do we update this when a user is created? I looked into man pages for useradd/usermod and mkuser, but could not find any option to add/update login class for a user. ... (5 Replies)
Discussion started by: manju--
5 Replies

7. Shell Programming and Scripting

Displaying default values when accepting input from user

Is there a way to display the default answer when accepting input from the user in the unix script.. e.g. ans="n" read $ans?"Enter y to continue n to exit:" altough ans contains n the message doesn't display the current contents on ans .. you get Enter y to continue n to exit: (8 Replies)
Discussion started by: flopster
8 Replies

8. Programming

STL Iterator and user-defined class

Is is possible to make STL-Iterator to work with user defined class ,like the one below? #include <iostream> #include <stdexcept> using namespace std; template <class T> class Array { public: T& operator (unsigned i) throw(out_of_range) { return data_; } protected: ... (2 Replies)
Discussion started by: johnbach
2 Replies

9. Solaris

wrong ELF class: ELFCLASS32 when trying to run ps as particular user

I get the following error when running /usr/bin/ps on Solaris 10 as a particular non-privileged user: ld.so.1: ps: fatal: /usr/dt/lib/libXm.so.3: wrong ELF class: ELFCLASS32 Killed However I can run /usr/bin/ps successfuly as root or as any other non-privileged user. What could it be about this... (5 Replies)
Discussion started by: aussieos
5 Replies

10. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies
Login or Register to Ask a Question