If statements (câu lệnh if)

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int v1 = rand() % 100 + 1, ip;

	cout << "Rand number has been ready, guess it, if it's smaller or equal 50, press 0, the opposite, press 1";
	cin >> ip;

	if (v1 <= 50 && ip == 0);
	{
		cout << " Congratulations !!!" << endl;
	}
	else if (v1 > 50 && ip == 1);
	{
		cout << " Congratulations !!!" << endl;
	}
	else 
		cout << "DOG !" << endl;
	
	system("pause");
	return 0;
}

chương trình báo là lỗi ở dòng 17 và dòng 21, nó nói expected statement, mọi người giúp em với !

Ở cuối dòng 17 và dòng 21 bạn viết thừa dấu chấm phẩy. Bạn đọc lại tài liệu để biết cú pháp của câu lệnh if nhé.

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
	srand(time(NULL));
	int A = rand()%100 + 1;
	int n;
	cout << "Tren man hinh se xuat hien ngau nhien mot so tu 1 den 100. Ban hay doan:\n";
	cout << "\t + Neu ban doan so do lon hon 50 thi bam so (1)\n" << "\t + Neu ban don so do be hon hoac bang 50 thi bam so (0)\n";
	cout << "\t", cin >> n;
	if (n != 0 && n != 1)
	{
		cout << "\t Ban nhap so khong hop le!\n";
	}
	else if ((n ==1 && A>50)|| (n == 0 && A <= 50))
	{
		cout <<"\t"<<A<< endl<<"\t Ban da doan dung.\n";
	}
	else
	{
		cout <<"\t"<<A<<endl<< "\t Ban da doan sai.\n";
	}
	system("pause");
	return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
	srand(time(NULL));
	int a = rand() % 101;
	int b;
	cout << "We have a random number from 0 to 100 ...\nLet's guess number ..." << endl;
	cout << "Enter 1 if you think the number is at least 50 , 0 if smaller than 50" << endl; 
    cin >> b ;
	if (b != 0 && b != 1)
		cout << "You enter wrong input, please try again !! " << endl;
	else {
		cout << "Random number is " << a << endl;

		if (a >= 50) {
			if (b == 1)
				cout << "So your right " << endl;
			else
				cout << "So your wrong" << endl;
		}
		else {
			if (b == 0)
				cout << "So your right" << endl;
			else
				cout << "So your wrong" << endl;
		}
	}
	system("pause ");
	return 0;
}

My first comment :smile:

Em viết như thế này. Nhưng delta nó chỉ nhận tới dòng 9, còn lại thì nó không nhận giá trị delta nữa, mà nó cho delta=0. Như vật thì em bị sai bước nào ạ?

So sánh là == cơ :slight_smile: dấu = lúc nào cũng là gán.

1 Like

Em cảm ơn nha. Bài chạy đúng rồi ạ

CODE của mình ,chờ góp ý của các bạn 

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void main()
{
srand(time(NULL));
int random = rand() % 101
int number;
int i;
cout << "\t\tTHe RAndom NUmber GAMe\n\n";
do
{
	cout << "\n\nGuess: ";
	cin >> number;
} while (number != 1 && number != 0 && cout<<"Erorr ( Enter 1 or 0 )\n");
i = (number == 0 && random < 50) ? 1: 0 || (number == 1 && random > 50) ? 1: 0;
cout <<i<<"\n"<<"\nThe number random is: "<< random <<"\n( 1: Right / 0: Wrong)\n"<<endl;
system("pause");
}
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
	srand(time(NULL));
	int r = rand() % 100 + 1;
	double a;
	
	cout << "Nhap mot so co dinh de so sanh voi mot so ngau nhien chay tu 0 -> 100.\nVoi so co dinh A = ";
	cin >> a;
	cout << "So ngau nhien la: " << r << endl;
	
	if(a>r)
	{
		cout << "A > " << r;
	}
	else if(a==r)
	{
		cout << "A = " << r;
	}
	else
	{
		cout << "A < " << r;
	}
	
	return 0;
	
}
#include <iostream>
#include <cmath>
using namespace std;

float delta(float a,float b , float c )
{ float d;
 d = b*b - 4*a*c;
 return d;
 
}
int main ()
{
cout<<" giai phuong trinh bac 2 cua phuong trinh ax^2 + bx +c =0"<<endl;
int a;
cout<<"nhap a:";
cin>>a;
cout<<endl;
int b;
cout<<"nhap b:";
cin>>b;
cout<<endl;
int c;
cout<<"nhap c:";
cin>>c;
float delta1;
delta1=delta(a,b,c);
if (delta1 > 0){ float x1;
                float x2;
x1=(-b+sqrt(delta1))/(2*a);
x2=(-b-sqrt(delta1))/(2*a);

cout<<"phuong trinh co 2 nghiem phan biet"<<endl<<"x1="<<x1<<" "<<"x2="<<x2;
              }
else if (delta1=0){
float x;
x=-b/(2*a);
  
cout<<"phuong trinh co nghiem kep:"<<x;   }        
                  

else 
cout<<"phuong trinh vo nghiem"	;
return 0;
			  
}

Mình có viết code cho bài giải pt bậc 2 nhưng khi chạy nó không thực hiện các lệnh if :neutral_face:
Kiểu như là từ dòng nhập số a,b,c và hiện lên chương trình xong thì chỉ cho ra “Press any key to continue”
Mình không biết là mình sai chỗ nào nữa. Xin mọi người góp ý kiến!

#include <iostream> //iostream
#include <cmath> //cmath
#include <cstdlib> //cstdlib
using namespace std;

int main()
{
    float a,b,c;
    cout<<"Nhap so a: ";
    cin >> a;
    cout<<"Nhap so b: ";
    cin >> b;
    cout<<"Nhap so c: ";
    cin >> c;
    cout<< "\nPhuong trinh cua ban la: " << a <<"x^2 + "<< b <<"x + "<< c <<" = 0 ?" <<endl;
    system("pause");
    if (a!=0 && b!=0 ) //Mình nghĩ vấn đề là ở chỗ này, nó không thực hiện lệnh để in ra màn hình
    {
        float Denta = (sqrt(pow(b,2) - 4 * a * c));
        if (Denta > 0)
        {
            cout<< "Nghiem cua phuong trinh la: " << endl;
            cout<< "x1= " << (-b + Denta) / (2*a);
            cout<< "x2= " << (-b - Denta) / (2*a);
        }
        else
        {
            if (Denta == 0)
            {
                cout<< "Nghiem cua phuong trinh la x1=x2= "<< -(b)/(2*a) <<endl;
            }
            if (Denta < 0)
            {
                cout<< "Phuong trinh vo nghiem!"<<endl;
            }
        }
    }

    else
    {
        if (a==0 && b!=0)
        {
            cout<< "Nghiem cua phuong trinh la x= "<< -(c)/b <<endl;
        }
        if (a!=0 && b==0)
        {
            cout<< "Nghiem cua phuong trinh la: " << endl;
            cout<< "x1= " << sqrt((-c)/a);
            cout<< "x2= " << sqrt((-c)/a);
        }
    }
    system("pause");
    return 0;
}
Nhap so a: 4
Nhap so b: 2
Nhap so c: 3

Phuong trinh cua ban la: 4x^2 + 2x + 3 = 0 ?
Press any key to continue . . .
Press any key to continue . . .

Process return 0 (0x0)  execution time : 3.730 s
Press any key to continue.
-

Here’s my code for test 1 <3

#include <iostream> // iostream
#include <cstdlib> // cstdlib

using namespace std;

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int Number = rand() %100 + 0;
    bool Guess;
    cout <<"Please input your number: "; cin >> Guess;
    if (Number < 50)
    {
        if (Guess == true )
            cout<< "That is incorrect guest! The generating number is " << Number << " (" << Number <<"<50)" <<endl;
        else
            cout<< "That is the correct guest! The generating number is " << Number;
    }
    else
    {
        if (Guess == true)
            cout<< "That is the correct guest! The generating number is " <<Number;
        else
            cout<< "That is incorrect guest! The generating number is " << Number << " (" << Number <<">50)" <<endl;
    }
system("pause");
return(0);
}
1 Like
#include <iostream>;
#include <cstdlib>;
#include <ctime>;
#include <stdio.h>;
using namespace std;

int main()
{
	srand( time (0));
	float c1 = rand()% (100);
	float guess1 ;
//	cout<< "guess ( chi nhap 1 or 0 ( 1 la > 50 , 0 la < 50 ) = ";
//	cin >> guess1;
	printf ( "guess ( chi nhap 1 or 0 ( 1 la > 50 , 0 la < 50 ) = ");
	scanf_s ("%f", &guess1);
	if (guess1 != 0 && guess1 != 1)
		cout << "byebye - nhap tam bay , khong doc ky yeu cau "<< endl;
	if (guess1 == 0 or guess1 == 1)
	{
		if (guess1 == 0 && c1 < 50)
			cout << "ban da doan dung , so ngau nhien la : " << float(c1) << " nho hon 50" << endl;
		else if ( guess1 ==0 && c1 > 50)
			cout << "ban da doan sai , so ngau nhien la : " << float(c1) << " lon hon 50" << endl;
		else if (guess1 ==1  && c1 < 50)
			cout << "ban da doan sai , so ngau nhien la : " << float(c1) << " nho hon 50" << endl;
		else if (guess1 == 1 && c1 > 50)
			cout << "ban da doan dung , so ngau nhien la : " << float(c1) << " lon hon 50" << endl;
		else if ( c1 == 50)
			cout << " so ngau nhien la : " << c1 << " xin nhap lai " << endl;
	}
	system("pause");
	return 0;
} 

dùng cout , cin thì nhập số thập phân , dấu /.,;’\ … thì hiểu là bằng 0 , nên mình dung prinf , scanf

các bác cho mình hỏi ctime là thư viện gì thế

Thư viện của Google: ctime library

3 Likes

17 posts were split to a new topic: Vấn đề về tạo số ngẫu nhiên

Em xin hỏi. Em viết cái mã code vầy nè :

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
	{
		//BT1:
		srand(time(NULL));
		int y = rand() % (100 - 0 + 1) + 0;
		bool x;
		cout << "So ngau nhien lon hon 50 hay be hon. Chon 1-Lon hon _ 0-Be hon." << endl;
		cin >> x;
		if (x == true && y > 50)
		{
			cout << "Dung roi day" << endl;
		}
		else if (x == 0 && y > 50)
		{
			cout << "Sai roi. Doan lai xem" << endl;
		}
		else if ((x == 0 || x == 1) && y == 50)
		{
			cout << "So bang 50. Khong dung cung khong sai. Doan lai di" << endl;
		}
		else if (x == true && y < 50)
		{
			cout << "Sai roi. Doan lai xem" << endl;
		}
		else
		{
			(x == 0 && y < 50);
			cout << "Dung roi day" << endl;
		}
		cout << "So random : " << y << endl;

	}
	{
		//BT2:
		int a, b, c;
		float Delta, x1, x2;
		cout << "Nhap lan luot a-b-c :" << endl;
		cin >> a >> b >> c;
		Delta = b * b - 4 * a * c;
		if (Delta < 0)
		{
			cout << "Phuong trinh vo nghiem. \n";
		}
		else if (Delta == 0)
		{
			x1 = (-b) / (2 * a);
			cout << "x = " << x1 << endl;
		}
		else if (Delta > 0)
		{
			x1 = (-b + sqrt(Delta)) / (2 * a);
			x2 = (-b - sqrt(Delta)) / (2 * a);
			cout << "x1 = " << x1 << ", " << "x2 = " << x2 << endl;
		}
	}
	system("pause");
	return 0;
}

Khi em ấn số khác 0 và 1 ở bài tập 1 thì ở bài tập 2 nó không cho em nhập số trong lệnh cin mà nó lại chạy luôn “Phương trình vô nghiệm”. Thậm trí em có để if (x == true && y > 50) , chữ “true” thành “1” thì nó vẫn vậy. Dù nó vẫn hiểu đúng là true. Nhưng BT2 nó lỗi. Em có dùng khối lệnh rồi đấy. Và em dùng VS 2019.
Xin cám ơn.

Bạn nên dùng kiểu int thay cho bool, nếu dữ liệu nhập sai thì làm những dữ liệu phía sau sai hàng loạt.

#include <iostream>

int main()
{
    bool x;
    int n;
    std::cin >> x;
    std::cout << "bool: " << x << std::endl;
    std::cin >> x;
    std::cout << x << std::endl;
    std::cin >> x;
    std::cout << x << std::endl;
    
    std::cin >> n;
    std::cout << "int: " << n << std::endl;
    return 0;
}

Như đoạn phía trên. Chỉ cần nhập 1 số khác 0 hoặc 1 hoặc kí tự chữ cái thì toàn bộ đoạn nhập (boolint) phía sau sẽ nhận sai dữ liệu.
Vd:
Nhập:

5

Xuất:

bool: 1
1
1
int: 0
3 Likes

bác cho e hỏi sao k cần gán giá trị của n= 1 hoặc 0 mà chương trình vẫn chạy đúng thế ạ

Chắc ý bạn là:

không cần so sánh n bằng 1 hoặc n bằng 0.

Vì bạn ấy lợi dụng cái này: (a < 50)
(a < 50) sẽ là 1 nếu a < 50, 0 trong trường hợp ngược lại. :slight_smile:

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?