SolvedNeed help with making a simple Hello World C++ Program

Posts 15 of 5 · Page 1 of 1
Need help with making a simple Hello World C++ Program
So I just started my Computer Science class and I am having a lot of problems.

I need to make a hello world program that can accept names and such.

However, the program is I don't know how to do it so well. I can output Hello World, but I wanted to add names and answers. So in the end it would be like:

Code:
Hello, what is your name?
(bob)
Who is your friend?
(suzy)
This is Bob, he has a friend called Suzy. Bob kills Suzy.
Can anyone help me out? I can't seem to find tutorials through Google
You'll need to include the header file string
Code:
#include <string>
and create a couple of variables of type string to hold the user's input for Name and friend's name
Code:
string userName, friendName;
then you'll be using the keyword cin to get user input

If you've started a CS class then your first chapter should be on cout, cin, and should possibly include some basic data types (int, double float, and probably string).

And since everyone is going to try to correct name and say it needs to be std::string, yes, I know that, and it should be, but this is a beginner computer science class. The teacher is going to have them doing
Code:
using namespace std;
at the beginning of every project so it doesn't matter. When they get to some more intermediate programming the teacher will explain namespaces in more detail.
And yes, you can use char* or char[] instead to hold the user input, but again, this is a beginner computer science class. Trying to explain a char* and char[] to people who don't even know what a function is is like trying to teach a cat dog tricks. It can be done, just not right now.
I hope this helps.
Code:
#include <iostream> 
#include <string>
using namespace std;

int main()
{

    cout << "Hello, what is your name." << endl;          // endl; is one way of starting a new line
    string name;                                          // name is a string of chars
    cin >> name;                                          // user input 

    cout << "Who is your friend?" << endl;
    string Friend;
    cin >> Friend;

    cout << "This is " << name << ", he has a friend called " << Friend << ". " << name << " kills " << Friend << ".";

}
sorry if any errors but im sure you can figure it out.
simple way, it can be much neater but will do what you want. and can learn off of it easily.
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string friends;
	string name;
	cout << "Hello what is your name: ";
		cin >> name;
	cout << "And who is your friend: ";
		cin >> friends;
		cout << "This is " << name << ", and has a friend named " << friends << ". " << name << " kills " << friends <<endl;
		system("pause");
}
Solutions have been posted.

Solved
Posts 15 of 5 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?