C++ Help

Discussion in 'Mac Programming' started by sixstorm, Oct 3, 2006.

  1. sixstorm macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #1
    Hello all experienced more than me people! :D I have a very urgent favor to ask of you all. I have an intro to C++ class online and I am doing a project, which is very confusing. It is about functions and for some reason, the class program or XCode doesn't want to seem to wanna build some obvious things. For example, here is a very very basic version of my program (without anything related to functions at all, I like to build from the ground up :D):

    I get a build error on the first "cin >>" line!!! WTF? It's never done this before and I have no clue what it is. But like I always say, two sets of eyes are better than one. Obviously, I'm missing something . . . Any suggestions? And please, I deserve to be called a n00b for this one prolly so feel free.
     
  2. uaaerospace macrumors 6502

    Joined:
    Feb 15, 2005
    Location:
    Alabama
    #2
    Try removing the ">> endl" from the cin statement.

    Also, keep program scope in mind when writing if statements. The proper structure for an if statement is

    if (argument)
    {
    statement;
    statement;
    }

    You have to use the {}'s unless you just have one statement. In your code, you have 3 statements, so the {}'s are needed.


    Also...there is no if-else statement in c++. Use another if statement.

    haha...Also. Keep in mind that coding in c++ is usually if not always case sensitive. You may wish to check your use of subtotal vs. subTotal. You are using a good convention (camelCase), just stick with it.
     
  3. deputy_doofy macrumors 65816

    deputy_doofy

    Joined:
    Sep 11, 2002
    #3
    Yes. That's exactly it.
    endl means ENDLINE, (which basically puts the next output on the next line) and you would only use that for output.
     
  4. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #4
    Thanks for the help, I can't believe I didn't see that. What do you mean there is no if-else statement in C++? Don't think I'm calling you a liar, but I've been using If-Else statements for weeks now . . . unless I am reading your statement wrong. Geez, what a day lol.
     
  5. deputy_doofy macrumors 65816

    deputy_doofy

    Joined:
    Sep 11, 2002
    #5
    There IS an if (then) statement. Well, there's no THEN...
    if (comparison)
    {
    // Do stuff and things.
    // Do more stuff and things.
    }
    else
    {
    // Do completely different stuff and things.
    // Do MORE completely different stuff and things.
    }
     
  6. uaaerospace macrumors 6502

    Joined:
    Feb 15, 2005
    Location:
    Alabama
    #6
    hmm, well, it compiles fine with the if/else command in xCode, however the text I have on c++ mentions no such command. Also, when taking c++ classes, we always used if statements. Perhaps it's just personal preference of my professors. Oh well, I learn something new every day....this time from a self proclaimed "noob" :p :D
     
  7. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #7
    The way you have it, the statement discountAmt = totalOne / bDiscount; is executed for the if. The semicolon doesn't belong on the statement with the if.

    The other statements, had it compiled, except for the else would be executed without condition.

    When confused, use the braces {} to explicitly declare the block.
     
  8. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #8
    Thanks a lot for the help. I played and tinkered for a little bit and finally got it to work. I do have to take out a lot of "//" in my program (there's a lot more to the program lol) and add my function. I'm sure I'll be back here soon with more Q's. Thanks again everybody.
     
  9. Heath macrumors regular

    Joined:
    Aug 19, 2005
    Location:
    Canada
    #9
    Another thing.

    You might want to revisit that if statement again.

    You had:
    if (breakfastMeals >= quanBreak);

    This will not execute any conditional logic because of the semicolon at the end.
    It will process the if statement, then merrily execute code that immediately follows, regardless of the outcome of that comparison.

    (At least this was the case the last time I touched C++, ymmv).
     
  10. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #10
    Here we go again. I'm trying to use a function now . . . here's the full code:

    It's giving me errors on subTotal and totalOne with "no type" (whatever that means) and a few other things. Any ideas?
     
  11. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #11
    Watch where you put your semicolons.

    Note the difference between your definition of main() and your definition of calcDiscount().
     
  12. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #12
    I've got both definitions in {}s, so that shouldn't be a problem. BTW, I'm using a remote terminal (via OSX terminal, which is a PITA) and it's giving me different errors than XCode is. XCode is a bit easier to work with.
     
  13. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #13
    Let me put it plainly: you have a semicolon between the closing parenthesis and the opening brace.
     
  14. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #14
    If I'm not being a PITA for ya, could you explain why I wouldn't put it there? Just trying to learn . . .
     
  15. Heath macrumors regular

    Joined:
    Aug 19, 2005
    Location:
    Canada
    #15
    Better yet,

    Try to explain why you WOULD put it there.

    double calcDiscount (int quantity, double eachCost, double discountPerCent, int orderMoreThan);

    The semicolon is usually an indication to the compiler that you're done whatever it is you are going to do in that statement.
    So you're telling the compiler, 'Hey I'm done defining my function!' and it goes 'alright.. an empty function.. gotcha.. what's the rest of the code for then?' and proceeds to spew errors at you that make no sense :)
     
  16. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #16
    Okee dokee, that makes sense. Now tell me this. Take a look at my identifiers, my function call and my function definition header. Why am I still getting an errors about "assignment 'int' to 'double'"? If in a function call, it makes the temp memory cells from left to right, it shouldn't have a problem defining those temp memory cells for this.

    EDIT: Guess I should give you some code huh?

    int quanBreak = 10;
    int breakfastMeals;
    double bDiscount = .10;
    double breakfast = 5.50;

    double calcDiscount (int quantity, double costEach, double discountPerCent, int orderMoreThan)
     
  17. Heath macrumors regular

    Joined:
    Aug 19, 2005
    Location:
    Canada
    #17
    It looks like you're trying to stuff a double into an int, not the other way around.


    double calcDiscount (int quantity, double costEach, double discountPerCent, int orderMoreThan);

    double quanBreak = 10; <-- this won't fit into an int even though it looks like it should.

    breakfastTotal = calcDiscount (breakfastMeals, breakfast, bDiscount, quanBreak);

    You are correct in that the ints can be implicitly converted to doubles, but the last argument you are passing in a double into an int, and the compiler will complain about that one since it can result in the loss of information.
     
  18. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #18
    I changed the "double quanBreak" into "int quanBreak" (that's what it's supposed to be, but I'm still getting errors on my function call. Why won't 10 go into an int like that?
     
  19. ifjake macrumors 6502a

    Joined:
    Jan 19, 2004
    #19
    I'm also in a beginners C++ course and am looking at some of your stuff and comparing it to how I'm being taught.

    I'm actually interested in your #include statements and such.

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


    We use <iostream.h> which means that the std::cout stuff and
    using namespace std; aren't needed. But I feel like we're using outdated stuff. If this is all it takes to move to newer stuff I might do that on my own. But is <iomanip> needed to state using namespace std;?

    Are there any similar steps that need to be taken when moving from <fstream.h> to <fstream>?
     
  20. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #20
    You're right. You're using C++ prior to the ISO and ANSI standards.
     
  21. ifjake macrumors 6502a

    Joined:
    Jan 19, 2004
    #21
    What do I need to do to get stuff to work right with these new standards?
     
  22. YoNeX macrumors regular

    YoNeX

    Joined:
    Apr 29, 2005
    #22
    Ahh yes memories of the introduction C++ class. My criticism is why do you need the iomanip and the cmath libs? From the code, I don't see any any purpose for these libs from the code given. Another pointer I can give you is try to use abbreviations for your function names. You are prone to make less mistakes if you use abreviations. For example instead of entering in quantity, use qty. Instead of breakfast, you can use bfast. These small little thing will add up, plus you avoid line wrapping (or having long horizontal code). Oh yeah, forgot, don't need to take my advice, many people have different style of coding. Use what you like (or use based on the school's coding style).
     
  23. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #23
    Anyone know why I'm getting a "int from double" error, even though my code is going "int to int"?
     
  24. Heath macrumors regular

    Joined:
    Aug 19, 2005
    Location:
    Canada
    #24
    What line is giving you the issue?
     
  25. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #25
    My function call.
     

Share This Page