Hi Akramm,
You've mixed c and c++ here, I don't know which style you were meant to be taught in but since this is a c++ forum I'll assume you want a c++ answer, so the first thing I would say is rather than using scanf to get input use cin like so:
this takes keyboard input and attempts to store what was inputted in a.
Now for your questions. To store a word or sentence or any string of characters, you can use the string type, and use the same operators to take input and to print output, like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include <string>
int main()
{
std::string str; // declare a string variable
std::cin >> str; // take input and store it in str
if (str == "fine") // are they fine?
std::cout << "ok" << endl; // output ok if they are fine
else
std::cout << "why are you bad?" << endl; // output why are you bad if they are not
return 0;
}
|
Notice that you must include string at the top of the file, and if you wonder why I've put
std::cin
and
std::cout
instead of just
cin
and
cout
, it's because your program declares that it is using namespace std but mine doesn't, this isn't important for now but if you leave out
using namespace std
they must be preceded by
std::
One more thing, the == operator I have used here
if(str == "fine")
just compares whether the value stored in str is equal to "fine" so that we can decide what to output.