9

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 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
  • Possibly from Pascal where you can call a function without the (). Commented Jan 20, 2011 at 5:45
  • That's strange, C++ isn't Pascal. Commented Jan 20, 2011 at 17:50

3 Answers 3

11

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;
 }
Sign up to request clarification or add additional context in comments.

4 Comments

@Mike: Have you included the iostream header? #include <iostream> Also, you should probably be using g++ to compile C++ programs, rather than gcc.
I did include iostream. The problem was in terminal I had to type g++ main.cpp -o hello_world instead of gcc. Thanks cody!
@Mike: You're welcome. Please don't take this as offensive, but it looks like you're a little confused between C and C++. They're actually different languages, even though one is derived largely from the other and they share a lot of the same syntax. If you're trying to learn the language, you need to pick one. As both answers indicate, there are very different idioms used in C++ (as compared to C) for things as simple as reading input. Everything but your title gives the impression you might be trying to use C in the first place, which explains why you write the code you did and used gcc.
Well I downloaded a C++ IDE that created hello world using stdio so I assumed it should compile but a few things happened; 1. I couldn't build projects in the IDE, 2. I didn't know much about GCC to know that it doesn't compile C and C++, I though G++ enable GCC to compile C++, 3. All my knowledge of C++ was on windows, and as minimal as it was, I assumed if iostream wasn't working it was a windows header. Now I'm fine and can continue using this book I got which was supposed to be platform independent C++ but wasn't working due to the fact I was using GCC not G++. Thanks again for your help!
3

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.

Comments

2

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 Comments

Well I can't even compile Hello World properly. I think something is wrong with the compiler, whether the code is right or not, I know there is nothing wrong with #include <iostream> using namespace std; int main() { cout << "Hello World!" << endl; return 0; }
What seems more interesting here is that when building under MSVC the implicit cast from std::string to const char* succeeds but does not when compiled with GCC. I imagine this is because the MSVC implementation of std::string contains a cast operator for const char* whereas the GCC implementation does not?

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.