I am trying to test this simple function, but Opt.status, Opt.Year values are not returned back to main(). Why? Please help as I am new to C++.I am using visual c++ to execute these codes.This is in my .cpp file
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "stdio.h"
using namespace std;
int main()
{
Easy_Task obj_EasyTask;
TimeDateMonthOptions whatOptions=DATE;
TOptions Opt;
Opt.status=FALSE;
Opt.Year=1970;
printf("Enter code\n");
scanf("%d",&obj_EasyTask.code);
cout << "the code entered is: " << obj_EasyTask.code;
obj_EasyTask.display2(obj_EasyTask.code);
cout << "\nOutput: " << obj_EasyTask.show();
printf("\nEnter the options that you prefer\n");
scanf("%d",&whatOptions);
obj_EasyTask.display3(whatOptions, Opt);
cout << "\nOpt.Year: " << Opt.Year;
if(Opt.status)
{
obj_EasyTask.x=(Opt.Year)& 0x00FF;
obj_EasyTask.y=((Opt.Year)& 0xFF00)>>8;
cout << "\nX: " << obj_EasyTask.x;
cout << "\nY: " << obj_EasyTask.y;
obj_EasyTask.Result=(obj_EasyTask.x)*(obj_EasyTask.y);
}
char holdWindow;
std::cin >> holdWindow;
return 0;
}
uint16_t Easy_Task::display2(uint16_t code)
{
if(code==1)
{
c = 7;
}
else
{
c = 9;
}
return c;
}
uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions Opt)
{
switch(whtOptions)
{
case 0:
case 1:
case 2:
case 3:
Opt.status=TRUE;
cout << "\nStatus1: " << Opt.status;
Opt.Year=1991;
cout << "\nYear1: " << Opt.Year;
break;
case 7:
Opt.status=FALSE;
cout << "\nStatus2: " << Opt.status;
Opt.Year=2013;
cout << "\nYear2: " << Opt.Year;
break;
default:
Opt.status=FALSE;
cout << "\nStatus3: " << Opt.status;
Opt.Year=2010;
cout << "\nYear3: " << Opt.Year;
break;
}
return Opt.status, Opt.Year;
}
In my .h file I have the class defined as follows:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
typedef unsigned short uint16_t;
#define TRUE 1;
#define FALSE 0;
typedef struct TOptions
{
bool status;
uint16_t Year;
};
typedef enum
{
YEAR,
MONTH,
DATE,
HOURS,
MINUTES,
SECONDS,
HUNDRETHS,
UNDEFINED
}TimeDateMonthOptions;
class Easy_Task
{
public:
uint16_t code, c, x,y, Result;
uint16_t display2(uint16_t code);
uint16_t show()
{
return c;
};
uint16_t display3(TimeDateMonthOptions whatOptions, TOptions Opt);
};
The problem I have is line:
if(Opt.status)
Where it doesn’t return the value of 1 but instead in takes the default value which was defined earlier. Why is this happening?
Better still define a function to return TOptions
TOptions Easy_Task::display3(...);and return a structure.Remember that you can only ever return ONE SINGLE return value from the function.