Primary Expression Error Makes me Doubt my Grasp of Functions

This is a discussion on Primary Expression Error Makes me Doubt my Grasp of Functions within the C++ Programming forums, part of the General Programming Boards category; So I've been working through the C++ tutorial on this site by trying to slowly add the concepts I'm learning ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Primary Expression Error Makes me Doubt my Grasp of Functions

    So I've been working through the C++ tutorial on this site by trying to slowly add the concepts I'm learning into a text adventure. I had previously made a series of if statements that allowed the player to check their stats and environment, and rather than copy it out into the main function again and again, I thought it would be neat to turn it into a function so I could just call it up whenever the player had the option to look at their stats. What I brewed up for my function was this (I plan to add more options for the variable in the if statement as I go on so the result changes as the variables change through player actions):

    Code:
    //
    //
    void breathedeep (int weapon, int room, int healthstat, int spiritstat, int strengthstat, int dexteritystat)
    {
                cout << "You take a deep breath, close your eyes and look inward. You have a brief moment of deep awareness. \n Health: "<< healthstat << "\n Spirit:" << spiritstat << "\n Strength: " << strengthstat << "\n Dexterity: " << dexteritystat << "\n";
                if (weapon == 0)
                {
                    cout << "You're completely unshod, and unarmed.";
                }
                if (room == 1)
                {
                    cout << "You are in some sort of clinic. It is tidy and neat, but doesn't appear to have much in the way of medical supplies. Everything is worn down, but obvious efforts have been made to maintain what little is there. A mare in an orange vest resides here, looking watchful.";
    
                }
            }
    //
    //
    I inserted it into main when the option to look at your inventory came up like so:

    Code:
    //
    //
    if (tutorialchoiceone == 1)
        {
            cout << int breathedee (weapon, room, healthstat, spiritstat, strengthstat, dexteritystat) << "\n"; // I get two error messages on this line, one that says that the program expected a primary-expression before 'int' and another that says that it expected a ; before 'int'.
        } 
    //
    //
    It seems to fit with what the tutorial on this site described, and I have tried several modifications to the line, but nothing seems to let my program compile. I also tried googling what a primary-expression was in C++ terminology, but I couldn't seem to find a straight answer to that either.

    Would any of you more experienced and (I can only presume) mind bogglingly intelligent programmers be able to help a poor newbie out?

    Edit: I noticed the missing "p" at the end of my variable in the function call, but I still got the same issue.
    Last edited by chaucer345; 07-05-2012 at 03:21 PM. Reason: comment symbols

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    programmer does not mean intelligent really haha, means ' good at programming!'

    You dont need to write your return type when you call the function, so leave off int;
    Code:
    //
    cout <<  breatheDeep(..) << "\n"; 
    //
    Last edited by rogster001; 07-05-2012 at 03:50 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Hmm... I got rid of the return type like this:
    Code:
    //
    //
    if (tutorialchoiceone == 1)
        {
            cout << breathedeep (weapon, room, healthstat, spiritstat, strengthstat, dexteritystat) << "\n"; // I get two error messages on this line, one that says that the program expected a primary-expression before 'int' and another that says that it expected a ; before 'int'.
        } 
    //
    //
    But then I just got a different error message that read: C:\Documents and Settings\Administrator\Desktop\program\First Prince Text Adventure\main.cpp|100|error: no match for 'operator<<' in 'std::cout << breathedeep(weapon, room, healthstat, spiritstat, strengthstat, dexteritystat)'|

    Then I thought the (...) you typed might have been more significant, so I tried it again with:

    Code:
     
    //
    //
    if (tutorialchoiceone == 1)
        {
            cout << breathedeep (...) << "\n"; // I get two error messages on this line, one that says that the program expected a primary-expression before 'int' and another that says that it expected a ; before 'int'.
        } 
    //
    //
    But then I just get an error that says: C:\Documents and Settings\Administrator\Desktop\program\First Prince Text Adventure\main.cpp|100|error: expected primary-expression before '...' token|

    I'm very confused.
    Last edited by chaucer345; 07-05-2012 at 03:22 PM. Reason: comment symbols

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    no its not more significant sorry.. it just means 'put your stuff here' you will get used to that type of shorthand, it is not always neccesary to write out every detail for explanation, though i hear you.
    Your code bits are hard to read, they come out really narrow, throw some comment symbols at the top and bottom or something to exapnd the block a bit.

    You should try this as an example, not sure i would put the function call into the output statement myself though, not for my own functions anyway, its just a preference thing, I would rather spend another line of code perhaps and say ' x = ReturnValue(...); and then put x directly into the cout statement. : you can see from your own code how unwieldly it could become, imagine you wanted to output the breathedeep return value several times in a sentence? plus the other text and values , blah..

    Code:
    int FooTest(int n)
    {
        return n * 10;
    }
    int main()
    {
        int x = 5;
        cout << FooTest(x) << "\n";
    
        return 0;
    }
    Last edited by rogster001; 07-05-2012 at 03:36 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Roger that, comment symbols added to above posts.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Why are you trying to chain a call to a void function through the cout stream?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    When making a function call, you specify the function's name. You do not add its return type.
    The second problem you are facing is that your function is returning void (ie nothing). It is very hard to print out "nothing", so the compiler complains. Perhaps you were simply trying to call the function? In such case, you should get rid of the whole cout << bit to the left and right of the function call.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Perhaps you were simply trying to call the function? In such case, you should get rid of the whole cout << bit to the left and right of the function call.
    thats a good observation - were you looking to call it or output a return value? Plus like i say was a bit fiddly reading for me, so never saw your function declared void! take note of your other messages above, and good luck.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Quote Originally Posted by Elysia View Post
    When making a function call, you specify the function's name. You do not add its return type.
    The second problem you are facing is that your function is returning void (ie nothing). It is very hard to print out "nothing", so the compiler complains. Perhaps you were simply trying to call the function? In such case, you should get rid of the whole cout << bit to the left and right of the function call.
    You were right!
    Code:
     
    //
    //
    if (tutorialchoiceone == 1)
        {
            breathedeep (weapon, room, healthstat, spiritstat, strengthstat, dexteritystat); // I get two error messages on this line, one that says that the program expected a primary-expression before 'int' and another that says that it expected a ; before 'int'.
        }
    //
    //
    Let the code compile and it did what I wanted it to!

    Also, I have learned a new word! Chain calling... interesting...

    Thank you guys so much! This community is awesome!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator!= and primary-expression error : First time poster
    By kentrenholmpei in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2011, 07:24 AM
  2. Error: Expected primary expression before "else"
    By llind212 in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2011, 10:48 PM
  3. primary expression error when calling operation
    By elsparko in forum C++ Programming
    Replies: 2
    Last Post: 05-18-2010, 01:43 PM
  4. Replies: 24
    Last Post: 09-06-2006, 07:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21