Find the answer to your Linux question:
Page 2 of 3 FirstFirst 1 2 3 LastLast
Results 11 to 20 of 21
yeah i think i need some code to look at and see whats going on...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98

    yeah i think i need some code to look at and see whats going on

  2. #12
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Alrighty. So let's say I want a function that will utilize two variables at once. I could do something like the following:

    Code:
    #include <iostream>
    using namespace std;
    
    void test_func&#40;int a, int b&#41;
    &#123;
        cout << "The first num is " << a << ", and the second is " << b << endl;
    &#125;
    
    int main&#40;&#41;
    &#123;
        test_func&#40;2,3&#41;;
    
        return 0;
    &#125;
    The output here would be:

    "The first num is 2, and the second is 3"

    You see how test_func takes in two parameters, and can use them together?

    That's what you'd need in order to properly display the time.


    Do you also need help with the "06" issue?

  3. #13
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98
    with the code i saw from you this is all i could think of to make it work, but it still doesnt and yes i still need help with the 06

    Code:
    #include <iostream>
    using namespace std;
    void hi &#40;int a, int b&#41;;
    int main&#40;&#41;
    &#123;
            cout << "enter a number in hours " << endl;
            cin >> hi&#40;int a&#41;;
            cout <<  "enter a number in minutes" << endl;
            cin >> hi&#40;int b&#41;;
            return 0;
    &#125;
    
    void hi&#40;int a, int b&#41;
    &#123;
            cout << "the time is " << a << "&#58;" << b << endl;
    &#125;
    here are the errors i get when i run it, probably dont need them, but o well
    Code:
    sh-3.00$ g++ Time2.C
    Time2.C&#58; In function `int main&#40;&#41;'&#58;
    Time2.C&#58;7&#58; error&#58; parse error before `&#41;' token
    Time2.C&#58;9&#58; error&#58; parse error before `&#41;' token

  4. #14
    Linux User benjamin20's Avatar
    Join Date
    Oct 2004
    Location
    jacksonville beach FL
    Posts
    317
    you seem to be using the function wrong. when you make a function you have to send it everything you want it to deal with in one peice.

    also, when you use the function you dont put in the type of variable.

    also you are trying to pass a number to a function wrong.

    you typed

    cin >> hi(int a);

    which according to my second note (ignoring first note) should be said like this

    cin >> hi (a);

    ok, you just cant do that. you need a variable.

    int a;
    cin >> a;
    hi(a);

    now according to my first rule it should look like this.

    int a;
    int b;
    cin >> a;
    cin >> b;
    hi(a, b);

    now here comes the formality part. in c++ programming there are certian things we just do that we dont have to but we do as a way of creating "good code" im shure the book has given you the talk of proper symantics.
    one of the first thing it should have mentioned it that although we can specify all the code on one line we like to spread it out over multiple lines to make it easier to read. now another thing we do is whenever we name a varialbe or function, we give it a name that reflects its pourpose. in demo's we often use x and y or a and b. but in actual coe you should be using minuets and seconds. i saw you used cookies, oreo, and hi. that doesnt give anyone a clue as to what you are realy doing. now a lot of the rules are left up to you to make, but usualy you will se variables with multiple words have the first work uncapitalized and all sequential words capitalized. i usualy like to have all funtions with all words capitalized to help show its a function.

    void TimeOutPut (int inHours, int inMinuets)

    int hours;
    int minuets;

    notice that the names of the function variables are different than the other variables. thats because you dont have to have functions use the same variables, in fact, its actualy bad programing to try and do so because almost never will you be send ing the exact sme variables in all the time.
    nVidia G-Force 6600GT (bfg) pci-e: amd 64 2000+ (939): 1024 corsair ram: 2X 80gb seagate harddisk SATA: plextor cd/dvd-read/write cdrom SATA

  5. #15
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98
    Code:
    #include <iostream>
    using namespace std;
    void hi &#40;a,b&#41;;
    int main&#40;&#41;
    &#123;
            int a;
            int b;
            cout << "enter a number in hours " << endl;
            cin >> a;
            cout <<  "enter a number in minutes" << endl;
            cin >> b;
            hi &#40;a,b&#41;;
            return 0;
    &#125;
    
    void hi&#40;a,b&#41;
    &#123;
            cout << "the time is " << a << "&#58;" << b << endl;
    &#125;
    well thats what i thought you were saying 2 do, i guess not cuz thats not right either, lol, and btw the book did explain all about good coding methods, but in making these kinds of programs, i just found it worthless and more time consuming

  6. #16
    Linux User benjamin20's Avatar
    Join Date
    Oct 2004
    Location
    jacksonville beach FL
    Posts
    317
    well true. actualy in c++ because of the seperation of all the code you will often be declairing the same variable in different functions and classes (if you are outside of a function that declairs a variable, you can declair a variable by the same name. it will be covered when they start talking about global and private variables. the bookl i learned from (learn c++ in 24 hours) by the same company as yours is actualy a nice book but went a little fast and lest me somewhat confused with the more advanced stuff. classes just totaly blew my mind, and pointers!. bt you will soon realize that classes are super cool and pointers are critical (which is the main reason why i hate java, wich doesnt allow you to decide when to use pointers and when not to).

    but ya, its usualy just good programing practice to use whatever stupid little rules you have learned sofar so that they just come out naturaly later. like in for statements i always use k for the variable and stuff like that.

    oh ya, a way to make it do stuff like 06 for the minuets would be to tell it to print out a 0 if minuets is less than ten.

    if (b < 10)
    cout << "the time is " << a << ":0" << b << " o'clock" << endl;
    else
    cout << "the time is " << a << ":" << b << " o'clock" << endl;
    nVidia G-Force 6600GT (bfg) pci-e: amd 64 2000+ (939): 1024 corsair ram: 2X 80gb seagate harddisk SATA: plextor cd/dvd-read/write cdrom SATA

  7. #17
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98
    so can anyone see what i did wrong on the code i posted above? because it doesnt compile

  8. #18
    Linux User benjamin20's Avatar
    Join Date
    Oct 2004
    Location
    jacksonville beach FL
    Posts
    317
    yes, you make functions like this

    void hi (int a,int b)

    not

    void hi (a,b)
    nVidia G-Force 6600GT (bfg) pci-e: amd 64 2000+ (939): 1024 corsair ram: 2X 80gb seagate harddisk SATA: plextor cd/dvd-read/write cdrom SATA

  9. #19
    Just Joined!
    Join Date
    Mar 2005
    Posts
    98
    this is what i thought u were saying to do, but it didnt work
    Code:
    #include <iostream>
    using namespace std;
    void hi&#40;int a,int b&#41;;
    int main&#40;&#41;
    &#123;
            int a;    // here i tried int hi&#40;a&#41; too, but that didnt work either
            int b;   // here i tried int hi&#40;b&#41; too, but that didnt work either
            cout << "enter a number in hours " << endl;
            cin >> hi&#40;a&#41;;
            cout <<  "enter a number in minutes" << endl;
            cin >> hi&#40;b&#41;;
            return 0;
    &#125;
    
    void hi&#40;int a,int b&#41;
    &#123;
            cout << "the time is " << a << "&#58;" << b << endl;
    &#125;

  10. #20
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Right.

    Here, you have a function (hi) that takes in two parameters (a and b). So hi is actually:

    void hi(int a, int b)

    However, you are calling a function with only one parameter. The hi that you are calling is:

    void hi(int a)

    which of course, doesn't exist.


    The other problem is that you are trying to input data to a function that actually needs to be stored in a variable. So you might try something like this:
    Code:
    int a, b;
    
    cout << "Enter the Hours&#58; ";
    cin >> a;
    
    cout << "Enter the Minutes&#58; ";
    cin >> b;
    And then try doing the function call after that.

Page 2 of 3 FirstFirst 1 2 3 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •