0
why my code is not working?
#include<conio.h> #include<stdio.h> #include<iostream.h> void main() { char a[25]; cout<<"who is the chief minister of mp?"; cin>>"%c",&a; if ("%c"=="kamal nath"); { cout<<"correct"; } else { cout<<"wrong"; } getch(); \\the else block is not executing what should i do?
2 Respostas
+ 2
What's "%c"? Are you mixing up c with c++?
stdio is c header
iostream is c++ header
C and C++ aren't the same.
Correct syntax would be
cin >> a;
And you can't compare array like that cause a[] points to the first element of the array. You have to use a loop or use data type
string a;
+ 1
Perhaps try writing standard C++ instead of Turbo C++ (?). Your code won't compile on modern compilers.
#include <iostream>
#include <string>
int main() {
std::string a;
std::cout << "Who is the chief minister? : ";
getline(std::cin, a);
if (a == "Kamal Nath") {
std::cout << "Correct";
}
else {
std::cout << "Wrong";
}
}
Anyway, if you insist to write in Turbo C++, the problem should actually be with that extra semicolon after the if statement... and a couple more problems as pointed out by Akib Reza.