开发者

C++ String Variable Declaration

I'm having some trouble declaring a string variable. Code and the errors are here: http://pastebin.com/TEQCxpZd 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 i开发者_运维知识库nput 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*, ...)’


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;
 }


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;.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜