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;
}
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
  • 2
    Why wouldn't you just use `std::cout << Name;`? – Retired Ninja Jul 30 '14 at 16:41
  • An array of `char` is already a string that can be printed. Test it out by doing `cout << Name;`. – David G Jul 30 '14 at 16:42
  • Why are you using `char[]` instead of `std::string`? – clcto Jul 30 '14 at 16:45
  • @clcto *"just a beginner student learning basic C++..."* – David G Jul 30 '14 at 16:45
  • 2
    @0x499602D2 More reasons to use `std::string`. `char[]` (as opposed to `std::string`) is definitely not "*basic*" C++. – Shoe Jul 30 '14 at 16:46
  • @0x499602D2 Ah, thanks! I didn't know that. :) Now I just can't figure out the second question why I can't init an array like: TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'}; without getting errors – Samuel Garcia Jul 30 '14 at 16:49
  • @SamuelGarcia You are obviously lacking the very basics of C++, while trying to do non-basic tasks. [Please read a C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) first. – Shoe Jul 30 '14 at 16:51
  • Because C++ doesn't work like that, unfortunately. You can use `strcpy(TESCStudent.Name, "blah");` – Neil Kirk Jul 30 '14 at 16:52
  • @SamuelGarcia Because a braced initializer `{...}` can only be used in certain contexts, that is not one of them. Also, the `[20]` shouldn't be there. Instead, change `Name` into `std::string Name;` and do `TESCStudent.Name = "SuperProgrammer"` – David G Jul 30 '14 at 16:52
  • C++ (and also plain C) will do you the favor of recognizing a string constant such as "This is a string" as an array of `char`, so that you don't have to enclose each individual character in quotes when it is used as an initializer. It will even provide a null terminator character. Just the same, I'd recommend using `str` instead of `char[]`. – Logicrat Jul 30 '14 at 16:59
  • @Jefffrey Sorry, I'm just learning and starting, and well, what better place to ask than here? – Samuel Garcia Jul 30 '14 at 17:02
  • @SamuelGarcia I'm glad it worked! But be clear I agree with the other posters. `std::string` is a lot easier to use. – Neil Kirk Jul 30 '14 at 17:07
  • @NeilKirk it may be, but the requirement for the coding called specifically for char array of 20 characters, so I'm stuck with it anyways. – Samuel Garcia Jul 30 '14 at 17:10

2 Answers2

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

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • @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`. – Christian Hackl Jul 30 '14 at 18:33
  • @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). – Shoe Jul 30 '14 at 18:45
  • @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. – Christian Hackl Jul 30 '14 at 18:47
  • @ChristianHackl I was required and instructed to use char[]. That's why I don't need it. – Samuel Garcia Aug 24 '14 at 00:35
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[].

James Curran
  • 101,701
  • 37
  • 181
  • 258