Jump to content

Simple C++ code examples.


Heretic121

Recommended Posts

  • Leader

I am not familiar with C or C++, but I can only see loops in the code-sets that used the while statements.

Link to comment
Share on other sites

Okay here I was going for the loop, but after entering either y or n it closes the program. Hmmph!

#include <iostream>

int main()
{

    float Num1;
    float Num2;
    float Answer;

    int which_calculation;

    char again = 'y';

    { // here
        std::cout << "Press 1 to add.\n";
        std::cin >> which_calculation;

        std::cout << "Please enter your first number.\n";
        std::cin >> Num1;
        std::cout << "Please enter your second number.\n";
        std::cin >> Num2;
    } // Here
        if (which_calculation == 1)
        {
            Answer = Num1 + Num2;
        }

    std::cout << "The answer is ...\n";
    std::cout << Answer << std::endl;

    if (again == 'y')
    {
        std::cout << "again (y/n)?";
        std::cin >> again;
    }
}




 

 

The lines I'm talking about have "// here" after them.

 

I just noticed... where's your "return 0;" line? :D

Edited by Heretic121
Link to comment
Share on other sites


#include <iostream>



int main()

{

  int input = 0;

  int iterator = -1;



  do

  {

    iterator++;

    std::cout << "Enter a number that's not " << iterator << " please";

    std::cout << std::endl;

    std::cin >> input;

    if (iterator == 10)

    {

      std::cout << std::endl;

      std::cout << "Wow, you're more patient then I am, you win.";

      return 0;

    }

  }while(input != iterator);

  std::cout << std::endl;

  std::cout << "Hey! you weren't supposed to enter " << iterator << "!";

  return 0;

}



 

Link to comment
Share on other sites

  • 3 weeks later...

#include <iostream>

using namespace std;

class Rectangle{
public:
int getArea();
Rectangle(int, int);
private:
int height;
int length;
};

Rectangle::Rectangle(int length, int height){
this->length = length;
this->height = height;
}

int Rectangle::getArea(void){
return length * height;
}



int main(int argc, char* argv[]){
int length;
cout << "Enter length: " << endl;
cin >> length;

int height;
cout << "Enter height: " << endl;
cin >> height;

Rectangle rectangle(length, height);
cout << "The area of the rectangle is: " << rectangle.getArea() << endl;
return 0;
}
Link to comment
Share on other sites


#include <iostream>

using namespace std;

class A{
public:
string displayMsg(void);
};

class B: public A{
};

string A::displayMsg(void){
return "A message from class A";
}

int main(int argc, char* argv[]){
B b;
cout << b.displayMsg() << endl;
return 0;
}
Link to comment
Share on other sites

One of the most interesting things about OOP is Polymorphism. It allows an object to take on many forms. It's similar to the example above, involving class inheritance. Both C++ and Java implement this technique well. Tell me if you like to know more. I'll post some code.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.