6 Replies - 2094 Views - Last Post: 06 October 2009 - 04:13 PM Rate Topic: -----

#1 vv29   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-September 09

Need Help With Return Multiple Value in Function!

Posted 06 October 2009 - 02:35 PM

Hi,
I need help with this code. I write a program that requires user to enter a string, after that determine it is a valid integer or not. If not, return the character that is not a valid integer (the first character that program finds). If it is a valid integer, print "Good job!". For example, if user enters "12j34q", the program will print "j is an invalid". If user enter "123456", the program will print "Good job". However, I still stuck on the way to show the invalid character. In the function isInt, I want to return 2 value ( b and i) so in function main, I could print in[i] to show the invalid character. I have googled and they use "call by reference" to return multiple values but I couldn't apply it. In my current fuction right now, it could only return b (0 or 1 to determine the string is a valid integer or not). Could anyone help me to solve this? Thank you very much!

#include <stdio.h>
#include <string.h>
void getInput(char in[]);
int isInt(char in[], int length);
int main(void)
{
	char in[20];
	int length,b;
	getInput(in);
	printf("You entered: %s\n", in);
	length = strlen(in);
	b = isInt(in, length);
	if (b==0)
	{
	printf("%s is not an integer!\n", in);
	}
	else
	{
	printf("Good job!You have entered a number.\n");
	}
	return 0;
}

void getInput(char in[])
{
	printf("Please enter your number: ");
	gets(in);
	return;
}

int isInt(char in[], int length)
{
	int i;
	for (i=0; i<length; i=i+1)
	{
		if(in[i] < '0' || in[i] > '9')
		return 0;
	}	
	return 1;
}	



Is This A Good Question/Topic? 0
  • +

Replies To: Need Help With Return Multiple Value in Function!

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need Help With Return Multiple Value in Function!

Posted 06 October 2009 - 02:49 PM

C/C++ can only "return" a single value.. but there are a number of ways you can get arround this:

#1 Return a structure...
#2 Pass arguments by reference (either using a pointer ( C ) or a reference(C++))
#3 return a pointer to a structure/array
#4 (and least useful/most troublesome) use a global variable.

For what you are doing it sounds like #2 would be the way to go.
Was This Post Helpful? 1
  • +
  • -

#3 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Need Help With Return Multiple Value in Function!

Posted 06 October 2009 - 02:54 PM

To make a reference is very simple
#include <iostream>
using namespace std;
void one(int &number){//receive on function with an ambersan before the name
number=1;
}
int main()
{
int i=0;//declare variable thats gonna store
one(i);//send it to function
cout<<i;//output :)
cin.get();//to make a pause
return 0;
}


Hope that helps :P
Was This Post Helpful? 1
  • +
  • -

#4 vv29   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-September 09

Re: Need Help With Return Multiple Value in Function!

Posted 06 October 2009 - 02:59 PM

Thank you guys but could you edit directly my code since I'm still blind to use it. I'm sorry because I don't intend to let you guys complete my homework but I really want to understand the concept. I have tried calling address by *, however, it still doesn't work!
@poncho4all: Thank for your code but some parts are really weird to me. one, cout, cin. I never used it before :(
Was This Post Helpful? 0
  • +
  • -

#5 poncho4all   User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,422
  • Joined: 15-July 09

Re: Need Help With Return Multiple Value in Function!

Posted 06 October 2009 - 03:24 PM

Oops my bad sorry man i only read the problem didnt even saw the code, you are using C so if you wish to return a value to the main from the function IsInt then
int isInt(char in[], int length, char &letter)//this new variable is gonna be the one we will be returning by reference
{
	int i;
	for (i=0; i<length; i=i+1)
	{
		if(in[i] < '0' || in[i] > '9')
{	   letter=in[i];
		return 0;
}
	}	
	return 1;
} 


Now on the main you would have to just declare the variable letter.
int isInt(char in[], int length, char &);//on the function declaratio declcare the reference like that
int main(void)
{
	char in[20];
	int length,b;
	char l;//Declared new variable
	getInput(in);
	printf("You entered: %s\n", in);
	length = strlen(in);
	b = isInt(in, length,l);//here i send the declared variable
	printf("%c",l);//printing the character 



Hope that helps

This post has been edited by poncho4all: 06 October 2009 - 03:26 PM

Was This Post Helpful? 1
  • +
  • -

#6 vv29   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 20-September 09

Re: Need Help With Return Multiple Value in Function!

Posted 06 October 2009 - 03:49 PM

@ poncho4all : Greatly thank you! But I have declare it using * rather than &. However, your code helps me get into the point. Thank you again!!!
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Need Help With Return Multiple Value in Function!

Posted 06 October 2009 - 04:13 PM

Sounded like fun, so I wrote up a little proggy to demonstrate a number of ways to do it:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/* Returns actual character */
int isInt(const char *input, char *firstInvalidChar)
{
    int i = 0;

    /* Set to null char */
    *firstInvalidChar = '\0';

    /* While we've not found an invalid character,                              
       and we've not reached the end of the input string */
    while (*firstInvalidChar == '\0' && input[i] != '\0')
    {
        /* Create a temp var, so we can increment i */
        char check = input[i++];
        if (check < '0' || check > '9')
            /* Set the value pointed to by firstInvalidChar                     
               to the first invalid char (natch) */
            *firstInvalidChar = check;
    }

    /* Return whether we've found an invalid char */
    return *firstInvalidChar == '\0';
}

/* Returns index of bad character in array */
int isInt2(const char *input, int *firstInvalidIndex)
{
    int i = 0;
    while (input[i] != '\0')
    {
        if (input[i] < '0' || input[i] > '9')
        {
            *firstInvalidIndex = i;
            return 0;
        }
        ++i;
    }

    return 1;
}

/* Returns a shadow array of ints with the bad                                  
 characters set to 1. */
int isInt3(const char *input, int **badChars)
{
    int hasBad = 0;
    int i = 0;
    int *bad = calloc(strlen(input), sizeof(*badChars));
    while (input[i] != '\0')
    {
        if (input[i] < '0' || input[i] > '9')
{
            hasBad = 1;
            bad[i] = 1;
        }
        ++i;
    }

    *badChars = bad;
    return hasBad == 0;
}

int main()
{
    char input[128];
    char badInput;
    printf("Enter a string (empty line to quit): ");
    while (fgets(input, sizeof(input), stdin))
    {
        if (strlen(input) == 1)
            break;
        input[strlen(input) - 1] = '\0';
        if (!isInt(input, &badInput))
            printf("Input %s is bad per isInt. First bad letter is %c\n",
                   input, badInput);
        int badCharIndex;
        if (!isInt2(input, &badCharIndex))
            printf("Input %s is bad per isInt2. First bad letter is %c\n",
                   input, input[badCharIndex]);
        int *badCharList = NULL;
        if (!isInt3(input, &badCharList))
        {
            unsigned int i = 0;
            unsigned int len = strlen(input);
            printf("Input is bad per isInt3\n");
            printf("\t%s\n\t", input);
            for (; i < len; ++i)
            {
                if (badCharList[i] == 1)
                    printf("^");
                else
                    printf(" ");
            }
            printf("\n");
            free(badCharList); /* Free memory */
        }
        printf("Enter a string (empty line to quit): ");
    }
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1