Page 4 of 5 FirstFirst ... 2 3 4 5 LastLast
Results 61 to 80 of 95

Thread: C++ question im confused     submit to reddit submit to twitter

  1. #61
    Yoshi P
    Join Date
    Jul 2005
    Posts
    5,016
    BG Level
    8

    Chapter 3 seems to explain functions in pretty basic terms. If that's still too difficult, you might consider dropping the class. Even if I tell you the answer to all your questions, it won't help. The problem you're dealing with is the Bubbly Bernie of programming, and you're going to be fighting Kirin in about a month.

    You can blame it on the teacher and/or the textbook, but the harsh reality is that it's going to get fucking hard later on no matter who your teacher and/or textbook is, and unless you get used to relying only on yourself, you won't make it.

    i thought that i declared them above at the top os there wouldnt be a problem. Unless i'm writing it wrong?
    What you wrote up top was a function prototype. Not a variable declaration.


    You might want to read these four articles, since it seems you're having trouble on a conceptual level as well:
    Local Variables
    Global Variables
    Function Prototype
    Functions

  2. #62
    VZX
    VZX is offline
    Relic Shield
    Join Date
    Dec 2006
    Posts
    1,700
    BG Level
    6
    FFXI Server
    Asura

    Quote Originally Posted by divisortheory
    Chapter 3 seems to explain functions in pretty basic terms. If that's still too difficult, you might consider dropping the class. Even if I tell you the answer to all your questions, it won't help. The problem you're dealing with is the Bubbly Bernie of programming, and you're going to be fighting Kirin in about a month.

    You can blame it on the teacher and/or the textbook, but the harsh reality is that it's going to get fucking hard later on no matter who your teacher and/or textbook is, and unless you get used to relying only on yourself, you won't make it.

    i thought that i declared them above at the top os there wouldnt be a problem. Unless i'm writing it wrong?
    What you wrote up top was a function prototype. Not a variable declaration.
    beginner level course final/project should be at max a hakutaku

  3. #63
    Salvage Bans
    Join Date
    Dec 2005
    Posts
    946
    BG Level
    5

    Quote Originally Posted by LinktheDeme
    Quote Originally Posted by divisortheory
    Quote Originally Posted by LinktheDeme
    Quote Originally Posted by Charla
    Quote Originally Posted by LinktheDeme

    how did you clean it up?
    Well, he fixed your function call with assignments in it, then he probably made another variable to stick the final result in before returning it, kinda like we've all been saying to do.

    Seriously though, there's something wrong if you think "return x + y * z + pow(n, 2)" is a good idea.
    Could you give me an example of an assignment please?

    edit: @ aurik how the fuck am i supposed to learn how to do that complicated shit with no knowledge base on functions in my 3rd class
    While I won't take quite as abrupt of a stance as aurik, I do agree with him. How can you honestly ask for an example of an assignment and not expect people to be like "wtf"?

    The reason he's bashing you is because you are making no effort on your own to actually learn this material. You're taking the standard cop out approach of "holy shit it's complicated wtf is going on lemme ask someone else" without making a serious and dedicated effort to figure it out. Not knowing what an "assignment" is means you probably need to drop the class. It's more or less the same as signing up for College Algebra and then asking someone to explain what addition is. it's probably explained on page 1 or 2 of your book. If you even know what the word "assignment" means in the English language, and you've actually made a serious effort to look at your book for more than 5 minutes, you would have seen a glaring example of something and said "holy shit, I bet that's an assignment."

    The problme is you're throwing your hands up in the air at the slightest hint of adversity, when quite frankly even the lowest of the lowest common denominator could figure it out with a little effort.
    I've been trying to figure this shit out since 12:00 PM CST and its now 10:00 PM CST. i know very little of programming and have looked at every page about 10 times. There has only been 3 classes (50 minute classes)
    If you're having trouble this early and getting frustrated, you may want to consider another major. CS/EE will get a lot tougher where you will need to look within yourself in understanding the problems given.

  4. #64
    Yoshi P
    Join Date
    Jul 2005
    Posts
    5,016
    BG Level
    8

    Quote Originally Posted by VZX
    beginner level course final/project should be at max a hakutaku
    Touche

  5. #65
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    Quote Originally Posted by divisortheory
    Quote Originally Posted by VZX
    beginner level course final/project should be at max a hakutaku
    Touche
    I get most of the other stuff its just this function crap has got me stumped, I dont get how to write one, I dont get how to define one, I dont understand where to place it, And i definitely don't get the prototype bs

    edIT: the book has an example of a one variable function but i need to do one with a two variable and im still a little confused on a one variable one

  6. #66
    VZX
    VZX is offline
    Relic Shield
    Join Date
    Dec 2006
    Posts
    1,700
    BG Level
    6
    FFXI Server
    Asura

    You probably know what function is since elementary school. Teachers just won't say it "function" to you
    example : function to compute the area of rectangle
    variables you need : 2 sides of rectangle
    what you do : multiply them both
    your response : give the number multiplied to the caller

    it's as simple as that.
    I know some people got shocked when they learn recursion. But this one is the thing you shouldn't ever get confused with

  7. #67
    Salvage Bans
    Join Date
    Dec 2005
    Posts
    946
    BG Level
    5

    Quote Originally Posted by LinktheDeme
    Quote Originally Posted by divisortheory
    Quote Originally Posted by VZX
    beginner level course final/project should be at max a hakutaku
    Touche
    I get most of the other stuff its just this function crap has got me stumped, I dont get how to write one, I dont get how to define one, I dont understand where to place it, And i definitely don't get the prototype bs

    edIT: the book has an example of a one variable function but i need to do one with a two variable and im still a little confused on a one variable one
    Use google. "c++ function define prototype" gave me http://en.wikipedia.org/wiki/Function_prototype

  8. #68
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    I think im starting to understand functions a bit but how would i make something a two variable function

    // homewoxk3.cpp : Defines the entxy point fox the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    #include <cmath>

    using namespace std;

    double Temp(double stuff);
    double Temp2(double stuff2);


    int main ()
    {
    cout << "Please input temperature in Fahrenheit ";
    double t;
    cin >> t;
    cout << "Please input relative humidity ";
    double x;
    cin >> x;

    double hi = Temp(t);
    double bye = Temp2(x);

    cout << "The answer is " << hi << "\n";
    }



    double (Temp2, Temp)
    {
    double Temp2(double x);
    double Temp (double t);

    return (t-x);
    }
    If i only had to subtract one variable in the botton thing for the function( the return thing) I would get it easy but I have no idea what the fuck to do since i have to use more than one variable in the function if someone would show or explain how im pretty sure i could do this greater ease

    edit: btw ignore some of the stupid mistakes i made in the thing it was me trying random stuff to try to get it to recognize the two variables at the bottom

  9. #69
    Yoshi P
    Join Date
    Jul 2005
    Posts
    5,016
    BG Level
    8

    I'm only going to explain this once, since I need to go to sleep. If this isn't sufficient for you to undersatnd, you may want to consider dropping. I'm not saying it to be mean or anything, but like Simon from American Idol, it's better for someone to tell you straight so you don't waste your life away trying for something which you won't succeed at.

    For the record, your book probably explains all of this in the same terms (I know because I used the Look Inside function of Amazon)

    What is a function?
    A function is a small piece of code that is designed to perform an easily understood task. Functions have names that can be pretty much whatever you want. Usually the name of the function is something that represents what its purpose is. For example, AreaOfRegularTrapezoid is a "good" name for a function.

    Prototype vs. Definition
    A function prototype is a hint to the compiler that a function with a certain name exists. It tells the compiler "I haven't yet told you what this function does, but a function with the following name and the following "arguments" exists. Therefore, please don't generate compiler errors when the function is "called".

    Examples of function prototypes:
    int AreaOfSquare(int SideLength);
    int AreaOfSquare(int);
    int LengthOfRightTriangleHypotenuse(int SideA, int SideB);

    Note the difference between the first and second examples. The compiler doesn't give a rat's ass what you call the arguments in the prototype. You can call them whatever you want. The only thing that must match up with a "definition" are the return type, the name of the function, and the TYPES of the arguments. Since the compiler doesn't care about names of arguments at this point, it's valid to completely omit them.

    Function prototypes never appear within {} {} blocks define what are called "scopes", and function prototypes always appear outside of any and all scopes in the entire program. The same is true for function definitions, it is not legal to "define" a function inside any scope.

    A function definition is a full specification of the function. It tells the compiler HOW to perform the operation. For the function CalculateAreaOfSquare, it will include logic to multiply the length of the side by itself.


    In order for you to "call" a function (i.e. use it to perform a task), at least one of the prototype or the definition must appear BEFORE the point of the code where you call the function.

    How to write a function
    Code:
    ReturnType FunctionName(Argument List Goes Here)
    {
        //Code to execute function goes here.
    }
    a function meters to millimeters might look like this:
    Code:
    int ConvertMetersToMillimeters(int NumberOfMillimeters)
    {
        return NumberOfMillimeters * 1000;
    }

    notice that that "int main" crap you're required to type in every program looks like a function. That's because it is one. You've been writing functions all along and didn't even know it.

    How to call a function
    As mentioned above, a function must have been "seen" in some form (either prototype or definition) before you attempt to call it. This means that putting the the following code in a file:


    Code:
    int main()
    {
        int NumberOfMillimeters = ConvertMetersToMillimeters(5);
    }
    
    int ConvertMetersToMillimeters(int NumberOfMeters)
    {
        return NumberOfMeters * 1000;
    }
    and compiling it will fail. However either this:

    Code:
    int ConvertMetersToMillimeters(int NumberOfMeters)
    {
        return NumberOfMeters * 1000;
    }
    
    int main()
    {
        int NumberOfMillimeters = ConvertMetersToMillimeters(5);
    }
    or this:

    Code:
    int ConvertMetersToMillimeters(int);
    
    int main()
    {
        int NumberOfMeters = ConvertMetersToMillimeters(5);
    }
    
    int ConvertMetersToMillimeters(int NumberOfMeters)
    {
        return NumberOfMeters * 1000;
    }
    will work.


    If you have any other questions (about functions anyway), you need to look within for the answer. The #1 point of this course is teaching you to rely on yourself for solving problems of an unfamiliar nature.


    Edit: Here's an example prototype for a function that takes two arguments, as well as code that calls the function. It's up to you to figure out how write the definition, thus completing the program.

    Code:
    int AddTwoNumbersTogether(int FirstNumber, int SecondNumber);
    
    int main()
    {
        int ThreePlusFour = AddTwoNumbersTogether(3, 4);
    }

  10. #70
    VZX
    VZX is offline
    Relic Shield
    Join Date
    Dec 2006
    Posts
    1,700
    BG Level
    6
    FFXI Server
    Asura

    Quote Originally Posted by LinktheDeme
    I think im starting to understand functions a bit but how would i make something a two variable function

    // homewoxk3.cpp : Defines the entxy point fox the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    #include <cmath>

    using namespace std;

    double Temp(double stuff);
    double Temp2(double stuff2);


    int main ()
    {
    cout << "Please input temperature in Fahrenheit ";
    double t;
    cin >> t;
    cout << "Please input relative humidity ";
    double x;
    cin >> x;

    double hi = Temp(t);
    double bye = Temp2(x);

    cout << "The answer is " << hi << "\n";
    }



    double (Temp2, Temp)
    {
    double Temp2(double x);
    double Temp (double t);

    return (t-x);
    }
    If i only had to subtract one variable in the botton thing for the function( the return thing) I would get it easy but I have no idea what the fuck to do since i have to use more than one variable in the function if someone would show or explain how im pretty sure i could do this greater ease
    just imagine if you wanna do it by hand

    bolded things you need to change

  11. #71
    Relic Horn
    Join Date
    Mar 2006
    Posts
    3,215
    BG Level
    7

    Kirin is actually easier than Hakutaku most of the time because you aren't trying to fight it 12 times in a row with half of the critical people dropping in the meantime. And the final project of an entry level programming class is more like doing Bune with 4 guys imo.

    Getting back on topic:

    You can skip the prototype part by just putting the sub function above the main one (where it's called). Don't even bother trying to have two different variables with the same name, that's an easy way to mess yourself up. If you want t and x to be double, make them double everywhere. In this case, the simplest way to do the cin statements would be

    cin >> double t;
    and
    cin >> double x;

    Or you could do

    double x, t;
    ...

    cin >> t;
    cin >> x;

    As for the header files (lines with #include), you probably don't need to use them at all unless the teacher explicitely told you to, and hopefully told you exactly which ones to use and how. I could be wrong, but #include <cmath> shouldn't be necessary for what you're trying to do.

    Other than that, the original problem you posted sounds ok for a beginner problem, but the mess of algebra you posted later doesn't. He probably doesn't want you to calculate anything based on the temperature and humidity unless he said so.

    edit: ignore this stuff, whatshisname said it better while I was typing
    Summary of a function:

    Return_type Function_name(Variable_name1, Variable_name2, Variable_name3)
    {
    Stuff_to_do;
    }

    Now, right after the { is where you should be putting the declarations. Right before the } is where you should probably have a return statement. That looks like

    return Variable_name;

    Now, when you want to call that function, you use Function_name(Argument_name1, Argument_name2, Argument_name3);

    Then that turns into whatever is in the return statement of Function_name. If the function in question has no arguments, you use Function_name(). The () are important.

  12. #72
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    side note if i put -42.379
    and -(5.48E-2)
    Will it understand this or do i need to convert it?

  13. #73
    Relic Horn
    Join Date
    Mar 2006
    Posts
    3,215
    BG Level
    7

    -42.379 will be parsed correctly if assigned to a float or double.
    I have no idea what -(5.48E-2) is supposed to mean. Is that supposed to mean -(5.4 * 10^-2), scientific notation for -.054 or some sort of special format for humidity? In the first case it might at least attempt to parse, but I wouldn't try it, especially not in a simple case like 10^-2.

  14. #74
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    // homework3.cpp : Defines the entxy point fox the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    #include <cmath>

    using namespace std;

    double HeatIndex(double r, double t);

    int main()
    {
    cout << "Enter a Fahrenheit temperature ";
    double TempFahren;
    cin >> TempFahren;
    cout << "Enter relative humidity ";
    double TempRel;
    cin >> TempRel;

    double Heat = HeatIndex(TempFahren, TempRel);

    cout << "The answer is " << Heat;
    }

    double HeatIndex(double r, double t)
    {
    return (r - t);
    OSNAP I GOT IT

    and Id assume E-3 is ^-3

    edit: Thanks for everyones help thus far i appreciate the patience of everyone.

    I've put this equaion in for the return
    (-42.379 + 2.04901523 * t + 10.14333127 * r -0.22475541 * t * r - (6.83783 * -1000) * pow(t, 2) -(5.48171 * -100) * pow(r, 2) + (1.22874 * -1000) * pow(t, 2) * r +(8.5282 * -10000) * t * pow(r, 2) - (1.99E *-1000000) * pow(t, 2) * pow(r, 2));
    And get this error i cant understand it
    1>c:\documents and settings\dimitriosl\desktop\school\c++ assignments\homework3\homework3\homework3.cpp(27) : error C2021: expected exponent value, not ' '
    edit: #2 was error because i forgot E in it i guess it does understand Exponents as E?

  15. #75
    Nikkei's Hoe
    Worse than her at uno

    Join Date
    Dec 2006
    Posts
    6,236
    BG Level
    8
    FFXIV Character
    Eanae Hikari
    FFXIV Server
    Gilgamesh
    FFXI Server
    Cerberus
    WoW Realm
    Hyjal

    Quote Originally Posted by LinktheDeme
    // homework3.cpp : Defines the entxy point fox the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    #include <cmath>

    using namespace std;

    double HeatIndex(double r, double t);

    int main()
    {
    cout << "Enter a Fahrenheit temperature ";
    double TempFahren;
    cin >> TempFahren;
    cout << "Enter relative humidity ";
    double TempRel;
    cin >> TempRel;

    double Heat = HeatIndex(TempFahren, TempRel);

    cout << "The answer is " << Heat;
    }

    double HeatIndex(double r, double t)
    {
    return (r - t);
    OSNAP I GOT IT

    and Id assume E-3 is ^-3

    edit: Thanks for everyones help thus far i appreciate the patience of everyone.

    I've put this equaion in for the return
    [quote:55d7b](-42.379 + 2.04901523 * t + 10.14333127 * r -0.22475541 * t * r - (6.83783 * -1000) * pow(t, 2) -(5.48171 * -100) * pow(r, 2) + (1.22874 * -1000) * pow(t, 2) * r +(8.5282 * -10000) * t * pow(r, 2) - (1.99E *-1000000) * pow(t, 2) * pow(r, 2));
    And get this error i cant understand it
    1>c:\documents and settings\dimitriosl\desktop\school\c++ assignments\homework3\homework3\homework3.cpp(27) : error C2021: expected exponent value, not ' '
    edit: #2 was error because i forgot E in it i guess it does understand Exponents as E?[/quote:55d7b]

    it'd be neater if you stored that into a variable and then returned the variable.

  16. #76
    VZX
    VZX is offline
    Relic Shield
    Join Date
    Dec 2006
    Posts
    1,700
    BG Level
    6
    FFXI Server
    Asura

    Quote Originally Posted by LinktheDeme

    I've put this equaion in for the return
    (-42.379 + 2.04901523 * t + 10.14333127 * r -0.22475541 * t * r - (6.83783 * -1000) * pow(t, 2) -(5.48171 * -100) * pow(r, 2) + (1.22874 * -1000) * pow(t, 2) * r +(8.5282 * -10000) * t * pow(r, 2) - (1.99E *-1000000) * pow(t, 2) * pow(r, 2));
    And get this error i cant understand it
    [quote:b310e]1>c:\documents and settings\dimitriosl\desktop\school\c++ assignments\homework3\homework3\homework3.cpp(27) : error C2021: expected exponent value, not ' '
    edit: #2 was error because i forgot E in it i guess it does understand Exponents as E?[/quote:b310e]
    you know what to do

    E can simply be translated to '*10^'

  17. #77
    Nikkei's Hoe
    Worse than her at uno

    Join Date
    Dec 2006
    Posts
    6,236
    BG Level
    8
    FFXIV Character
    Eanae Hikari
    FFXIV Server
    Gilgamesh
    FFXI Server
    Cerberus
    WoW Realm
    Hyjal

    Also, delete #include <iostream> at the top and put #include <cmath> into stdafx.h... this is the general header file in which all #include statements should go.

  18. #78
    VZX
    VZX is offline
    Relic Shield
    Join Date
    Dec 2006
    Posts
    1,700
    BG Level
    6
    FFXI Server
    Asura

    Quote Originally Posted by Eanae
    Also, delete #include <iostream> at the top and put #include <cmath> into stdafx.h... this is the general header file in which all #include statements should go.
    you can't read i/o if you delete #include <iostream> right

  19. #79
    Relic Horn
    Join Date
    Mar 2006
    Posts
    3,215
    BG Level
    7

    You can if you #include it inside something else that you #include. Whether to put it in the main file or the other header file is a matter of style really.

  20. #80
    Nidhogg
    Join Date
    Jul 2006
    Posts
    3,999
    BG Level
    7

    // homework3.cpp : Defines the entxy point fox the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    #include <cmath>

    using namespace std;

    double HeatIndex(double t, double r);

    int main()
    {
    cout << "Enter a Fahrenheit temperature ";
    double TempFahren;
    cin >> TempFahren;
    cout << "Enter relative humidity ";
    double TempRel;
    cin >> TempRel;

    double Heat = HeatIndex(TempFahren, TempRel);

    cout << "The answer is " << Heat;
    }

    double HeatIndex(double t, double r)
    {
    return (-42.379 + 2.04901523 * t + 10.14333127 * r
    -0.22475541 * t * r - (6.83783 * -1000) * pow(t, 2)
    -(5.48171 * -100) * pow(r, 2) + (1.22874 * -1000) * pow(t, 2) * r
    +(8.5282 * -10000) * t * pow(r, 2) - (1.99 *-1000000) * pow(t, 2) * pow(r, 2));
    }
    /* Enter a Fahrenheit temperature 4
    Enter relative humidity 2
    The answer is 1.26068e+008Press any key to continue . . .
    */
    end product the /* is notes of a result what concerns me is that it differs from what that other guy had

    edit: Thanks again everyone

Page 4 of 5 FirstFirst ... 2 3 4 5 LastLast

Similar Threads

  1. a tech question because im a noob
    By Medivac in forum General Discussion
    Replies: 11
    Last Post: 2006-09-21, 23:57
  2. Just got a question for you guys
    By Blackwar in forum General Discussion
    Replies: 3
    Last Post: 2004-08-06, 20:14
  3. psst.. im at work...
    By Judah in forum General Discussion
    Replies: 6
    Last Post: 2004-07-19, 14:47