• Lucid Dreaming - Dream Views




    Page 4 of 7 FirstFirst ... 2 3 4 5 6 ... LastLast
    Results 76 to 100 of 165
    1. #76
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      Quote Originally Posted by guitarboy View Post
      radius * radius= diameter
      plus, not times
      (\_ _/)
      (='.'=)
      (")_(")

    2. #77
      .. / .- –– / .- .-. guitarboy's Avatar
      Join Date
      Sep 2008
      LD Count
      Over 9000
      Gender
      Location
      Homeward Bound
      Posts
      1,571
      Likes
      49
      Quote Originally Posted by Ynot View Post
      plus, not times
      ohfail
      :O
      time to review 5th grade math

    3. #78
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Lolfail

      I had it in my program. I had defined PI to be 3 because I was lazy, I never did it exact because it was only for testing purposes. But yea, formula was: cirumfrence = PI * r * 2 and for radius I had r = circumference/(r*2).

      All I did was let the "user" input the circumference if they wanted radius, or input radius if they wanted circumference.

    4. #79
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      Quote Originally Posted by guitarboy View Post
      Also, you don't really need a window yet, do you.
      it would be nice to know how to do it.
      And color it.
      and change text color.
      etc.
      I understand why you want to get into GUI stuff, but trust me, at this stage it'd just confuse you

      You've got to get a proper handle (no pun intended) on the language before delving into external APIs

      At present, you're doing really simple maths with C++
      you've yet to grasp the more advanced concepts needed for GUI apps

      We haven't even covered stuff outside main() yet
      let alone classes, templates, memory allocation, STL, etc. etc.

      Walk, then run
      But right now, we're still bobing about in the sea, before we developed legs
      (\_ _/)
      (='.'=)
      (")_(")

    5. #80
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by Ynot View Post
      You've got to get a proper handle (no pun intended)
      I get it

      Now lets do methods, this program how two methods, one for area, and one for circumference, and the user gets to decide which to use.

      Code:
      #include <iostream>
      #define PI 3.14159
      
      float Circumference(float r){
         return r * 2 * PI;
      }
      
      float Area(float r){
         return r * r * PI;
      }
      
      int main(){
         //Get the radius of the circle from the user
         float radius;
         std::cout << "Enter radius." << std::endl;
         std::cin >> radius;
      
         //Get the function wanted
         std::cout << "\nPress 1 to Calculate Circumference.\nPress 2 to Calculate Area" << std::endl;
         int option;
         switch (option){  //kind of like a fast if / else
            case 1: //do if option == 1
               std::cout << Circumference(radius) << std::endl;
               break;
            case 2: //do if option == 2
               std::cout << Area(radius) << std::endl;
               break;
            default:  //neither of the other options
               std::cout << "Error, you didn't input one or two" << std::endl;
         }
         return 0;
      }

    6. #81
      .. / .- –– / .- .-. guitarboy's Avatar
      Join Date
      Sep 2008
      LD Count
      Over 9000
      Gender
      Location
      Homeward Bound
      Posts
      1,571
      Likes
      49
      Alright.
      Whhat should I learn next, then? Youtube lessons are not my friend.

    7. #82
      .. / .- –– / .- .-. guitarboy's Avatar
      Join Date
      Sep 2008
      LD Count
      Over 9000
      Gender
      Location
      Homeward Bound
      Posts
      1,571
      Likes
      49
      what is the std::--- for?
      Why not just cout, int etc.

    8. #83
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Hmm... how to describe how namespaces work That's not important right now (and an odd concept before learning classes,) you can use cout simply and add a using namespace std at the top of the program.


      Basically, there are multiple couts and the compiler has to know which one to use. std:: tells it to use the std one, you did that in your program by defining that the entire program uses std. Here's a little demonstration of namespaces.

      Code:
      #include <iostream>
      using namespace std;
      
      namespace Integer{
        int var = 5;
      }
      
      namespace Decimal{
        double var = 3.1416;
      }
      
      int main () {
        cout << Integer::var << endl;
        cout << Decimal::var << endl;
        return 0;
      }
      Weird, I know, but very useful for more advanced programming, for now you can forget what I just said
      Last edited by ninja9578; 01-11-2010 at 02:08 AM.

    9. #84
      .. / .- –– / .- .-. guitarboy's Avatar
      Join Date
      Sep 2008
      LD Count
      Over 9000
      Gender
      Location
      Homeward Bound
      Posts
      1,571
      Likes
      49
      Quote Originally Posted by ninja9578 View Post
      Hmm... how to describe how namespaces work That's not important right now (and an odd concept before learning classes,) you can use cout simply and add a using namespace std at the top of the program.


      Basically, there are multiple couts and the compiler has to know which one to use. std:: tells it to use the std one, you did that in your program by defining that the entire program uses std. Here's a little demonstration of namespaces.

      Code:
      #include <iostream>
      using namespace std;
      
      namespace Integer{
        int var = 5;
      }
      
      namespace Decimal{
        double var = 3.1416;
      }
      
      int main () {
        cout << Integer::var << endl;
        cout << Decimal::var << endl;
        return 0;
      }
      Weird, I know, but very useful for more advanced programming, for now you can forget what I just said
      You said something?
      I'll come back to it.

      Alright, a few questions-

      float Circumference(float r){
      return r * 2 * PI;
      }

      float Area(float r){
      return r * r * PI;

      What's that for?
      and why do you have to put \n

    10. #85
      .. / .- –– / .- .-. guitarboy's Avatar
      Join Date
      Sep 2008
      LD Count
      Over 9000
      Gender
      Location
      Homeward Bound
      Posts
      1,571
      Likes
      49
      Why isn't this working? It says I need } at the end, but I did...
      Code:
      #include <iostream>
      
      using namespace std;
      
      int main ()
      
      {
          int x = 3;
          int y = 4;
      
          if (x > y)
      
          {
              cout << "X is greater then Y " << endl;
          }
          else
              {cout << "X is less then Y " << endl;
      
          system("PAUSE");
          return 0;
      }

    11. #86
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by guitarboy View Post
      You said something?
      I'll come back to it.

      Alright, a few questions-

      float Circumference(float r){
      return r * 2 * PI;
      }

      float Area(float r){
      return r * r * PI;

      What's that for?
      and why do you have to put \n
      Those are methods. That's how you do things multiple times, you can keep calling that as many times as you want. Everything is done like this, even << in cout is kind of a method.

      \n is just a newline character, it's the same as endl, but can go inside of the quotes, I just did it to save space.

      Quote Originally Posted by guitarboy View Post
      Why isn't this working? It says I need } at the end, but I did...
      Code:
      #include <iostream>
      
      using namespace std;
      
      int main ()
      
      {
          int x = 3;
          int y = 4;
      
          if (x > y)
      
          {
              cout << "X is greater then Y " << endl;
          }
          else
              {cout << "X is less then Y " << endl;
      
          system("PAUSE");
          return 0;
      }
      You have a stray bracket before the last cout, you never close it.

    12. #87
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      Quote Originally Posted by ninja9578 View Post
      Hmm... how to describe how namespaces work That's not important right now
      Well, namspaces do use the same delimiting syntax as class methods, so I think an understanding is helpful

      Namespaces

      Code:
      #include <iostream>
      using namespace std;
      
      int main()
      {
          cout << "hello world" << endl;
          return 0;
      }
      Note the #include line,
      we're pulling in external code
      specifically, code to allow input / output of data using streams

      There's loads of external code you can pull into your program to acheive common tasks
      because of this, it's important to separate code into "namespaces"

      Say we are making a program that tracks finances of a company
      We could define a couple of classes in our program to track money incoming and outgoing from our company
      We may call these classes "cin" for cash in, and "cout" for cash out
      but we'd have a problem - these names clash with the names defined in iostream for input & output to the standard streams

      For this reason, any "standard" external code (Ie. comes with all compilers and is expected to be present) is separated off into it's own namespace, called "std"

      This brings us to the second line
      using namespace std;
      is asking the compiler, should it come across something not defined in our program's namespace, to look in the standard namespace for it

      You can omit the "using namespace std" line, and append the std namespace to object calls
      The above can be re-written to this
      Code:
      #include <iostream>
      
      /*
       * we don't need this line anymore
       * as we're using absolute referencing
       * for std objects
       */
      // using namespace std;
      
      int main()
      {
          std::cout << "hello world" << std::endl;
          return 0;
      }
      So, namespaces
      just a way to separate out definitions so code from different sources don't clash with each other
      (\_ _/)
      (='.'=)
      (")_(")

    13. #88
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Quote Originally Posted by ninja9578 View Post
      Now lets do methods, this program how two methods, one for area, and one for circumference, and the user gets to decide which to use.
      Those are functions. I thought you only used the term "methods" for member functions of classes.

      Quote Originally Posted by ninja9578 View Post
      \n is just a newline character, it's the same as endl, but can go inside of the quotes, I just did it to save space.
      Except that endl also flushes the stream
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    14. #89
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      Quote Originally Posted by khh View Post
      Except that endl also flushes the stream
      I'm pretty sure new line also ends up flushing the stream. This might be implementation-dependent though. Not sure what the actual spec says.

    15. #90
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      \n doesn't flush the stream
      \n is just a character, and therefore cannot "do" anything
      (\_ _/)
      (='.'=)
      (")_(")

    16. #91
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Ynot's right, \n does not flush the stream.

      This is why \n is useful for inside of a string, it allows you to keep buffering it instead of having to wait for the buffer to be flushed.

    17. #92
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      Well, SOMETHING is flushing the stream, because when I run:

      Code:
      #include <iostream>
      #include <unistd.h>
      using namespace std;
      
      int main(void)
      {
          cout << "Thingger1\n";
          sleep(1);
          cout << "Thingger2" << endl;
          return 0;
      }
      I get "Thingger1" displaying immediately, then it pauses for a second, and then Thingger2 displays. This has consistently been my experience for years and years. If it didn't flush the stream, it would have paused a second and then displayed both lines at the same time. So, what's happening here if not flushing? It might be the lower level stream operations that happen under the hood, but you can't deny this behaviour.

      EDIT to add: Did some googling, and it seems that this is dependent on the compiler. I suspect cout is special in that behaviour, and other streams (fstream, etc.) don't do that. Back in the C days, it was the same story with '\n' and printf.
      Last edited by Replicon; 01-12-2010 at 07:32 AM.

    18. #93
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      \n isn't flushing the stream, in most compilers, the stdout streams aren't buffered because there is boatloads of memory and speed to burn. But you're not guaranteed to get that result all the time. Try it with file i/o and see what you get

      Just remember there is a difference between what a compiler will do and what the C++ Standard states.

      Case and point:
      Code:
      if ((sizeof(char) < sizeof(short)) &&
                   (sizeof(short) < sizeof(int)) &&
                   (sizeof(int) < sizeof(long))){
         cout << "Expected" << endl;
      }  else {
         cout << "Reality on most compilers" << endl;
      }
      The C++ Standard states that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long), but it's perfectly legal for them to even be the same size.
      Last edited by ninja9578; 01-12-2010 at 02:33 PM.

    19. #94
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      I have a question.

      In this program, when you input two words, (i.e. first name and second name) it only takes the first word. I read somewhere in the tutorials of a data type that takes in any amount of words, rather than one. But I can't seem to find it. In fact, I'm not even sure if it was to do with data types or not.

      But anyways, yea, how would I allow for two words to be input?

      Code:
      #include <iostream>
      #include <sstream>
      using namespace std;
      
      int main()
      {
          string question = "What is your name? ";
          string greeting = "Hello, ";
          string yourname;
      
          cout << question << endl;
          cin >> yourname;
          cout << greeting << yourname << endl;
      
          system ("PAUSE");
          return 0;
      }

    20. #95
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      standard streams are whitespace delimited (unix convention)
      use getline() to get an entire line (up to newline char)

      Code:
      #include <iostream>
      #include <sstream>
      using namespace std;
      
      int main()
      {
          string question = "What is your name? ";
          string greeting = "Hello, ";
          string yourname;
      
          cout << question << endl;
          getline(cin, yourname);
          cout << greeting << yourname << endl;
      
          system ("PAUSE");
          return 0;
      }
      btw, what's this system("PAUSE") thing?
      (\_ _/)
      (='.'=)
      (")_(")

    21. #96
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Oohhhhhhhh... Thanks.

      system("PAUSE") means that when I run the program, it doesn't close as soon as it finishes. But it lets you close it by pressing any key.
      Within the programing software stuff it isn't an issue, but when it is the .exe on it's own, when you run it, it would close the moment you finish inputing stuff.

    22. #97
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Code::Blocks automatically pauses the execution at the end for you, you don't need that unless compiling or running outside of the IDE

    23. #98
      Gentlemen. Ladies. slayer's Avatar
      Join Date
      Mar 2007
      Gender
      Location
      Right here... Reputation: 9999
      Posts
      4,902
      Likes
      473
      DJ Entries
      4
      How to I package together my code into an exe or something both windows and macs can open?

    24. #99
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Quote Originally Posted by ninja9578 View Post
      Code::Blocks automatically pauses the execution at the end for you, you don't need that unless compiling or running outside of the IDE
      Yea, which is why I like to add the system ("PAUSE") at the end all the time. Not that it is important when I am just making stupid little things, but I just like to do it anyway.

    25. #100
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by slayer View Post
      How to I package together my code into an exe or something both windows and macs can open?
      A dos console and unix consoles are completely different. Macs can run windows console programs, you need to compile seperately for mac and windows

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

    Bookmarks

    Posting Permissions

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