Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,176 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,388 people online right now. Registration is fast and FREE... Join Now!




Calling user input in the function

2 Pages V  1 2 >  
Reply to this topicStart new topic

Calling user input in the function

yongone
7 Jul, 2008 - 11:57 AM
Post #1

New D.I.C Head
*

Joined: 7 Jul, 2008
Posts: 5

CODE

#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..
User is offlineProfile CardPM
+Quote Post

Einherjar
RE: Calling User Input In The Function
7 Jul, 2008 - 12:22 PM
Post #2

D.I.C Head
**

Joined: 10 Feb, 2008
Posts: 73


My Contributions
QUOTE(yongone @ 7 Jul, 2008 - 03:57 PM) *

CODE

#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:

CODE

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

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Calling User Input In The Function
7 Jul, 2008 - 12:31 PM
Post #3

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
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:
cpp
#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 smile.gif
User is online!Profile CardPM
+Quote Post

Einherjar
RE: Calling User Input In The Function
7 Jul, 2008 - 12:38 PM
Post #4

D.I.C Head
**

Joined: 10 Feb, 2008
Posts: 73


My Contributions
QUOTE(gabehabe @ 7 Jul, 2008 - 04: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:
cpp
#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 smile.gif


Or this works too, and actually demonstrates how return values work.
User is offlineProfile CardPM
+Quote Post

yongone
RE: Calling User Input In The Function
7 Jul, 2008 - 12:45 PM
Post #5

New D.I.C Head
*

Joined: 7 Jul, 2008
Posts: 5

QUOTE(Einherjar @ 7 Jul, 2008 - 01:38 PM) *

QUOTE(gabehabe @ 7 Jul, 2008 - 04: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:
cpp
#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 smile.gif


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


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

CODE

#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.
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: Calling User Input In The Function
8 Jul, 2008 - 04:43 AM
Post #6

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
Try system("PAUSE"); in place of cin.get();
User is offlineProfile CardPM
+Quote Post

brainy_creature
RE: Calling User Input In The Function
8 Jul, 2008 - 04:46 AM
Post #7

D.I.C Head
**

Joined: 7 Aug, 2006
Posts: 174



Thanked: 1 times
My Contributions
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 smile.gif
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: 8 Jul, 2008 - 04:49 AM
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Calling User Input In The Function
8 Jul, 2008 - 04:47 AM
Post #8

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
And may I ask why it would be advised to use system ("pause"); over using cin.get(); ?
User is online!Profile CardPM
+Quote Post

captainhampton
RE: Calling User Input In The Function
8 Jul, 2008 - 05:03 AM
Post #9

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
QUOTE(gabehabe @ 8 Jul, 2008 - 05:47 AM) *

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.
User is offlineProfile CardPM
+Quote Post

brainy_creature
RE: Calling User Input In The Function
8 Jul, 2008 - 05:15 AM
Post #10

D.I.C Head
**

Joined: 7 Aug, 2006
Posts: 174



Thanked: 1 times
My Contributions
QUOTE(captainhampton @ 8 Jul, 2008 - 06:03 AM) *

QUOTE(gabehabe @ 8 Jul, 2008 - 05:47 AM) *

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 smile.gif

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Calling User Input In The Function
8 Jul, 2008 - 05:18 AM
Post #11

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
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:
    1. Pauses the Program
    2. Opens the system shell
    3. Calls the actual shell command passed to the system function
    4. Closes the shell
    5. Continues the program

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

</rant>
User is online!Profile CardPM
+Quote Post

polymath
RE: Calling User Input In The Function
8 Jul, 2008 - 09:00 AM
Post #12

D.I.C Regular
Group Icon

Joined: 4 Apr, 2008
Posts: 415



Thanked: 4 times
Dream Kudos: 500
My Contributions
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));
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 10:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month