Write a simple confession program in C++

Let's take a look at the effect achieved:

Input no and the system will always remind you to try again; input yes will display a color-changing heart and the words "I love you, what?". The test in the VC2010 learning version is as follows:

Running result :
insert image description here
If you enter no:
insert image description here
when you continue to enter no:
insert image description here
the system will keep asking for another choice until you select yes
when you enter yes:
insert image description here
insert image description here
insert image description here
the system will automatically clear the previous no option and a heart that will change color and love you will appear typeface.

Code:

Use the following code to realize that the user chooses no and has been looping in the while, knowing that no more input of no (what I wrote here is that as long as it is not no, other things are fine. When using it, you can optimize it and make sure it is no or yes. In the if It can be changed to if (answer[0] == 'n' && answer[1] == 'o') The following can also be changed similarly. I didn't do this because if I didn't press no for the first time and pressed other letters, no It will make the heart pop out. As shown in the picture below:
insert image description here

if ( !answer.empty()&& answer[0] != 'y')
  {
	cout<<"你拒绝了我,我好难受,5555"<<endl;
	do
	{
		cout<<"再选择一次吧(期待脸)"<<endl;
		cout<<"不要再拒绝我了"<<endl;
		cin>>answer2;
	}while(!answer2.empty()&& answer2[0]  != 'y');
  }

After the if, you need to use a C++ clear screen function, the header file <process.h>

system("cls"); //清屏

Then there is a small algorithm for drawing a heart with a for loop, and output love you at the bottom of the heart:

for (y = 1.5f; y > -1.5f; y -= 0.1f)
  {
    for (x = -1.5f; x < 1.5f; x += 0.05f)
    {
      z = x * x + y * y - 1;
      f = z * z * z - x * x * y * y * y;
	  cout<<(f <= 0.0f ? "*********"[(int)(f * -8.0f)] : ' ');
    }
     cout<<endl;
  }
cout<<"爱你,么么哒";

Cyclic color change program:

 for (;;)
  {
    system("color a");
    for (time = 0; time < 99999999; time++);
    system("color b");
    for (time = 0; time < 99999999; time++);
    system("color c");
    for (time = 0; time < 99999999; time++);
    system("color d");
    for (time = 0; time < 99999999; time++);
    system("color e");
    for (time = 0; time < 99999999; time++);
    system("color f");
    for (time = 0; time < 99999999; time++);
    system("color 0");
    for (time = 0; time < 99999999; time++);
    system("color 1");
    for (time = 0; time < 99999999; time++);
    system("color 2");
    for (time = 0; time < 99999999; time++);
    system("color 3");
    for (time = 0; time < 99999999; time++);
    system("color 4");
    for (time = 0; time < 99999999; time++);
    system("color 5");
    for (time = 0; time < 99999999; time++);
    system("color 6");
    for (time = 0; time < 99999999; time++);
    system("color 7");
    for (time = 0; time < 99999999; time++);
    system("color 8");
    for (time = 0; time < 99999999; time++);
    system("color 9");
  }

Finish.

emmmmm, this program is just a small program I wrote when I was learning C++, and the specific use needs to be improved by the user according to their own needs.

Complete source code download link: https://download.csdn.net/download/weixin_43737995/11604024

Guess you like

Origin blog.csdn.net/weixin_43737995/article/details/100039703