String/Char* Problem in GMP

This is a discussion on String/Char* Problem in GMP within the C++ Programming forums, part of the General Programming Boards category; I am using the GMP multiple precison package. I need to input very large integers as strings, for example Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    String/Char* Problem in GMP

    I am using the GMP multiple precison package.

    I need to input very large integers as strings, for example
    Code:
    mpz_init_set_str(x,"1234567890",10)
    will set x to that large integer in quotes (10 gives the base.)

    I want to be able to have the user enter the value, so I did something like:
    Code:
    #include <string>
    #include <gmpxx.h> // GMP
    
    string num;
    std::cin >> num;
    mpz_init_set_str(x,num,10)
    but this causes g++ to give the following error:
    error: cannot convert `std::string' to `const char*' for argument `2' to `int __gmpz_init_set_str(__mpz_struct*, const char*, int)'

    Any ideas how to input a data type that will work?

    I am using Ubuntu on an AMD64.

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    You need to use the c_str() member function, e.g. num.c_str()
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    nevermind i seriously need to start paying attention
    when i know im half asleep
    Last edited by ILoveVectors; 08-27-2005 at 01:27 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Quickest problem solution ever.

    Thanks!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    plz elaborate

    Laser light

    Could you please elaborate on the use of c_srt() function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21