0

just a beginner student learning basic C++. I'm trying to figure out the best way to:

  1. Turn a char array Name of 20 into a string that can be printed. I found in other Stack Overflow topics to use "str()" such as "str(Name)", but it always comes up 'identifier not found'.

    cout << "Name:" << str(Name) << endl;
    
  2. Set a char array of 20 characters. For some reason, the following gives me errors when declaring. I've tweaked it so many times, but I cannot get why it won't give.

    TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
    

Full code I have so far:

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

//Step 1
struct StudentRecord
{
char Name[20];
//Accessor
void printInfo() const;
};

void StudentRecord::printInfo() const
{
cout << "Name:" << str(Name) << endl;
}

int main()
{
//Step 2
StudentRecord TESCStudent;
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};

//Step 3
TESCStudent.printInfo();

_getch();
return 0;
}
13
  • 2
    Why wouldn't you just use std::cout << Name;? Commented Jul 30, 2014 at 16:41
  • An array of char is already a string that can be printed. Test it out by doing cout << Name;. Commented Jul 30, 2014 at 16:42
  • Why are you using char[] instead of std::string? Commented Jul 30, 2014 at 16:45
  • @clcto "just a beginner student learning basic C++..." Commented Jul 30, 2014 at 16:45
  • 2
    @0x499602D2 More reasons to use std::string. char[] (as opposed to std::string) is definitely not "basic" C++. Commented Jul 30, 2014 at 16:46

2 Answers 2

1

Given that you are at a very beginner level, just use std::string:

#include <iostream>
#include <conio.h>
#include <string>

struct StudentRecord {
    std::string Name;
    void printInfo() const {
        std::cout << "Name:" << Name << '\n';
    }
};

int main() {
    StudentRecord TESCStudent;
    TESCStudent.Name = "SuperProgrammer";
    TESCStudent.printInfo();

    _getch();
}

Live demo

Sign up to request clarification or add additional context in comments.

4 Comments

@SamuelGarcia: Why is it not what you need? Don't let the "Given that you are at a very beginner level" confuse you. I'm sure it was intended in the opposite sense: C++ beginners tend to overuse char arrays, whereas experienced programmers appreciate the benefits of std::string.
@ChristianHackl Both beginners and experienced programmers (especially beginners) should use std::string. char[] is something you may want to study later in the years. C++ beginners tend to overuse char arrays, because there are terrible tutorials out there that advocate such a thing (often together with new and new[] overuse).
@Jeffrey: I think the also overuse it because they either come from C or from a newer language where arrays are not very dangerous in the first place, or because they assume that a built-in feature must be the first choice.
@ChristianHackl I was required and instructed to use char[]. That's why I don't need it.
0

The syntax like this:

char Name[20] = {'S','u','p','e','r','\0'}; 

is used to initialize a variable when you define it. However, in your case,

StudentRecord TESCStudent;
TESCStudent.Name[20] = ...;

You've already defined it on the line before, so you can't "initialize", you have to "assign" it.

This is pretty much why you use std:string instead of char[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.