473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can I fit this integer into the string?

bajajv
152 100+
I have a string as - "Id is %d" and an integer as 1000. I want a final string as - "Id is 1000".

I tried like this -
Expand|Select|Wrap|Line Numbers
  1.     char* temp = "id is %d";
  2.     int id = 1000;
  3.     char* temp1 = new char[20];
  4.     memset(temp1, '\0', 20);
  5.     sprintf(temp1, "%s %d", temp, id);
  6.     cout << temp1 << endl;
  7.  
the output is - Id is %d 1000.
How can I fit this integer into the string?
Nov 23 '10 #1
4 1356
donbock
2,426 Expert 2GB
Change temp to "Id is ". You only need one %d.
By the way, the memset on line 4 is unnecessary.
Nov 23 '10 #2
bajajv
152 100+
Actually, I am getting the string from some other function, and it is possible that I may get more than one "%d", so I need to put it at run time.
Nov 23 '10 #3
donbock
2,426 Expert 2GB
What should your program do if that other function puts two "%d"'s in temp? Might that other function put "%f" or %s" in temp? Can you trust that other function not to put something crazy in temp?

The answer to these questions will determine how carefully your program needs to examine the contents of temp before calling sprintf.
Nov 23 '10 #4
bajajv
152 100+
There are chances that the other function may return string containing 2, 3 or may be more %ds, but I do get the count for that.
Only thing is, I want to add my integers into that string wherever %d comes.
Nov 23 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mr_burns | last post by:
hi, if i have a datebase field like so: $username = $row; //after SELECT and mysql_query etc. how do i check what file type it is? i have tried using is_string to check it but it returns...
3
by: news.hku.hk | last post by:
could you tell me how can i convet a string to integer?? #include <iostream> #include <string> using namespace std; int main(){ int integer; string buffer("123456789");
4
by: msnews.microsoft.com | last post by:
Very simple goal ... to string format an int with commas. I have int x = 12000 If I use x.ToString("N") I get 12,000.00
13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
35
by: pinkfloydhomer | last post by:
How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and integer, and another if not. /David
3
by: veeman | last post by:
Can someone please write an example containing one element which value is integer type, and one attribute which value is also integer type: Is it something like this: <SomeAttribute...
1
by: Stile213 | last post by:
What's the easiest way to input an integer string using the cin function? The string will be single digits and from 1 to 5 elements long.
2
by: titan nyquist | last post by:
To convert a string to an int, I used: int integer; string str; integer = Convert.ToInt32(str); But, that assumes int = int32. Is there a method that matches whatever "int" is, instead of...
7
by: Jim | last post by:
Hi guys, The variable '$pixels' in the code below may be an integer type or an integer represented as a string type. I need to check if the value is indeed one of these. Is the following code...
1
by: thesti | last post by:
hi, i have a field named 'nim', in my table which is a char(10) field. i want to create a constraint that checks the field so the field must be 10 characters long and each character is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.