-6

this program is simple : 1)take an input string . 2) convert it to long . 3) print convert result.

expected an output,but nothing found.

            #include <stdio.h>
            #include <string>

            using namespace std;
         int main()
              {
               string ch;
               scanf("%s",ch);

               long l=stol(ch);

               printf("%l",l);
               return 0;
              }
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Dah
  • 92
  • 8
  • 4
    std::string and scanf don't work together. Please pay attention to your compiler warnings. If your compiler didn't warn you, upgrade it now. – n. m. could be an AI Mar 31 '19 at 13:26
  • 2
    Your code is compiled under my Ubunu g++ and returns: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'std::__cxx11::string' {aka 'std::_ ------------ I think you want to get char from user and return its long integer value but be careful your sacnf gets string with %s – CumaTekin Mar 31 '19 at 13:28
  • @n.m. with what i can replace it – Dah Mar 31 '19 at 13:33
  • 1
    Why are you mixing C and C++? Just use the one that you really want. – machine_1 Mar 31 '19 at 13:39
  • 3
    This site is not an on-demand C++ tutorial. Try reading a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – n. m. could be an AI Mar 31 '19 at 13:47

1 Answers1

2

Here's how it's done with C++ I/O. There's very little reason for using C I/O in a C++ program.

#include <iostream>
#include <string>

int main()
{
    std::string input;
    std::cin >> input; // take an input string
    long lval = stol(str); // convert to long
    std::cout << lval << '\n' // print the result
}

Now this stuff would be covered in the first chapter of any C++ book. A good book will greatly increase how quickly and how well you learn C++.

john
  • 85,011
  • 4
  • 57
  • 81