5 Replies - 569 Views - Last Post: 26 November 2014 - 12:31 PM Rate Topic: -----

#1 tarnold4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-November 14

Help with return value

Posted 24 November 2014 - 08:03 AM

I am very new to C/C++ and I have and assignment. My instructor said i am missing a return value with the code posted below. I cant seem to figure it out. Also any help writing a code with nandd,norr,nott,orr,xnorr,and xorr functions would be great.

#define preferred ‘l’
#define other ‘o’
#define invalid ‘x’

char andd( char operand1, char operand2)
{
     char result=invalid;
     if (operand1 == preferred) {
           if (operand2 == preferred) {
                 result = preferred;
           }
     }
     if (operand1 == preferred) {
           if (operand2 == other) {
                 result = other;
           }
     }
     if (operand1 == other) {
           if (operand2 == preferred) {
                 result = other;
           }
     }
     if (operand1 == other) {
           if (operand2 == other) {
                 result = other;
           }
     }

This post has been edited by jimblumberg: 24 November 2014 - 08:42 AM
Reason for edit:: Added missing code tags please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Help with return value

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 13,493
  • Joined: 08-August 08

Re: Help with return value

Posted 24 November 2014 - 08:29 AM

  • Please use code tags so that your code can retain its indenting and so that it is easy to copy/paste into a project. Help us help you!
  • What do you think the first char means here: char andd( char operand1, char operand2)

Was This Post Helpful? 0
  • +
  • -

#3 LtJars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-November 14

Re: Help with return value

Posted 24 November 2014 - 10:16 AM

Seeing how we don't have your main I am going to assume its safe to say your passing in 2 values into
char andd( char operand1, char operand2)
function. When ever you have a function call you need to return a value, unless the function is set to a void.

char result=invalid;
seems to be the value you are looking to return.

Here is an example:
#include <stdio.h>

int max ( int a, int b )
{
   return ( a > b ? a : b );
}

int main()
{
    int nOne = 5;
    int nTwo = 7;

    printf_s("\n%d is bigger\n", max( nOne, nTwo ));
}

Was This Post Helpful? 0
  • +
  • -

#4 tarnold4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-November 14

Re: Help with return value

Posted 26 November 2014 - 07:34 AM

View PostLtJars, on 24 November 2014 - 10:16 AM, said:

Seeing how we don't have your main I am going to assume its safe to say your passing in 2 values into
char andd( char operand1, char operand2)
function. When ever you have a function call you need to return a value, unless the function is set to a void.

char result=invalid;
seems to be the value you are looking to return.

Here is an example:
#include <stdio.h>

int max ( int a, int b )
{
   return ( a > b ? a : b );
}

int main()
{
    int nOne = 5;
    int nTwo = 7;

    printf_s("\n%d is bigger\n", max( nOne, nTwo ));
}

Was This Post Helpful? 0
  • +
  • -

#5 tarnold4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-November 14

Re: Help with return value

Posted 26 November 2014 - 07:50 AM

Thank you, after looking at the code again [code]char return=invalid; is what i was looking for.
This is one down... and these are what i have to do nandd,norr,nott,orr,xnorr,and xorr functions. With these i also have to write a code out for each of them. Would i start similar to the above one with these. My instructor gave us these functions originally in vbscript form. We have to take them and translate them to C/C++ code. The code above came from the code in this message.


function andd( operand1, operand2 )
dim result
result = invalid
if operand1 = preferred then
if operand2 = preferred then
result = preferred
end if
end if
if operand1 = preferred then
if operand2 = other then
result = other
end if
end if
if operand1 = other then
if operand2 = preferred then
result = other
end if
end if
if operand1 = other then
if operand2 = other then
result = other
end if
end if
andd = result
if trace then traceAdd( "andd" )
end function
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,870
  • Joined: 31-December 10

Re: Help with return value

Posted 26 November 2014 - 12:31 PM

From looking at your code, you need to read up on boolean algebra as it pertains to C. You could really shorten your function down a little because you're repeating checks that have already been made:
char andd( char operand1, char operand2)
{
     char result=invalid;
     if (operand1 == preferred) {
           if (operand2 == preferred) {
                 result = preferred;
           }
           else if(operand2 == other) {
                 result = other;
           }
     }
/*     if (operand1 == preferred) {
           if (operand2 == other) {
                 result = other;
           }
     }*/
     if (operand1 == other) {
           if (operand2 == preferred) {
                 result = other;
           }
           else if(operand2 == other) {
                 result = other;
           }
     }
/*     if (operand1 == other) {
           if (operand2 == other) {
                 result = other;
           }
     }*/
}


Do you see how I commented out those if-statements and put them inside the other if-statements as else ifs?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1