-2

Using char only you can make a single character variable. How would you make a variable with a word? After searching i found this way:

#include <iostream>
int main()
{
char word[] = "computer";
std::cout<< "choose" << word << std::endl;
return 0;
}

Which gives what i need but it is not the proper way to do it i think? So is this the correct way: to put empty brackets after the variable of char?

Ron
  • 14,674
  • 4
  • 34
  • 47
J_p
  • 435
  • 1
  • 5
  • 16
  • 2
    What source are you learning C++ from? (Also, `#include iostream` is a syntax error.) – melpomene Jan 27 '18 at 16:20
  • 3
    `std::string word = "computer"s;` should work well. –  Jan 27 '18 at 16:22
  • There are various ways of declaring your variable. You can either use char array or a char pointer or a string data type. It depends what you are looking for and how you are going to use your variable in your code – Maddy Jan 27 '18 at 16:24
  • `std::string` is managed and holds a shed load of useful utility. Typically constant char pointers to string literals and char arrays, should only be used in place of `std::string` when using a legacy interface, or (only really in extreme cases) for optimisation purposes. – George Jan 27 '18 at 16:27
  • If you just need to handle some predefined string constant then you can write `auto const & word{"computer"};`. – user7860670 Jan 27 '18 at 16:33
  • 1
    Thanks George and TheDude. I agree. Deleted the comment. – Sudheesh Singanamalla Jan 27 '18 at 16:37
  • what is the "s" and what it does? – J_p Jan 27 '18 at 16:52
  • @J_p See [here](https://stackoverflow.com/questions/48465116/any-advantage-of-using-the-s-suffix-in-c); –  Jan 27 '18 at 17:01

1 Answers1

1

What you have there is not a string but a character array initialized with a string literal. Idiomatic way of working with strings in C++ is utilizing the standard std::string type and invoking one of its constructors, for example the one accepting a std::initializer list:

std::string word{"computer"};

or the copy constructor:

std::string word("computer");

or using the strings operator= to assign the value of a string literal to your variable.

std::string word;
word = "computer";

Be sure to include the <string> header:

#include <iostream>
#include <string>
int main() {
    std::string word{ "computer" }; // starting with C++11
    std::string word2("computer 2");
    std::string word3;
    word3 = "computer 3";
    std::cout << word << '\n';
}
Ron
  • 14,674
  • 4
  • 34
  • 47
  • cant make it work. can you explain?for example where did the name of the variable "word" went? – J_p Jan 27 '18 at 16:49
  • `std::string` is not built-in, but it is standard, which is close enough. – Quentin Jan 27 '18 at 16:54
  • it says `[Error] in C++98 'word' must be initialized by constructor, not by '{...}'` – J_p Jan 27 '18 at 16:55
  • The C++98 standard does not support the initializer lists. Use parentheses instead of braces. The right most column in the above links (the text in green) indicates the standard and availability. – Ron Jan 27 '18 at 16:56
  • 2
    @J_p Why are you using a standard 20 years out of date? – George Jan 27 '18 at 16:57
  • i downloaded devc++ and this is what it had. Also i just started with the c++.Why it had the 98 release??? – J_p Jan 27 '18 at 16:59
  • @J_p devc is capable of working with newer toolchains, simply get the lates version of GCC and install that one. –  Jan 27 '18 at 17:03
  • i picked 11 iso. i typed also your answer with the "s" and it says `[Error] 'word' was not declared in this scope` – J_p Jan 27 '18 at 17:11
  • @J_p Did you miss `#include `? –  Jan 27 '18 at 17:33