NEED HELP FOR OOP C++

I really need your help guys! I need a sample or the SCRIPT for simple input output in OOP style in C++. This includes "class". For example, I need to input my name, and then show it after I enter it.
Name: _______

output>> Your Name is: _____

Hope you can help me! thanks in advance dude! :)
closed account (o3hC5Di1)
Hi there,

This functinality is pretty much provided by std::iostream already, which provides classes for standard input and output:

1
2
3
4
5
6
7
//you'll need to #include <string> and <iostream> and define this in main()
std::string name;

std::cout << "Name: ";
std::cin >>name;

std::cout << "Your name is " << name;


You could wrap your own class around std::cin and std::out, but that would be kind of ridiculous since the standard provides them to you ready to use.

All the best,
NwN
thank you for your reply sir. but I really don't get it. where is the class? I really need it :( thank you again!
I need a script in OOP style :( this is what i need to do...

Name: ______ // input name
Age: _______ // input age
Birthday: ______ // input bday

and will display: for example

OUTPUT:

Name: Darren Claveria
Age: 21
Birthday: Sept. 23 1991

Really need your help guys! :( thanks!
Last edited on
So the outputs of the Name, Age and Bday need to be presented just like you wrote? Or can you do things like:

"Please enter in your first name."
User enters in name.
"Please enter in your age."
User enters in age.
"Please enter your birthday."
User enters birthday.

then the output is just

Name: Darren Claveria
Age: 21
Birthday: Sept. 23 1991





For this, you will have to use multiple variables.

For the age, you can simply use an int variable, so you would set that up like this:

int age;

Simple.

Now the Name, if you were to do first and last, you would have to use a string. You will initialize that string like this.

First, along with #include <iostream> You will also have to include the header file #include <string>

When you initialize your string, it will be as simple as this.

string name;

And thats it. You can also use the string for the birthday as well.

string birthday;

Also very easy.

Now, to get all of the info read into the program, you will have to prompt the user to enter in the info.

For the int, age, that is very simple.

Write a command for the user to read, and tell them exactly what you want them to do.

cout << "Please enter your age" << endl;

That just prints out "Please enter your age" Onto the screen. The command for the user to enter in their age is just as simple.

cin >> age;

That is all. Note the different direction used for the arrows < and >. It is very easy to remember the differences between cin and cout too. cin is used for the user to enter information, or put information in. cIN. cout is used to output information. cOUT.

The string command is a little bit different. To prompt the user to enter in a string, use the exact same prompt you used for the int.

cout << "What is your name?" << endl;

or

cout << "What is your birthday?" << endl;

Now for the user to enter in the info, the command looks like this.

getline (cin, name);

or

getline (cin, birthday);

This will gather all of the info the user enters in, whether it be numbers, names, strings, characters, whatever they enter in, it will store that info under the string.

getline simply gets that line of info, and stores it cIN the string birthday or name.

So your program would look something like this.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

int main()
{

      int age, a;
      string name, birthday;

      cout << "What is your name?" << endl;
      getline (cin, name);
      cout << "What is your birthday?" << endl;
      getline (cin, birthday);
      cout << "What is your age?" << endl;
      cin >> age;
      

      cout << "Name: " << name << endl;
      cout << "Age: " << age << endl;
      cout << "Birthday: " << birthday << endl;
 
      cin >> a;

   return 0;
}


For some reason, when I had the user enter in the name, age, and then birthday, or string, int, string, the program would jump the gun and output everything before the user could enter in the birthday. I'm not sure why that is, maybe someone can answer?

Anyways, the program that is written there does exactly what you asked for it to do. I hope that helps you in that you both learned how to write the program, and also will help you think deeper into writing programs.
Topic archived. No new replies allowed.