TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ken H
NA
646
360.7k
use + for string concatenation in c++
Oct 13 2013 11:07 PM
Hello everyone,
Use the + connection string error occurs, the code is as follows:
It is a reference Vulpes sir great article:
http://www.c-sharpcorner.com/UploadFile/b942f9/converting-numbers-to-words-in-C-Sharp/
#include<iostream>
#include <stdlib.h>
#include <string>
using namespace std;
string NumberToWords(int number,bool isUK)
{
if(number==0) return "Zero";
string and=isUK?"and ": ""; // deals with UK or US numbering
if(number==-2147483648)
return (string)"Minus Two Billion One Hundred "+and+"Forty Seven Million Four Hundred "+and+"Eighty Three Thousand "+
"Six Hundred "+and+"Forty Eight";
int *num=new int[4];
int first=0,u,h,t;
string sb;
if(number<0)
{
sb+="Minus";
number=-number;
}
string words0[]={"","One ","Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "};
string words1[]={"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
string words2[]={"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "};
string words3[]={"Thousand ", "Million ", "Billion " };
num[0]=number % 1000; // units
num[1]=number / 1000;
num[2]=number / 1000000;
num[1]=num[1] - 1000 * num[2]; // thousands
num[3]=number / 1000000000; // billions
num[2]=num[2]-1000 * num[3]; // millions
for (int i=3;i>0;i--)
{
if(num[i]!=0)
{
first=i; // The maximum number of obtained
break;
}
}
for (int j=first;j>=0;j--)
{
if(num[j]==0) continue;
u=num[j]%10; // ones
t=num[j]/10;
h=num[j]/100; // hundreds
t=t-10*h; // tens
if(h>0) sb+=words0[h]+"Hundred";
if(u>0||t>0)
{
if(h>0||i<first) sb+=and;
if(t==0) sb+=words0[u];
else if(t==1) sb+=words1[u];
else sb+=words2[t-2]+words0[u];
}
if(i!=0) sb+=words3[i-1];
}
return (string)sb;
}
int main(void)
{
string input;
int number;
bool isValid;
bool isUK=false;
cout<<"\nEnter '0' to quit the program at any time\n";
while (true)
{
cout<<"\nUse UK numbering y/n:";
cin>>input;
if(!(toupper(input)=="Y"||toupper(input)=="N"))
cout<<"\nMust be 'y' or 'n', please try again\n";
else
{
if(toupper(input)=="Y") isUK=true;
cout<<'\n';
break;
}
}
do
{
cout<<"Enter integer:";
cin>>input;
isValid=(atoi(input))>0?true:false;
if(!isValid) cout<<"\n Not an integer, please try again\n";
else cout<<"\n"<<NumberToWords(number,isUK)<<endl;
} while (!(isValid&&number==0));
cout<<"\nProgram ended"<<endl;
return 0;
}
thank
s.
Reply
Answers (
3
)
buffer problems in c
custom console menu in c++