Help with Program

Hey guys,

I am having trouble calling an array...here is the code. What I'm needing help with is how to have the value of ttc (ten thousand count) return the value of it's respective partner in CharAmt.

Thanks for the help!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <iomanip>
#include <time.h>

using namespace std;

void LoadCharAmt();

string CharAmt[30], test;
char fname[30], lname[81];
double amt;
int ttc, tc, hc, tenc=1;
int main ()

{
    struct tm* ptrTime;
    time_t t=time(0);
    ptrTime=localtime(&t);
    
    system("CLS");
    
    cout << "\nThe Current Date is: ";
    cout<<ptrTime->tm_year+1900;
    cout<<ptrTime->tm_mon+1<< "/"<<ptrTime->tm_mday<<"/";
    cout<<ptrTime->tm_year+1900;
    
    system("Pause");
    
    cout << "        * * * * CHECK WRITER * * * * ";
    cout <<endl << "   Enter First Name: ";
    cin >> fname;
    cout <<endl << "    Enter Last Name: ";
    cin >> lname;
    cout << endl << "Enter the Amount of the Check:";
    cout << endl << "(up to 99,999.99): ";
    cin >>amt;
    
    if (amt >= 20000)
        while (amt > 10000)
        {
            ttc++;
            amt-=10000;
        }
    
    if (amt >= 1000)
        while (amt >=1000)
        {
            tc++;
            amt-=1000;
        }
    
    if (amt >= 100)
        while (amt >= 100)
        {
            hc++;
            amt-=100;
        }
    if (amt >=20)
        while (amt >=20)
        {
            tenc++;
            amt-=10;
        }
    void LoadCharAmt();
    cout << CharAmt[23];
    

system("Pause");
return 0;
}
// Functions

void LoadCharAmt()
{
    CharAmt[1]="One";
    CharAmt[2]="Two";
    CharAmt[3]="Three";
    CharAmt[4]="Four";
    CharAmt[5]="Five";
    CharAmt[6]="Six";
    CharAmt[7]="Seven";
    CharAmt[8]="Eight";
    CharAmt[9]="Nine";
    CharAmt[10]="Ten";
    CharAmt[11]="Eleven";
    CharAmt[12]="Twelve";
    CharAmt[13]="Thirteen";
    CharAmt[14]="Fourteen";
    CharAmt[15]="Fifteen";
    CharAmt[16]="Sixteen";
    CharAmt[17]="Seventeen";
    CharAmt[18]="Eighteen";
    CharAmt[19]="Nineteen";
    CharAmt[20]="Twenty";
    CharAmt[21]="Ten";
    CharAmt[22]="Twenty";
    CharAmt[23]="Thirty";
    CharAmt[24]="Forty";
    CharAmt[25]="Fifty";
    CharAmt[26]="Sixty";
    CharAmt[27]="Seventy";
    CharAmt[28]="Eighty";
    CharAmt[29]="Ninety";
}
Last edited on
1. What is a "String Function"? There is only one type of function: a function. Sometimes they return values or take parameters.
2. How do you "call an array"? Arrays are not functions last time I checked.
3. How can an int "return the value of it's respective partner in CharAmt"? Ints are not functions last time I checked.


It is not very clear what you want. :\
Last edited on
Sorry about the wording. I know it's not correct. Basically when say var ttc is 29. I want to get CharAmt[29] (Ninety) out of the function...does that make any more sense?
line 64 void LoadCharAmt();

You shouldn't have "void" there

Last edited on
His function returns nothing, thus it should have the keyword void before the function name.

@derekvw: have you considered CharAmt[ttc]?
I have. Krahl's idea worked perfectly. As soon as I took the void out, everything worked out fine.
He already has that in line 7. He has already declared the function. I'm talking about line 64.
Ah yes, I saw your post before you edited in the line number. Sorry.
Ok...one last thing...

When I loop back around (Do another) it skips first name and only lets me input last name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <iomanip>
#include <time.h>
#include <sstream>
#include <string>

using namespace std;

void LoadCharAmt();
void dollarFormat(string &);

string CharAmt[30], fname, lname, fullname;
char another;

int main ()
    {
        do{
        fname.clear(); 
        lname.clear(); 
        fullname.clear();
        double amt=0, amt1;
        int ttc=0, tc=0, hc=0, tenc=1, int_amt=0, length=0;
        struct tm* ptrTime;
        time_t t=time(0);
        ptrTime=localtime(&t);
        
        system("CLS");
        
        cout << "        * * * * CHECK WRITER * * * * ";
        cout <<endl << "   Enter First Name: ";
        getline (cin, fname);
        cout <<endl << "    Enter Last Name: ";
        getline (cin, lname);
        cout << endl << "Enter the Amount of the Check:";
        cout << endl << "(up to 99,999.99): ";
        cin >>amt;
        amt1=amt;
        
        //Add fname and lname
        fullname = fname + " " + lname;
        
        //Get String Length
        length=strlen(fullname.c_str());
        //Convert Double to String
        string s;
        stringstream ss, ss2;
        
        ss << amt1;
        ss>>s;
        
        //Comma (Couldn't get dollarFormat to work???)
        for ( short i = s.length() - 3; i > 0; i -= 3 ) 
        {
            s.insert(i, ",");
        }
        
        //Break Up
        if (amt >= 20000)
            while (amt > 10000)
            {
                ttc++;
                amt-=10000;
            }
        
        if (amt >= 1000)
            while (amt >=1000)
            {
                tc++;
                amt-=1000;
            }
        
        if (amt >= 100)
            while (amt >= 100)
            {
                hc++;
                amt-=100;
            }
        if (amt >=20)
            while (amt >=10)
            {
                tenc++;
                amt-=10;
            }
        
        LoadCharAmt();
        
    //Output
    
    cout << endl << endl;
    cout << setw(55) << "Date: ";
    cout<<ptrTime->tm_mon+1<< "/"<<ptrTime->tm_mday<<"/";
    cout<<ptrTime->tm_year+1900;
    cout << endl << " PAY TO THE";
    cout << endl << " ORDER OF: " << fullname << setw(39-length) <<"$"<< s;
    cout << endl << "______________________________________________________________";
    cout << endl; 
    // Ten Thousands
        if (ttc>0)
        cout << CharAmt[20+ttc];
    
    //Thousands
        if (tc>0)
        cout << " " << CharAmt[tc] << " Thousand";
        else (cout << " Thousand");

        
    //Hundreds 
        if (hc>0)
        cout << " " << CharAmt[hc] << " Hundred";
        
    //Ten
        if (tenc>1)
        cout << " " << CharAmt[19+tenc];
    
    //Everything Else
        
        int_amt = static_cast<int>(amt);
        cout << " " << CharAmt[int_amt];
        cout << endl;
    
    cout << endl << "______________________________________________________________";
    cout << endl;
    
    cout << " Do another? (Y or N): ";
    cin>>another;
}while(another != 'N' && another !='n');
    


system("Pause");
return 0;
}
// Functions

void LoadCharAmt()
{
    CharAmt[1]="One";
    CharAmt[2]="Two";
    CharAmt[3]="Three";
    CharAmt[4]="Four";
    CharAmt[5]="Five";
    CharAmt[6]="Six";
    CharAmt[7]="Seven";
    CharAmt[8]="Eight";
    CharAmt[9]="Nine";
    CharAmt[10]="Ten";
    CharAmt[11]="Eleven";
    CharAmt[12]="Twelve";
    CharAmt[13]="Thirteen";
    CharAmt[14]="Fourteen";
    CharAmt[15]="Fifteen";
    CharAmt[16]="Sixteen";
    CharAmt[17]="Seventeen";
    CharAmt[18]="Eighteen";
    CharAmt[19]="Nineteen";
    CharAmt[20]="Twenty";
    CharAmt[21]="Ten";
    CharAmt[22]="Twenty";
    CharAmt[23]="Thirty";
    CharAmt[24]="Forty";
    CharAmt[25]="Fifty";
    CharAmt[26]="Sixty";
    CharAmt[27]="Seventy";
    CharAmt[28]="Eighty";
    CharAmt[29]="Ninety";
}

void dollarFormat (string &currency)
{
    int dp;
    
    dp=currency.find('.');
    if (dp>3)
    {
        for (int x = dp-3; x>0; x-=3)
            currency.insert(x, ",");
    }
    currency.insert(0,"$"); 
}
There is probably an unused newline character in the stream. Try putting cin.ignore(unsigned(-1), '\n') before you ask for the first name.
I agree with L B. There is probably a newline in the stream. The problem with just adding cin.ignore(80,'\n'); somewhere before you ask for the first name will force a pause the first time you loop it so a rather crude but simple way of getting around the problem is to add the following code before the do loop within int main (after line 15 will do)
 
int count = 0;


then add the following code within the do loop (line 28 or something):
1
2
if(count > 0)  cin.ignore(80,'\n');
count++;


This ensures the cin.ignore does not take effect on the first loop. This is a quick fix I'm not sure if it will cause any other issues.

EDIT: Also line 59 doesn't match with line 66
Last edited on
Topic archived. No new replies allowed.