13 Replies - 668 Views - Last Post: 08 July 2008 - 12:27 PM Rate Topic: -----

#1 yongone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-July 08

Calling user input in the function

Post icon  Posted 07 July 2008 - 12:57 PM

#include <stdio.h>

int higher_num(int x, int y);

int main()
{
int x, y;

printf("Enter an integer: ");
scanf("%d", &x);
printf("Enter another integer: ");
scanf("%d", &y);

printf("The higher number is %f\n", higher_num(x, y));

return(0);
}

int
higher_num(int x, int y)
{
return( if (x > y)
printf("%f\n", x);
else
printf("%f\n", y);
)

}



ok... i dunno where im going wrong.
the question is: Write a program with a function (give it a meaningful name) that takes as parameters two integers and prints the larger integer with a meaningful message. In main, use scanf to get two integers from the user and pass them to your new function. Hint: test the function by calling it from main on two integer constants - after you know it works correctly, add the scanfs.

i think i have it right.. but apparently its wrong..

Is This A Good Question/Topic? 0
  • +

Replies To: Calling user input in the function

#2 Einherjar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 10-February 08

Re: Calling user input in the function

Posted 07 July 2008 - 01:22 PM

View Postyongone, on 7 Jul, 2008 - 03:57 PM, said:

#include <stdio.h>

int higher_num(int x, int y);

int main()
{
int x, y;

printf("Enter an integer: ");
scanf("%d", &x);
printf("Enter another integer: ");
scanf("%d", &y);

printf("The higher number is %f\n", higher_num(x, y));

return(0);
}

int
higher_num(int x, int y)
{
return( if (x > y)
printf("%f\n", x);
else
printf("%f\n", y);
)

}



ok... i dunno where im going wrong.
the question is: Write a program with a function (give it a meaningful name) that takes as parameters two integers and prints the larger integer with a meaningful message. In main, use scanf to get two integers from the user and pass them to your new function. Hint: test the function by calling it from main on two integer constants - after you know it works correctly, add the scanfs.

i think i have it right.. but apparently its wrong..


You can't really do that in the return statement of that function. I think this is what they are actually asking for:

void higher_num(int x, int y)
{
  if(x > y)
	printf("%d\n", x);
  else
	printf("%d\n", y);
}


Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Calling user input in the function

Posted 07 July 2008 - 01:31 PM

Correct me if I'm wrong, but I think your instructor probably wants you to return the highest, and be able to print the return value, like so:
#include <cstdlib>

float higher_num(int x, int y);

int main()
{
    printf("%.2f", higher_num (1,2));

    cin.get ();
    return EXIT_SUCCESS;
}

float higher_num(int x, int y)
{
    if (x > y)
        return x;
    else return y;
}

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#4 Einherjar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 10-February 08

Re: Calling user input in the function

Posted 07 July 2008 - 01:38 PM

View Postgabehabe, on 7 Jul, 2008 - 04:31 PM, said:

Correct me if I'm wrong, but I think your instructor probably wants you to return the highest, and be able to print the return value, like so:
#include <cstdlib>

float higher_num(int x, int y);

int main()
{
    printf("%.2f", higher_num (1,2));

    cin.get ();
    return EXIT_SUCCESS;
}

float higher_num(int x, int y)
{
    if (x > y)
        return x;
    else return y;
}

Hope this helps :)


Or this works too, and actually demonstrates how return values work.
Was This Post Helpful? 0
  • +
  • -

#5 yongone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-July 08

Re: Calling user input in the function

Posted 07 July 2008 - 01:45 PM

View PostEinherjar, on 7 Jul, 2008 - 01:38 PM, said:

View Postgabehabe, on 7 Jul, 2008 - 04:31 PM, said:

Correct me if I'm wrong, but I think your instructor probably wants you to return the highest, and be able to print the return value, like so:
#include <cstdlib>

float higher_num(int x, int y);

int main()
{
    printf("%.2f", higher_num (1,2));

    cin.get ();
    return EXIT_SUCCESS;
}

float higher_num(int x, int y)
{
    if (x > y)
        return x;
    else return y;
}

Hope this helps :)


Or this works too, and actually demonstrates how return values work.


i havnt learned cin.get is there another way?
i did this:

#include <stdio.h>

int higher_num(int x, int y);

int main()
{
  int x, y;

  printf("Enter an integer: ");
  scanf("%d", &x);
  printf("Enter another integer: ");
  scanf("%d", &y);
  printf ("%d\n", higher_num(x, y));

  return(0);
}

int
higher_num(int x, int y)
  {
	if(x > y)
	  printf("The higher number is: %d\n", x);
	else
	  printf("The higher number is: %d\n", y);
  }




this is giving me the highest number but the the printf(%d\n", higher_num(x,y)); is giving me 24.
Was This Post Helpful? 0
  • +
  • -

#6 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: Calling user input in the function

Posted 08 July 2008 - 05:43 AM

Try system("PAUSE"); in place of cin.get();
Was This Post Helpful? 0
  • +
  • -

#7 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: Calling user input in the function

Posted 08 July 2008 - 05:46 AM

your function higher_num's return type is int but it doesnt returns anything??
i think you can make it void rest is all correct :)
i just read in earlier post that system("PAUSE") is heavy in computation wise and should be avoided instead cin.get can be used to pause the screen??

This post has been edited by brainy_creature: 08 July 2008 - 05:49 AM

Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Calling user input in the function

Posted 08 July 2008 - 05:47 AM

And may I ask why it would be advised to use system ("pause"); over using cin.get(); ?
Was This Post Helpful? 0
  • +
  • -

#9 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: Calling user input in the function

Posted 08 July 2008 - 06:03 AM

View Postgabehabe, on 8 Jul, 2008 - 05:47 AM, said:

And may I ask why it would be advised to use system ("pause"); over using cin.get(); ?


If he already has the input read in and wants intermitent pauses in between that's what I would suggest, not to mislead from the original topic.
Was This Post Helpful? 0
  • +
  • -

#10 brainy_creature  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 177
  • Joined: 07-August 06

Re: Calling user input in the function

Posted 08 July 2008 - 06:15 AM

View Postcaptainhampton, on 8 Jul, 2008 - 06:03 AM, said:

View Postgabehabe, on 8 Jul, 2008 - 05:47 AM, said:

And may I ask why it would be advised to use system ("pause"); over using cin.get(); ?


If he already has the input read in and wants intermitent pauses in between that's what I would suggest, not to mislead from the original topic.

what do you mean by pauses in between??
can you give me an example so that i can understand better
thanks :)
Was This Post Helpful? 0
  • +
  • -

#11 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Calling user input in the function

Posted 08 July 2008 - 06:18 AM

I would actually advise using the system(); function at all costs... Take a look at the pinned topic, "holding the execution window open" here in the C/C++ forum for alternatives to "pause"

Why I advise against it:
  • Only works on systems that have "pause" as a system shell command
  • Takes more processing than alternatives:
    • Pauses the Program

    • Opens the system shell

    • Calls the actual shell command passed to the system function

    • Closes the shell

    • Continues the program

Now, you could get some identical results using other methods, which take nowhere near as much processing than calling system()

</rant>
Was This Post Helpful? 0
  • +
  • -

#12 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Calling user input in the function

Posted 08 July 2008 - 10:00 AM

Gabehabe, you never opened the rant html tag...

Though system uses a lot of processing power, it also has its uses for stuff other than pausing the window.

As much as I like to hammer on gabehabe, his code is the best snippet suggested so far (no offense everyone else).

If i'm not mistaken, isn't cin.get c++, not c?

BTW: You never checked it against integer constants like your instructor said. Before any of your output wouldn't you have to use : printf("The higher number between 1 and 2 is: %d\n", higher_num(1,2));
Was This Post Helpful? 0
  • +
  • -

#13 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Calling user input in the function

Posted 08 July 2008 - 12:06 PM

Thanks poly :)
And yeah, my bad... cin.get(); is C++ standard, but still, there are plenty more ways to hold the window open.

I do agree with you that system can be useful, but for something with such simple alternatives, it isn't really necessary.
Was This Post Helpful? 0
  • +
  • -

#14 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Calling user input in the function

Posted 08 July 2008 - 12:27 PM

True.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1