Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,069 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,583 people online right now. Registration is fast and FREE... Join Now!




int get_value(void)

 
Reply to this topicStart new topic

int get_value(void), Cant't get it to return two varibles

puzzler5809
12 Apr, 2008 - 08:14 PM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 9

int get_value(void); - I put in two function calls because I can't get it to work as single function. I had it working with globals, but I can't get it to work with a local variables.
Below is my program with two functions, please tell me how to rewrite it with one. I won't get any points for it but it will help me to know where I am wrong for the final exam. I have tried it as one function and tried "return (x,y); - when I do this, it returns y but gives a "4" for x. Not sure why it won't work with locals when just "return;" worked with globals.


CODE
#include<iostream>
#include<cmath>
using namespace std;

int get_value_x(void);
int get_value_y(void);

double compute_distance(int x, int y);


int main()
{
    double dis;    
    char ans;
    int x, y;
    do
    {
    x = get_value_x();
    y = get_value_y();
    dis = compute_distance(x, y);

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);

    cout << "The point (" << x << "," << y
         << ") is " << dis << "  units from the origin.\n\n";

    cout << "Would you like to try another point (Y/N)? ";
    cin >> ans;
    cout << endl;
    }
    while ((ans == 'Y') || (ans == 'y'));

    return 0;
}
int get_value_x()
{
    int x;
    cout << "Please enter a nonnegative value for the x-coordinate: ";
    cin >> x;
    while (x < 0)
    {
        cout << "The value must be nonnegative, please try "
             << "again.\n";
        cout << "Please enter a nonnegative value: ";
        cin >> x;
    }
    return (x);
}
int get_value_y()
{
    int y;
    cout << "Please enter a nonnegative value for the y-coordinate: ";
    cin >> y;
    while (y < 0)
    {
        cout << "The value must be nonnegative, please try "
             << "again.\n";
        cout << "Please enter a nonnegative value: ";
        cin >> y;
    }
    return (y);
}
double compute_distance (int x, int y)
{
    double dis, z;
    z = (x * x) + (y * y);
    dis = sqrt(z);
    return dis;
}


Mod Edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Int Get_value(void)
12 Apr, 2008 - 08:43 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
are the code tags a real mystery? Recently I have been adding code tags to just about every other post. Please use the code tags. It is not hard you put a tag (the word "code" surrounded by square brackets) at the top of a section of code, and then you close the tag by surrounding "/code" by square brackets at the end of your code section. If you wanted to get *fancy* you could use "code=cpp" at the top to turn on syntax coloring for C/C++.

Please use code tags: code.gif


Your question:

Generally you can't return more than one value using the return statement. There are ways to do it but they are all more trouble then they are worth.

The best thing to do is to pass an argument by reference. This means either by pointer © or reference (preferred C++ way).

function:

CODE
void getValues(int& x, int& y) {
cin >> x;
cin >> y;
return;
}

int main() {
int first, second;
cout << "enter two values: "
getValues(first, second);
cout << "First: " << first << endl;
cout << "Second: " << second << endl;
return 0;
}


(note that is untested code but it should work).




User is offlineProfile CardPM
+Quote Post

puzzler5809
RE: Int Get_value(void)
12 Apr, 2008 - 09:02 PM
Post #3

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 9

Thanks for the help. But prehaps you could switch the instructions with "We Will Not Do Your Homework!" lines with the instructions for posting codes as most of us try to describe our problem before we post our code. That way we could see it. Or better yet put in red above. Don't worry, I won't be back. I don't like being treated like I am idoit, when this is the first time I have used your web site. Thanks for insulting me too.


QUOTE(NickDMax @ 12 Apr, 2008 - 09:43 PM) *

are the code tags a real mystery? Recently I have been adding code tags to just about every other post. Please use the code tags. It is not hard you put a tag (the word "code" surrounded by square brackets) at the top of a section of code, and then you close the tag by surrounding "/code" by square brackets at the end of your code section. If you wanted to get *fancy* you could use "code=cpp" at the top to turn on syntax coloring for C/C++.

Please use code tags: code.gif


Your question:

Generally you can't return more than one value using the return statement. There are ways to do it but they are all more trouble then they are worth.

The best thing to do is to pass an argument by reference. This means either by pointer © or reference (preferred C++ way).

function:

CODE
void getValues(int& x, int& y) {
cin >> x;
cin >> y;
return;
}

int main() {
int first, second;
cout << "enter two values: "
getValues(first, second);
cout << "First: " << first << endl;
cout << "Second: " << second << endl;
return 0;
}


(note that is untested code but it should work).


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Int Get_value(void)
12 Apr, 2008 - 09:09 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well I am sorry that my response was so rude and that you felt insulted. I do tend to get a little short with people after the 100th time I add code tags and you just happen to be the lottery winner on my bad temper. For that I am sorry.

Perhaps we really do need to examine our instructions a little to make sure that we explain how to post with code tags (really people don't seem to get it).

I do hope that you were able to finish reading my post and at least walked away with the answer to your question.
User is offlineProfile CardPM
+Quote Post

puzzler5809
RE: Int Get_value(void)
12 Apr, 2008 - 10:11 PM
Post #5

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 9

Yes I did read your post and it did help. I am also a little short at 2am. For that I am also sorry. Thanks for your help.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:54PM

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