473,651 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

plz i want a function convert the "int" to " string"

25 New Member
Hi
i want your help again in c++ !!

i want a function change the "int" to "string"
because in my project "Registrati on system" in task "b" i must write a course class which contains number of credits and a string id. In addition to the constructor, it should contain a print function..

and the id of course is seprate in 2 part "for example:cs201" thats why i need this function to convert the "int" part to "string" and i found the function sprint but i can not undarstand its work well !! so i want metod more easy than it or if you can explain to me how does it work ??

this my traying to solve

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. class course
  5. {
  6. private:
  7. int credits;
  8. string ID;
  9. static int serialid; 
  10. public:
  11. course (int x, string y)
  12. {char a[10];
  13. sprint(a,"%d" ,serialid)
  14. ID= y+a;
  15. x= creadits;
  16.  
  17. }
  18. void print()
  19. cout<<creadits;
  20. cout<<ID;
  21. }
  22. }; 
  23. course::serialid=1;
  24.  
Nov 12 '07 #1
6 2695
sicarie
4,677 Recognized Expert Moderator Specialist
Warly girl-

Please read this section for use of code tags, and use them in the future. Thanks.
Nov 12 '07 #2
Warly girl
25 New Member
i am sorry . i will try to be best in the future .
sorry again but i realy can not use clear english in my qustion
i like this fourm
it is best than arabic fourm that i visit always
i think that i am student in this fourm
and i think also that all person will pe patient to my bad language
i am in level 1 . plz don not prevent me to learn here just because of my week language .....

thanks
Nov 12 '07 #3
toefraz
19 New Member
It's pretty simple. Just use the itoa function with the cstdlib header.

Example:

Expand|Select|Wrap|Line Numbers
  1. int number = 1200;
  2. char numArray[10];    //Size of 10 should be good, can increase if needed.
  3. itoa(number, numArray, 10); //10 is for the base.
  4. string str = numArray; //Convert to a string (requires string header)
Nov 12 '07 #4
Ganon11
3,652 Recognized Expert Specialist
If you prefer to not use the legacy C function, you can use a stringstream:

Expand|Select|Wrap|Line Numbers
  1. stringstream ss;
  2. string myStr;
  3. int myNum = 12345;
  4.  
  5. ss << myNum;
  6. ss >> myStr;
  7. //myStr now equals "12345"
Nov 12 '07 #5
toefraz
19 New Member
That's cool. I didn't know such a thing existed. I'll have to read up on stringstreams.
Nov 12 '07 #6
Warly girl
25 New Member
sorry about my late
&
Thanke you very much
Nov 16 '07 #7

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

Similar topics

30
2359
by: JKop | last post by:
When you want to store an integer in C++, you use an integral type, eg. int main() { unsigned char amount_legs_dog = 4; } In writing portable C++ code, there should be only two factors that influence which integral type you choose:
7
3666
by: ma740988 | last post by:
The string object value_f doesn't produce the right output. At issue, - I suspect - is the conversion from string to int with istringstream. An alternate approach? Thanks in advance #include <iostream> #include <sstream> #include <string> using namespace std;
5
7310
by: Stuart | last post by:
Ok let me explain: I am writing a c# program that calls into an unmanaged C++ third-party DLL. I have to make a number of calls and for simplification the protype I am calling is: bool fn(int *pResult); This result is used by other subsequent functions that I have to call. Q: Is it possible to call this function from C# declaring
2
1567
by: privetv7 | last post by:
ppl... HELP!!!!! i don't know how to convert String into char..... for example i can do String input; int number; input = JOptionPane.showInputDialog( "Enter what ever" ); number = Integer.parseInt(input)
3
3446
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a little bit complicated in this beautiful language to me .....
16
3762
by: ondekoza | last post by:
Hello, I need to convert the string "FFFFFFFF" to a long. To convert this string I tried the following: >>> 0xffffffff -1 >>> 0xffffffffL 4294967295L OK, this is what I want, so I tried
10
7539
by: R2d2Rabeau | last post by:
Hi, I'm new to C#, please be patient :-) I am trying to pass an Arraylist (myAL) to a jagged array (myAL2) . The problem I have is that when I write the values to a file i do not get the values instead I get "System.String" ? How can I fix the problem? I wanted to use ArralyList because its length is flexible and I can add values to it. ...All help will be greatly appreciated. Here's my code:
6
5316
by: Lawrence Spector | last post by:
I ran into a problem using g++. Visual Studio 2005 never complained about this, but with g++ I ran into this error. I can't figure out if I've done something wrong or if this is a compiler bug. Here's a very simple example which should illustrate what I'm doing. #include <iostream> template <class T> class TestBase {
1
4320
by: khgoh | last post by:
Hi, Need help for the error "OverflowError: long int too large to convert to int" while reading a zip file content. Python version: Python 2.4.1 (#1, Sep 13 2005, 00:39:20) on linux2 Code >>> import os, datetime, sys, optparse, string, operator, gzip >>> health_day_summary_file = gzip.GzipFile( "health_CORR_summary.csv.gz", "rb")
0
8347
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8694
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.