for C++

#include "stdafx.h";
#include <iostream>

using namespace std;

int main () {
	int	x;
	char name;
	cout << "Please Type in your Full Name: ";
	cin >> name;
	cout << "Hello" << name << " , Now please type in your age: ";
	cin >> x;
	cout << "Your name is " << name << " and you are years old " << x << endl;

	
	float l;
	cin >> l;
	return 0;
	
}

The problem isn't cin. It's your variable(s).

Your variable "name" is a single char, it's neither a C++-style std::string nor a C-Style char array.

It must be either of those 2 types to hold a person's complete name.

Edited 5 Years Ago by Fbody: n/a

On line 8 you define a single char, not an array (multiple char's linked together), so you need an array instead. e.g.

char name[30];

on line 10 you need the following if you want to read the entire line

cin.getline(name, 30) // first parameter is the variable you want to store the info in and second is maximum length of string input

Thank you that fixed it, can you please tell me why I had to put

char name[30];

That statement declares an array of 30 char instead of a single char variable.

You have to have an array of char to hold a "string" (multiple consecutive char, terminated by a NULL). Otherwise, all you have is a variable capable of storing only a single char.

Just a quick note. What you had earlier,

char name;

is the same as

char name[1]; // variable holding 1 character

// They changed it to 30,

char name[30];

// so that any input you enter will have enough space to be saved to the variable "name".

char name;

is the same as

char name[1];

I'm not so sure that's necessarily true, as the first doesn't involve a pointer and the second does. (I welcome opposing viewpoints)

Edited 5 Years Ago by jonsca: News editorial disclaimer

::Open mouth, insert foot::

You're right. I was *assuming* that if you could create a char by my previous statement than character with [1] array would be the same, only a character of 1 input. Even if you wanted to, needs to be a minimum of [2] for the NULL as described above. Else you get a debug error.

You are correct, Jonsca. In the future, I'll put it in a more questionable format for others to confirm instead of a 'matter of fact' phrase, and thoughtless ponder to actual trials.

This article has been dead for over six months. Start a new discussion instead.