LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-05-2004, 06:29 PM   #1
1337 Twinkie
Member
 
Registered: Jul 2004
Distribution: Fedora Core 2
Posts: 79

Rep: Reputation: 15
Question converting integers to strings


Is there an easy way to convert an integer value to a string so it can be printed? I am using the printw() functions in the ncurses library (C++) and they only accept strings as arguments.
 
Old 08-05-2004, 08:23 PM   #2
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
In c++:

Code:
using namespace std;

template<typename numeric_t>
string 
type_to_string(const numeric_t& n)
{
    ostringstream oss;
    oss << n;
    return oss.str();
}
 
Old 08-05-2004, 08:57 PM   #3
1337 Twinkie
Member
 
Registered: Jul 2004
Distribution: Fedora Core 2
Posts: 79

Original Poster
Rep: Reputation: 15
I tried what you said, but it didn't work. Here are the compiler errors:

Quote:
In function `std::string type_to_string(...)':
80: error: aggregate `std:stringstream oss' has incomplete type and
cannot be defined
81: error: `n' undeclared (first use this function)
81: error: (Each undeclared identifier is reported only once for each
function it appears in.)
In function `void draw_score(int)':
91: error: cannot convert `std::string' to `const char*' for argument
`3' to `int mvprintw(int, int, const char*, ...)'
BTW, the integer value I am trying to change is a variable, not a constant. I don't know if it makes a difference.

I did do #include <string>.
 
Old 08-05-2004, 09:10 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
You can either print the number directly:
Code:
printf("%d\n", num);
Or you can put it in a buffer and then print it:
Code:
{
  char numstr[100]; // Or some other sufficiently large size

  sprintf(numstr, "%d", num);
  puts(numstr);
}
That's the C way to do it, but should work fine in C++
 
Old 08-05-2004, 09:33 PM   #5
james.zhang
LQ Newbie
 
Registered: Feb 2004
Location: China Hunan
Posts: 9

Rep: Reputation: 0
sprintf function is a good way!
 
Old 08-05-2004, 10:00 PM   #6
1337 Twinkie
Member
 
Registered: Jul 2004
Distribution: Fedora Core 2
Posts: 79

Original Poster
Rep: Reputation: 15
Ok, I'm going to try the sprintf() function looks like it will work. So, the question is, what arguments do I need to pass to it? I tried passing the single int value, which didn't work.
 
Old 08-05-2004, 10:37 PM   #7
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
Code:
#include <ncurses.h>

int main() {

    initscr();

    int i = 23423;

    printw("%d\n",i);
    refresh();

    endwin();
    return 0;
}

Last edited by jhorvath; 08-05-2004 at 10:43 PM.
 
Old 08-06-2004, 12:13 AM   #8
1337 Twinkie
Member
 
Registered: Jul 2004
Distribution: Fedora Core 2
Posts: 79

Original Poster
Rep: Reputation: 15
Thumbs up

Thanks for the tip. Works great.

So, what does the "%d\n" do in the printw() function? Does it allow printing of multiple arguments or something?
 
Old 08-06-2004, 01:51 AM   #9
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
Quote:
Originally posted by 1337 Twinkie
Thanks for the tip. Works great.

So, what does the "%d\n" do in the printw() function? Does it allow printing of multiple arguments or something?
Do a search for the terms placeholder and escape sequences. First argument of printw isn't a string; it's a format string but `can be a string only'. As you will see in the man page prototype of printw is

Code:
int printw(const char *fmt, ...);
and three dots mean variable count of arguments. Finally, %d is the placeholder for integer type.
 
Old 08-06-2004, 02:11 AM   #10
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Hi if you still want a funtion to convert an integer into string, you can use the function in the header file stdio.h.

char *ecvt(double number, int ndigits, int *decpt, int *sign);

The ecvt() function converts number to a null-terminated string of
ndigits digits (where ndigits is reduced to an system-specific limit
determined by the precision of a double), and returns a pointer to the
string. The high-order digit is nonzero, unless number is zero. The low
order digit is rounded. The string itself does not contain a decimal
point; however, the position of the decimal point relative to the start
of the string is stored in *decpt. A negative value for *decpt means
that the decimal point is to the left of the start of the string. If
the sign of number is negative, *sign is set to a non-zero value, oth-
erwise it’s set to 0. If number is zero, it is unspecified whether
*decpt is 0 or 1.


Wish you all the best.
 
Old 08-06-2004, 04:59 AM   #11
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Quote:
In function `std::string type_to_string(...)':
80: error: aggregate `std:stringstream oss' has incomplete type and
cannot be defined
81: error: `n' undeclared (first use this function)
81: error: (Each undeclared identifier is reported only once for each
function it appears in.)
In function `void draw_score(int)':
91: error: cannot convert `std::string' to `const char*' for argument
`3' to `int mvprintw(int, int, const char*, ...)'
error at line 80: #include <sstream> // my fault, sorry

error at line 91: to convert std::string to const char* call string::c_str() for a null terminated string or string::data() for a non-null terminated string.

It sounds like you are using c and c-style strings so I would stick with one of the other suggestions.

(this post is principally to correct errors should anyone else read it or use it for reference)
 
Old 08-06-2004, 10:31 AM   #12
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
Quote:
Finally, %d is the placeholder for integer type
si seor,... so for every variable you want to insert into the string that is inside the ()'s of printw you need a placeholder for it...

Code:
#include <ncurses.h>

int main() {

    initscr();

    int i = 23423;
    int j = 456;

    printw("variable 'i' = %d\nvariable 'j' = %d",i,j);
    refresh();

    endwin();
    return 0;
}
i made that hard to read on purpose ^^ ..you should see the effect of the '\n' in the code now..i put it in the last post out of habit, it was not necassary..

http://www.iota-six.co.uk/c/c2_printf_and_scanf.asp will shed more light on it
as far as the '\n', look at http://www.liquifried.com/docs/useful/Cescapeseq.html

Last edited by jhorvath; 08-06-2004 at 10:32 AM.
 
Old 08-06-2004, 04:57 PM   #13
1337 Twinkie
Member
 
Registered: Jul 2004
Distribution: Fedora Core 2
Posts: 79

Original Poster
Rep: Reputation: 15
Smile Thanks!

Thanks for all the help! I went ahead and just printed the value using printw("%d\n",value). Since I had just been putting arguments into printw() like: printw("argument"), it wouldn't accept the integer with just printw(integer_value).

If anybody would like to see the fruits of this discussion, I posted the program and source available for download on my website. It is the ASCII Paddles game (at the bottom).

FYI, The integer value that needed to be printed was the score, seen in the upper right corner.

Thanks again!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
64-bit integers in C nodger Programming 1 03-31-2005 08:06 AM
C++ -> Converting strings to char* Dark Carnival Programming 1 01-25-2005 04:17 PM
C++ converting strings to variables dhbiker Programming 9 05-31-2004 04:06 AM
Bash, non-integers and arithmetic causticmtl Programming 5 07-16-2003 09:15 AM


All times are GMT -5. The time now is 08:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration