C++ String Variable Declaration

I'm having some trouble declaring a string variable. Code and the errors are here: Any thoughts on what I'm doing wrong? Also, please keep it platform independent. Thanks!

#include <stdio.h> #include <string> using namespace std; int main() { string input; //Declare variable holding a string input = scanf; //Get input and assign it to variable printf(input); //Print text return 0; } Getting this from GCC: main.cpp: In function ‘int main()’: main.cpp:53:10: error: invalid conversion from ‘int (*)(const char*, ...)’ to ‘char’ main.cpp:53:10: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]’ main.cpp:54:14: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’ 
2

3 Answers

You are mixing c++ and c I/O. In C++ this is,

#include <string> #include <iostream> int main(void) { std::string input; std::cin >> input; std::cout << input; return 0; } 
4

I understand the question to be: How do you make a string declaration in C++? Here's a short program to demonstrate:

#include<iostream> #include<cstdlib> using namespace std; int main() { string your_name; cout << "Enter your name: "; cin >> your_name; cout << "Hi, " << your_name << "!\n"; return 0; } 

So, include cstdlib at the start of your program. In practical terms, this means typing string instead of std::string, cout instead of std::cout and so on. The string variable itself (in the example, the string variable is your_name) is declared with string.

Let's say you've saved the program with the filename, 'str_example.cpp' To compile the program at the command line (in Linux):

g++ -o str_example str_example.cpp 

This creates an executable object file called str_example (no file extension). And finally, assuming you're in the same directory as the program, to run it:

./str_example 

The man page for g++ is extensive but not included by default. To install g++ documentation using the aptitude package manager:

sudo apt-get install gcc-7-doc 

Note that the '7' refers to version 7; the current version at the time of writing. Hope that helps.

cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’

input = scanf; //Get input and assign it to variable 

You're trying to assign the function pointer to scanf to a string variable. You can't do that, which is why you're getting the first error. The proper syntax would be.

char buffer[BIG_ENOUGH_SIZE]; scanf("%*s", sizeof(buffer) - 1, buffer); input = buffer; 

But that's a very C-style way of doing things. The idiomatic way to read input in C++ is with std::cin >> input as Nathan suggested.

cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’

printf(input); //Print text 

printf takes a const char* as its first argument, not a std::string. You can use .c_str() to convert to a C-style string. But never pass user input as the first argument to printf; the user can do nasty stuff by putting %'s in the string. If you insist on C-style output, the correct syntax is:

printf("%s", input.c_str()); 

But the C++-style alternative is std::cout << input;.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like