C ++ learning: Problems with converting integers into strings

Source: Internet
Author: User

Solution: 1) extract the input int data using the "%" and "/" methods and import the values of each bit into the character array.

2) since the number is imported into the character array from the delimiter to the highest digit, the character array should be in reverse order.

The programming code used is as follows:

# Include <iostream> using namespace STD; void change (INT number, char s []); void main () {int number; char s [50]; cout <"Please input INTEGER:" <Endl; CIN> Number; cout <change (number, s []) <Endl;/* why is this row incorrect: error c2059: syntax error: ']' */} void change (INT number, char s []) {int I = 0, J; char chtemp; while (0! = Number) {s [I] = Number % 10 + '0'; I ++; number/= 10;} s [I] = '\ 0 '; for (j = 0; j <I/2; j ++) {chtemp = s [J]; s [J] = s [i-j-1]; s [i-j-1] = chtemp ;}}

But there is always an error cout <change (number, s []) <Endl;/* why is this line incorrect: Error c2059: syntax error: ']' */
Subsequent changes:
First Change:
Void change (INT number, char s []); changed to Char change (INT number, char s []);
Cout <change (number, s []) <Endl; changed to cout <change (number, S) <Endl;
Void change (INT number, char s []) to Char change (INT number, char s [])
Add the following statement to the change function: Return s [I];
The error is lost, but no data is returned, as shown in the figure below

Later it was changed to: "Change (number, S); cout <S <Endl;: solved the problem.
But I don't know where the error is: 1) the array as a form parameter cannot be passed like this? <So there should be an error in the first change>
2) Is it because cout usage is not allowed?
If a pointer is used:

# Include <iostream> using namespace STD; void change (INT number, char * s); void main () {int number; char * s; cout <"Please input INTEGER: "<Endl; CIN> Number; change (number, S); // cout <* S <Endl; this row is incorrect! Cout <S <Endl;} void change (INT number, char * s) {If (0 = Number) {* s = '\ 0'; return ;} change (number/10, S); // before S, you cannot add "*" while (* s) s ++; * s = Number % 10 + '0 '; // * s = '\ 0 ';}

// Compilation is normal, but the program crashes ...... I don't understand. Put it first. Let's see it tomorrow ......

Solve the problem!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.