Results 1 to 23 of 23

Thread: C++ Help.

  1. #1
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default C++ Help.

    First off, i'm absolutely amazed and thankful at how similar Delphi/Pascal script is to C++, should make my learning transition much easier.
    First off, I want to know how to do a case statement in C++ -

    Code:
    //Calculator
    #include <iostream>  
    using namespace std; 
    int main()
    {
              //Defining variables
              char cMenu; //Menu?
              char cOperator; //Operator (+,-,*,/)
              int Num1; //X so to speak, first integer
              int Num2; //Y, second integer
              int y;  
              int i; 
              i = 2;
              y = 6;
              while(i = 2)
              { //this is basically a begin while the while statement is a n IF statement.
    
              cout<<"/ny First Calculator, G to calc, x to exit:"; //writeLn basically
              
              cin>>cMenu; //what this does is that it asks for an input, and stores it in cmenu
              
              if (cMenu == 104) //G I believe
                 { 
                        cout<<"enter a value an operator then a value pressing enter each time to use this";      
                 }         
              else if (cMenu == 120) //this is x I believe 
                   {
                        break; //kills the procedure 
                   } 
              else if (cMenu == 103)
                   {
                        cin>>Num1;
                        cin>>cOperator;
                        cin>>Num1;
                   }
              else 
                   {   
                        cout<<"neither of the options were selected, try again!"
                   }            }
                                                  
    }
    as you can see, it looks a bit messy so I want to do a Simba-esque way (case cOperator of). Any help is appreciated!

  2. #2
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

    Scroll down to the bottom of the page here for information about switch statements.

    Also, I think you missed an '=' in your while loop predicate.
    :-)

  3. #3
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Thanks! It's working now
    + rep.

  4. #4
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Also you have commented "kills the procedure" on break. Instead you should do:
    Code:
    return 0;
    There used to be something meaningful here.

  5. #5
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Oo, would that make a difference? Or is it just for efficiency?

  6. #6
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Okay guys, new question!
    How do I wait(500) in C++?
    I attempted sleep(500) but this comes up -
    38 G:\calculator.cpp `Sleep' undeclared (first use this function)

  7. #7
    Join Date
    Nov 2006
    Location
    Location, Location
    Posts
    1,126
    Mentioned
    6 Post(s)
    Quoted
    41 Post(s)

    Default

    Quote Originally Posted by Shay View Post
    Okay guys, new question!
    How do I wait(500) in C++?
    I attempted sleep(500) but this comes up -
    38 G:\calculator.cpp `Sleep' undeclared (first use this function)
    Check your includes.

  8. #8
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    It's case sensitive..

    #include <Windows.h>

    Sleep(500); //not sleep(500);
    I am Ggzz..
    Hackintosher

  9. #9
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    I included windows.h and it's working fine now

    My god, literally got ninja'd by you Brandon

  10. #10
    Join Date
    Jan 2012
    Posts
    273
    Mentioned
    7 Post(s)
    Quoted
    6 Post(s)

    Default

    Quote Originally Posted by Shay View Post
    First off, I want to know how to do a case statement in C++
    http://www-numi.fnal.gov/computing/d...ib/switch.html

    basically as they say - but you MUST use break statement else it will fall through

    Code:
    switch ( something ) {
    
      case 1: 
        // Process for something = 1
        ...
        break;
    
      case 'n':
        // process for 'n'
    
        // oops we forgot break... not good because:
      case 5 : 
        // Process for something = 5, or something = 'n' because we made a mistake and forgot to break
        ...
        break;
    
      default : 
        // Process for all other cases.
        ...
    
    }
    Perfect script? There is no such thing as "perfect", only "better than you expect".

  11. #11
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Yeh I learned that yesterday, I found that it was showing all values instead of just one.
    Also, can anyone tell me if itsnpossible to make a GUI type thing as the user interface?

  12. #12
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by Shay View Post
    Yeh I learned that yesterday, I found that it was showing all values instead of just one.
    Also, can anyone tell me if itsnpossible to make a GUI type thing as the user interface?
    For GUI, I'd recommend Qt. Qt also simplifies a lot of other stuff. But first learn to code properly. Before you go making yourself a GUI, learn the basics and most of the more advanced stuff.
    There used to be something meaningful here.

  13. #13
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Ah okay, what do you suggest I do next?

    C++ Code:
    //Calculator
    #include <iostream>
    #include <windows.h>  
    using namespace std;
    int main()
    {
              //Defining variables
              char cMenu; //Menu?
              char cOperator; //Operator (+,-,*,/)
              int Num1; //X so to speak, first integer
              int Num2; //Y, second integer
              int y;  
              int i;
              i = 2;
              y = 6;
              while(i = 2)
              { //this is basically a begin while the while statement is a n IF statement.

              cout<<"First Calculator, g to calc, x to exit, h for help:"; //writeLn basically
             
              cin>>cMenu; //what this does is that it asks for an input, and stores it in cmenu
             
              if (cMenu == 104) //G I believe
                 {
                        cout<<"enter a value an operator then a value pressing enter each time to use this";      
                 }        
              else if (cMenu == 120) //this is x I believe
                   {
                        return 0; //kills the procedure
                   }
              else if (cMenu == 103)
                   {
                        cout<<"Enter first number: "<<endl;
                        cin>>Num1;
                        cout<<"Enter second number: "<<endl;
                        cin>>Num2;
                        cout<<"What action?"<<endl;
                        cin>>cOperator;
                        switch(cOperator) //case statement
                        {
                         case 43:
                              int ans; //declares "ans" as an integer;
                              ans = Num1 + Num2;
                              cout<<"If adding:"<<ans<<endl; //endL ends the line.
                              break;
                         case 45:
                              ans = Num1 - Num2;
                              cout<<"If subtracting: "<<ans<<endl;
                              break;
                         case 42:
                              ans = Num1 * Num2;
                              cout<<"If multiplying: "<<ans<<endl;
                              break;
                         case 47:
                              ans = Num1 / Num2;
                              cout<<"If dividing: "<<ans<<endl;
                              break;
                              }
                             
                   }
              else
                   {  
                        cout<<"neither of the options were selected, try again!"<<endl;
                   }            
              }
                                                 
    }

  14. #14
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Please use the correct code tags for readability. [.Highlight=c++] <- without .

    c++ Code:
    //Calculator
    #include <iostream>
    #include <windows.h>  
    using namespace std;
    int main()
    {
              //Defining variables
              char cMenu; //Menu?
              char cOperator; //Operator (+,-,*,/)
              int Num1; //X so to speak, first integer
              int Num2; //Y, second integer
              int y;  
              int i;
              i = 2;
              y = 6;
              while(i = 2)
              { //this is basically a begin while the while statement is a n IF statement.

              cout<<"First Calculator, g to calc, x to exit, h for help:"; //writeLn basically
             
              cin>>cMenu; //what this does is that it asks for an input, and stores it in cmenu
             
              if (cMenu == 104) //G I believe
                 {
                        cout<<"enter a value an operator then a value pressing enter each time to use this";      
                 }        
              else if (cMenu == 120) //this is x I believe
                   {
                        return 0; //kills the procedure
                   }
              else if (cMenu == 103)
                   {
                        cout<<"Enter first number: "<<endl;
                        cin>>Num1;
                        cout<<"Enter second number: "<<endl;
                        cin>>Num2;
                        cout<<"What action?"<<endl;
                        cin>>cOperator;
                        switch(cOperator) //case statement
                        {
                         case 43:
                              int ans; //declares "ans" as an integer;
                              ans = Num1 + Num2;
                              cout<<"If adding:"<<ans<<endl; //endL ends the line.
                              break;
                         case 45:
                              ans = Num1 - Num2;
                              cout<<"If subtracting: "<<ans<<endl;
                              break;
                         case 42:
                              ans = Num1 * Num2;
                              cout<<"If multiplying: "<<ans<<endl;
                              break;
                         case 47:
                              ans = Num1 / Num2;
                              cout<<"If dividing: "<<ans<<endl;
                              break;
                              }
                             
                   }
              else
                   {  
                        cout<<"neither of the options were selected, try again!"<<endl;
                   }            
              }
                                                 
    }
    Working on: Tithe Farmer

  15. #15
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    Please use the correct code tags for readability. [.Highlight=c++] <- without .

    c++ Code:
    //Calculator
    #include <iostream>
    #include <windows.h>  
    using namespace std;
    int main()
    {
              //Defining variables
              char cMenu; //Menu?
              char cOperator; //Operator (+,-,*,/)
              int Num1; //X so to speak, first integer
              int Num2; //Y, second integer
              int y;  
              int i;
              i = 2;
              y = 6;
              while(i = 2)
              { //this is basically a begin while the while statement is a n IF statement.

              cout<<"First Calculator, g to calc, x to exit, h for help:"; //writeLn basically
             
              cin>>cMenu; //what this does is that it asks for an input, and stores it in cmenu
             
              if (cMenu == 104) //G I believe
                 {
                        cout<<"enter a value an operator then a value pressing enter each time to use this";      
                 }        
              else if (cMenu == 120) //this is x I believe
                   {
                        return 0; //kills the procedure
                   }
              else if (cMenu == 103)
                   {
                        cout<<"Enter first number: "<<endl;
                        cin>>Num1;
                        cout<<"Enter second number: "<<endl;
                        cin>>Num2;
                        cout<<"What action?"<<endl;
                        cin>>cOperator;
                        switch(cOperator) //case statement
                        {
                         case 43:
                              int ans; //declares "ans" as an integer;
                              ans = Num1 + Num2;
                              cout<<"If adding:"<<ans<<endl; //endL ends the line.
                              break;
                         case 45:
                              ans = Num1 - Num2;
                              cout<<"If subtracting: "<<ans<<endl;
                              break;
                         case 42:
                              ans = Num1 * Num2;
                              cout<<"If multiplying: "<<ans<<endl;
                              break;
                         case 47:
                              ans = Num1 / Num2;
                              cout<<"If dividing: "<<ans<<endl;
                              break;
                              }
                             
                   }
              else
                   {  
                        cout<<"neither of the options were selected, try again!"<<endl;
                   }            
              }
                                                 
    }
    Well my bad -.- I didn't know that existed..

  16. #16
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by Shay View Post
    Well my bad -.- I didn't know that existed..
    Nobody does :P They should make a button for it or something I forgive you.
    Working on: Tithe Farmer

  17. #17
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    Nobody does :P They should make a button for it or something I forgive you.
    Ty <3

    Anyways, any suggestions about what I can make in C++ while on my spare period?

  18. #18
    Join Date
    Sep 2007
    Location
    British Columbia, Canada
    Posts
    4,047
    Mentioned
    1 Post(s)
    Quoted
    2 Post(s)

    Default

    learn oop and you'll be set for uni.

    Also how old are you sin?

    You got a facebook? Cause am also from ontario (toronto).
    Oh Hai Dar

  19. #19
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    I always like to make a jump in the deep. Try to look into directX 3D, I learned c++ with that :P, but most people will not recommend it, no idea why.

    You will soon notice it isn't just that pascal and c++ are similar, but that java, pascal, c++, c, c#, actionscript, python and every other language works very similar. At a certain moment learning a new language is a matter of days instead of weeks/months.
    Working on: Tithe Farmer

  20. #20
    Join Date
    Dec 2011
    Location
    Toronto, Ontario
    Posts
    6,424
    Mentioned
    84 Post(s)
    Quoted
    863 Post(s)

    Default

    Quote Originally Posted by Main View Post
    learn oop and you'll be set for uni.

    Also how old are you sin?

    You got a facebook? Cause am also from ontario (toronto).
    Im 15, and nah. I deactivated mine because I was getting addicted to it
    And can you explain about OOP?

    Quote Originally Posted by masterBB View Post
    I always like to make a jump in the deep. Try to look into directX 3D, I learned c++ with that :P, but most people will not recommend it, no idea why.

    You will soon notice it isn't just that pascal and c++ are similar, but that java, pascal, c++, c, c#, actionscript, python and every other language works very similar. At a certain moment learning a new language is a matter of days instead of weeks/months.
    This is definetly true. I was used to Java so I picked up Pascal in only a few days.
    Also, what is D3D programming used for?

  21. #21
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Default

    I think he meant to write loop instead of oop
    And he's right cause in uni, they drilled us hard on those for loops and while loops. Plus also learn how to do a loop within a loop

    EDIT: surfing forums on andriod ain't so bad
    Last edited by Recursive; 05-25-2012 at 02:49 PM.

  22. #22
    Join Date
    Sep 2007
    Location
    British Columbia, Canada
    Posts
    4,047
    Mentioned
    1 Post(s)
    Quoted
    2 Post(s)

    Default

    oop is object orientated programming.

    Loops and stuff are quite easy if you have good logic from previous programming experience (such as simba hehe). Object orientate programming isn't too useful in terms of making a bot but its something you need for university maybe even beyond. So its a good to know
    Oh Hai Dar

  23. #23
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by Chig View Post
    I think he meant to write loop instead of oop
    And he's right cause in uni, they drilled us hard on those for loops and while loops. Plus also learn how to do a loop within a loop

    EDIT: surfing forums on andriod ain't so bad
    No, he meant oop. Not loop. Loops are easy and not important.

    Objective oriented programming.

    Let's say you got a wall. The wall exists out of bricks. Every brick is from the brick class.

    Pseudocode:
    actionscript Code:
    class Brick{
      var width, height:Int;
      var x, y:Int;


      constructor(_x, _y, _w, _h:Int){
        this.create();
        x = _x;
        y = _y;
        width = _w;
        height = _h;
      }

      Draw(canvas:Canvas){
        canvas.drawThis(x, y, width, height);
      }
    }

    Pseudocode:
    actionscript Code:
    class Wall{
      var bricks:Array;


      Create(){
        var i:Int = 0;
        var j:Int = 0;

        for(j = 0; j < 10; j++) {
          for(i = 0; i < 10; i++) {
            bricks.push(new Brick(5*j,10*i,5,10));
          }
        }
      }

      Draw(canvas:Canvas){
        for(j = 0; j < bricks.length; j++) {
          bricks[j].draw(this);
        }
      }
    }

    edit:
    If you want to start with directX look into this: http://drunkenhyena.com/cgi-bin/directx.pl
    Last edited by masterBB; 05-25-2012 at 03:18 PM.
    Working on: Tithe Farmer

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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