2

I want to check if the caller has passed a value for outOfand if not then I want to use a default value. I did some research online and saw that you can use argc to see how many variables are passed. I tried using that in the below code but I got the following error

Use of undeclared identifier 'argc'

How can I fix the below code?

 #include "Mark.h"
 #include <iostream>


using namespace std;


void Mark::set(int displayMode){
    m_displayMode= displayMode;
}

void Mark:: set(double mark, int outOf){
    m_mark= mark;
    if ( int argc < 2){
        m_outOf=1;
    }else{
        m_outOf=outOf;
    }


}
void Mark:: Mark ::setEmpty(){
    m_displayMode= DSP_UNDEFINED;
    m_mark=-1;
    m_outOf = 100;

}

bool Mark:: isEmpty()const{
    bool result= true;
    if((m_displayMode==DSP_UNDEFINED) && (m_mark==-1) && (m_outOf==100)){
        result =true;
    }else{
         result = false;
    }
    return result;
}

main function

m2.setEmpty();
    cout << "Setting m2 to the raw value of m1..." << endl;
    m2.set(m1.rawValue());
    cout << "done!" << endl;
    cout << "m2: The mark is: ";
    m2.set(DSP_ASIS);
    m2.display() << endl;
    cout << "m2: With the raw value of: ";
    m2.set(DSP_RAW);
    m2.display() << endl;
    cout << "m2: And percentage value of: ";
    m2.set(DSP_PERCENT);
    m2.display() << endl;

2 Answers2

2

What you found is your research is that argc is used in main to determine how many command line arguments were passed to the program when it was called. This is not what you need.

Since set takes two parameters, the caller will always have to specify outOf. This means you never have to check if it was supplied. If you want to be able to call set with or without providing outOf then what you need is a default argument for outOf that the code will use if the caller does not provide one. That would change your set to

void Mark::set(double mark, int outOf = 1){
    m_mark= mark;
    m_outOf=outOf;
}

And now m_outOf will get set to 1 if you call set like foo.set(1) and will get set to the value of N if you call set like foo.set(1, N);

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1

There are two valid declarations for main:

int main()

and

int main(int argc, char** argv)

If you want to look at command-line arguments you use the second one.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165