Char Array input

How do i input(cin) char array for example name[100] and user inputs "Mike Jordan" and the computer scans the whole line and stores it in name[100]. It must store the whole name including the space so when i cout it, it outputs whole name with space "Mike Jordan".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>

using namespace std;

int null(char name[500]) //Function for getting a number of how many digits/charachters "name" of title is made of.
{
	int i;
	for(i=0;name[i]!='\0';i++);
	return i;
}

int main()
{
	char name[500];
	cout << "Title:";
       //here i want to input it but i dont know how (getline is not working)
	cout << "Title is made of " << null(name) << " signs." << endl;
	return 0;
}
closed account (E0p9LyTq)
Easier to use std:string instead of a char array:

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<string>

int main()
{
   std::string name;

   std::cout << "Title: ";
   std::getline(std::cin, name);

   std::cout << "Title has " << name.size() << " characters.\n";
}

Title: Frank Furter The Movie
Title has 22 characters.


If you have to use a char array:
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<cstring>  // for strnlen_s()

int main()
{
   char name[500];
   
   std::cout << "Title: ";
   std::cin.getline(name, 500);

   std::cout << "Title has " << strnlen_s(name, sizeof(name)) << " characters.\n";
}

(same output as above)

http://en.cppreference.com/w/c/string/byte/strlen
Thank you very much on help!
Topic archived. No new replies allowed.