Hello, the purpose of this program is to have a user input a number, and the program will then read out the number in words. Example: User inputs 10004, you get one zero zero zero four.
Also, any leading zero should be ignored. user inputs 00001, you get one.
Looking at the final for loop with the switch statement, part of my problem from setting up how the loop and the switch statement interact.
Any hints or ideas?
---------------
#include <stdio.h>
int main(){
int n,m,i,j,k,temp,temp2,numdigits; //add all the variables!
printf("Enter an integer no bigger than 10 digits and not starting with 0: ");//The only easy part of this program
scanf("%d",&n);
numdigits=0; //begin the count here
m=n;
for(j=0;m>0;j++)//counting the number of digits
{
m=m/10;//Divide m, initilized as n, by 10. Keep going until m<=0
numdigits=j+1;//numdigits: adds one to the count each time the loop runs.
}
HERE IS WHERE THE PROBLEM BEGINS
m=n;//re-initialize m as n
for (i=numdigits-1;i>=0;i--)
{
temp=0;
for (k=numdigits-1;k>=0;k--)
{
temp=temp*10;
temp2=n%10;
temp=temp+temp2;
m=temp2%10;
}
switch (m){
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
case 0:
printf("zero ");
default:
printf("error!");
}
}
return 0;
}
/*
* main.cpp
*
* Created on: 19 Feb 2014
*
*/
#include <iostream>
#include <string>
usingnamespace std;
string drop_zeros(constint &length, const string &storage )
{
int position_count = 0;
int leading_zero_count = 0;
string return_string = storage;
while( position_count <= length )
{
char number = storage[position_count];
if( number == '0' )
{
//increase count of leading zero
leading_zero_count++;
//cout << number_store << endl;
}
elseif( number != '0')
{
break;
}
position_count++;
}
if( leading_zero_count != 0 )
{
return_string.erase( 0, leading_zero_count);
}
return return_string;
}
int num_to_words( const string &storage, constshortint &length)
{
shortint position_count = 0;
//Loop will cycle until there are no more numbers in the array.
while( position_count < length )
{
//Store the number as a char because C++ doesn't allow comparison
//for strings.
//Char is used because otherwise it gets the Ascii number instead
char number = storage[position_count];
//Check out which char this number is and then output the word
//associated with it.
switch( number )
{
case('0'):
{
cout << "Zero";
break;
}
case('1'):
{
cout << "One";
break;
}
case('2'):
{
cout << "Two";
break;
}
case('3'):
{
cout << "Three";
break;
}
case('4'):
{
cout << "Four";
break;
}
case('5'):
{
cout << "Five";
break;
}
case('6'):
{
cout << "Six";
break;
}
case('7'):
{
cout << "Seven";
break;
}
case('8'):
{
cout << "Eight";
break;
}
case('9'):
{
cout << "Nine";
break;
}
}
//Space the numbers horizontally
cout << ", ";
//Move to the next number.
position_count++;
}
return 0;
}
int main()
{
//Use string to get input and also using the array-like qualities.
string number_store;
//Not expecting a number over 30,000.
shortint length_of_number = 0;
//Since above is short....
shortint count = 0;
//Using getline since it's preferred over cin
getline( cin, number_store);
length_of_number = number_store.size();
//Checking that we have the right details.
cout << "Number: " << number_store << endl;
cout << "Count: " << count << endl;
cout << "Length of String: " << length_of_number << endl;
number_store = drop_zeros(length_of_number, number_store);
length_of_number = number_store.size();
cout << "Number storage: " << number_store << endl;
num_to_words(number_store, length_of_number);
//Reset!
// //Even though we don't need it, lets use it for completeness!
return 0;
}
That should be good enough, it makes use of the things you should have learned and a few extras that you should have covered in your own time. (Who does homework anyway).
For loops are not recommended with variable lengths.
Switch statements are recommended if you're not doing comparisons. If anyone wants to chip in with some tidying up ideas I'd welcome them.
Edit:
Functions have been added to clean up the thing, passing by the safer const refs of course (although this program is not `that` large, it's good practice.
The program successfully strips leading zeros and converts numbers to words from the end users perspective anyway.
My advice to you in the future is to make your variable names far more descriptive. Instead of i or j, try counting_position_in_array.
Yeah. It's much longer, but it's also much clearer as it's not always obvious what you're counting.