I want to develop the program

hello
i have a program but i would develop this program
1
2
3
4
5
6
7
8
9
# include <iostream>
using namespace std ; 
int main ()
{
int a ; //we have to declare a variable
cout <<" hello bourhane how are you ? " << endl ; 
scanf ("%d%",&a);
return 0 ; 
}



I want to add more questions :
1-how are you ? ( fine or bad )
2- the Initial proposal is fine :answer : ok
3- the Second proposal is bad : answer : why you are bad ??



finlay i'm waiting for an answer

and thnk you vrey mush

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:

1
2
int a;
cin >> a;


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.
Last edited on
probelem in
std::cout << "ok" << endl;
`endl' undeclared (first use this function)
sorry, that should be std::endl I didn't test it.
std::endl;

[edit]
Shit, too slow.
Last edited on
Just note that using the >> operator on an std::istream will not give the best results for inputting strings (spaces and tabs will be ignored). Use getline(std::istream&,std::string) instead.
Topic archived. No new replies allowed.