First and last name using char 2D array

Write your question here.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  //#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{

	char StudentName[5][6];
	char name = "\0";
	int i = 0, j = 0;
	for (i = 0; i < 5; i++)
	{
		for (j = 0; j < 6; j++)
		{
			cout << "Enter your name: ";
			getline(cin, name);
			if (name.empty())
			{
				StudentName[i][j] = '\0';
				int x, y;
				for (x = 0; x < 5; x++)
				{
					for (y = 0; y < 6; y++)
					{
						if (!StudentName[x][y].empty())
						{
							cout << StudentName[x][y];
							cout << "\n";
						}
						else
						{
							exit(0);
						}
					}
				}
				exit(0);
			}
			else
			{
				StudentName[i][j] = name;
			}
		}
	}


I am getting an error saying I can't use a char, but I need to use char and cant use string.
Any Help would be appriciated!!
you can't getline with a char, just use cin >> name
Hello MADZILLA,

While I see if I can duplicate your problem it would help to see the actual error message and to know what IDE/compiler you are using.

Andy
you can't getline with a char

Do you realize that there are two different getline() functions?
One works with std::string:
http://www.cplusplus.com/reference/string/string/getline/

the other works with a C-string:
http://www.cplusplus.com/reference/istream/istream/getline/

But in this instance you probably would be better off using the extraction operator, but don't forget to use setw() to limit the number of characters you will try to retrieve into a C-string to avoid potential buffer overrun problems.

Hello MADZILLA,

I have not worked out everything yet, but for a start:

You say that you can not use a "std::string", but you have included the header file "string". Why?

Your variable "name" is defined as a single "char" and I believe it should be an array. Second you are trying to initialize a single "char" with a string. This will not work. To be correct it should be single quotes around the (\0).

From C++11 on the use of empty {}s will properly initialize a variable. Or as you will see you can put something between the {}s.

Unless you need "i" and "j" outside of the for loops they should be defined in the for loops as:
for (int i = 0; i < 5; i++) actually I would suggest for (int i = 0; i < MAXROW; i++)

For line 16 a "std::getline(...)" is only used with a "std::string", which you can not use. I have not tested this yet, but I believe what you need is: std::cin.getline(name, MAXSIZE);

Your use of ".empty()" is a member function of the "std::string" class and can not be used on a character array. It has been awhile, but I think you want the C function of "strlen()".

In your last else statement the "=" would work with a "std::string", but for a character array you would need to use "strcpy" for this.

I have realized that you are using VS2017, which compiles to a minimum of C++14 standards, 0so the C functions of "strlen()" and "strcpy" are likely to be a problem. The compiler should suggest an alternative or you can put #pragma warning(disable : 4996) after your include statements.

Since you entire program is in "main" you should use return 0; and not exit(0);

I changed the beginning of the program. You should find this easier to work with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	constexpr size_t MAXROW{ 5 };
	constexpr size_t MAXCOL{ 10 };
	constexpr size_t MAXSIZE{ 10 };

	char StudentName[MAXROW][MAXCOL]{};
	char name[MAXSIZE]{};   // <--- Changed.
	//int i = 0, j = 0;

	for (int i = 0; i < MAXROW; i++)
	{
		for (int j = 0; j < MAXCOL; j++)
		{

Using VS2017 "size_t" should be a typedef for an "unsigned int" unless you have changed the "Solution Platform" from "x86" to "x64" then "size_t" is typedefed as a "long".

Until I can correct the problems I do not know yet how the program runs.

Hope that helps,

Andy
Hello MADZILLA,

Now that I have the program running it would help to know how it should work. And what it should do.

Andy
@jib

>Do you realize that there are two different getline() functions?
Yes, and while I'm aware you can also use getline with C-strings, OP said he would only want to use char.

My bad for not thinking he might have meant cstrings, since he obviously wanted to get the user to input his name :)
Topic archived. No new replies allowed.