Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to make a question and a answer in c++

+4 votes
asked Dec 19, 2021 by WHITELIGHT UNSEEN (370 points)
in a new project how to make a question and a answer

2 Answers

0 votes
answered Dec 19, 2021 by Areeb Sherjil (2,050 points)
What is your question?

Show us your code that you made so far, then we'll take it from there.
+1 vote
answered Dec 19, 2021 by Peter Minarik (101,360 points)

Probably you mean taking input from the user and providing output to the user.

The following small example should help.

#include <iostream>
#include <string>

int main()
{
    std::string name;

    std::cout << "What's your name? ";
    std::cin >> name;
    std::cout << "Hello, " << name << "!" << std::endl;

    return 0;
}

std::cout allows you to write to the standard output while std::cin allows you to read from the standard input.

commented Dec 20, 2021 by Areeb Sherjil (2,050 points)
not sure if #inlcude<string> is required, most modern compilers will have that inside iostream.
commented Dec 21, 2021 by Peter Minarik (101,360 points)
I just had a quick check. If I comment out the #include <string> line, the code still compiles and runs fine, so I guess you're right in the sense that it is not needed there since iostream will pull the string header anyway.

I like to explicitly refer to all the needed headers unless the classes are tightly coupled in the code.

For instance, if you have 3 headers for Car, Wheel and Seat and a car always contains a wheel and a seat and these parts always refer to a car, then they are closely coupled. It should be enough to include the car.h, as we are likely never to use only just the seat for instance.

On the other hand, if we have something loosely coupled, for instance in the example code above the std::string and the std::cout/std::cin are not dependent on one another. For instance, I could change the data type of the variable name to char [], or not use iostream as I don't want to display the string, but rather run some analytics on it. Or I want to use <cstdio> and print it via printf. In these cases, if I remove one iostream header, the compiler will not throw an error as now the string header (referenced from inside the now removed iostream header) is missing.

Last, but not least, every header file should have an include guard, so having the same header included multiple times should have a minimal overhead during the compilation.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...