### Thread: Help with array saving

Page 1 of 2 12 Last
1. #### Help with array saving

Hey all i am in need of some help with getting some data into an array and then displaying it after its all full..
Code:
void calcMonPayment(float, float, int, float&);
int noMonths[3];
int theMonths = 12;
int theCount=0;;

displayLoanSchedule(price, tradeIn, downPayment, loanAmt, annualIntRate);
monIntRate =  annualIntRate  / 12.0;

do while (theCount <= 3)
{
calcMonPayment(price, monIntRate, theMonths, monPayment);
noMonths[theCount] = monPayment;
theCount = theCount + 1;
theMonths = theMonths + 12;
};

// after 24/36/48/60 is done.. display the totals
// 24 month price: noMonths[0];
// 36 month price: noMonths[1];
// 48 month price: noMonths[2];
// 60 month price: noMonths[3];

void calcMonPayment(float loan, float monIntRate, int noMonths, float& monPayment)
{
monPayment = (loan * monIntRate) / (1.0 - (1 + monIntRate) - noMonths);
return  ;
}
The months are 24/36/48 & 60.

How can i loop to fill the array up? I'm lost as to how i can do this... :(

Any help would be wonderful!

David
Last edited by StealthRT; December 5th, 2008 at 05:11 PM.
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2008
Posts
84
Rep Power
71
Well, if you're intending to populate the array "noMonths" in the function "calcMonPayment", the first thing you're going to need to do is have the noMonths parameter be an array. Right now, your function definition has it as an int.

And secondly, is that actual code that you're trying to run, or pseudo-code for illustration purposes? If it's actual code, I'd scrap it and start over, perhaps with input from posters here after explaining more generally what your intention is.
3. Originally Posted by drgoodtrips
Well, if you're intending to populate the array "noMonths" in the function "calcMonPayment", the first thing you're going to need to do is have the noMonths parameter be an array. Right now, your function definition has it as an int.

And secondly, is that actual code that you're trying to run, or pseudo-code for illustration purposes? If it's actual code, I'd scrap it and start over, perhaps with input from posters here after explaining more generally what your intention is.
Thanks for the reply drgoodtrips. I have updated my code.
Code:
void calcMonPayment(float, float, int, float&);
float monIntRate;
int noMonths[3];
int theMonths = 12;
int theCount=0;;

displayLoanSchedule(price, tradeIn, downPayment, loanAmt, annualIntRate);
monIntRate =  annualIntRate  / 12.0;

do while (theCount <= 3)
{
calcMonPayment(price, monIntRate, theMonths, monPayment);
noMonths[theCount] = monPayment;
theCount = theCount + 1;
theMonths = theMonths + 12;
}

// after 24/36/48/60 is done.. display the totals
// 24 month price: noMonths[0];
// 36 month price: noMonths[1];
// 48 month price: noMonths[2];
// 60 month price: noMonths[3];

void calcMonPayment(float loan, float monIntRate, int noMonths, float& monPayment)
{
monPayment = (loan * monIntRate) / (1.0 - (1 + monIntRate) - noMonths);
return  ;
}
The part where it says
Code:
// after 24/36/48/60 is done.. display the totals
// 24 month price: noMonths[0];
// 36 month price: noMonths[1];
// 48 month price: noMonths[2];
// 60 month price: noMonths[3];
Is just for illustration purposes.

The error i am getting once i compile it is:
Code:
f:\program.cpp(100): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

Which is this line:
monIntRate = annualIntRate  / 12.0;

and f:\program.cpp(105): warning C4244: '=' : conversion from 'float' to 'int', possible loss of data

Which is this line:
noMonths[theCount] = monPayment;
David
Last edited by StealthRT; December 5th, 2008 at 05:17 PM.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2008
Posts
84
Rep Power
71
Where do your function and variable declarations reside? Are they global? Do they sit inside of a class? Inside main?
5. drgoodtips. They are inside the main. No global's.
Code:
int main()
{
float price;
float downPayment;
float annualIntRate;
float loanAmt;
float monPayment;
float monIntRate;
int noMonths[3];
int theMonths = 12;
int theCount=0;

getPrice(price);
getInterestRate(annualIntRate);
loanAmt = price - downPayment - tradeIn;
displayLoanSchedule(price, tradeIn, downPayment, loanAmt, annualIntRate);

monIntRate = annualIntRate  / 12.0;

do while (theCount <= 3)
{
calcMonPayment(price, monIntRate, theMonths, monPayment);
noMonths[theCount] = monPayment;
cout << "Payment for " << theMonths << setw(12) << monPayment << endl;
theCount = theCount + 1;
theMonths = theMonths + 12;
}
return 0;
}
Last edited by StealthRT; December 5th, 2008 at 05:30 PM.
6. No Profile Picture
I haz teh codez!
Devshed Frequenter (2500 - 2999 posts)

Join Date
Dec 2003
Posts
2,574
Rep Power
2343
c Code:
do while (theCount <= 3)

Never seen a do-while loop like that before. Doesn't even compile for me with gcc. How about
c Code:
do
{
calcMonPayment(price, monIntRate, theMonths, monPayment);
noMonths[theCount] = monPayment;
theCount = theCount + 1;
theMonths = theMonths + 12;
}  while (theCount <= 3);

As far as your errors (which are warnings) go, they're fairly self-explanatory. Change your floats to doubles to get rid of the first one and get increased precision. If you want to store decimals in your noMonths array, then make it an array of doubles.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2008
Posts
84
Rep Power
71
First, those are compiler warnings, not errors. If that's all that happens, the code should execute anyway.

And, for the second one, you're getting a warning because you're setting an integer value equal to a float without casting. As for the first one, I guess the compiler interprets 12.0 as a double. You could eliminate the warning by declaring those variables as doubles, or casting to float in your assignment. There might be something else at play, but I don't know what compiler you're using, what warning flags are on, etc.

Also, generally speaking, there are a number of rather conceptual problems with the code that you've put up there. I won't bother you with them, unless you're interested. I know that when I'm looking for something quick and dirty, I'm usually not interested in lectures on good coding practices ;)
8. Hum.. it looks like the error in the do while loop was causing everything else to crash after it...

Code:
int main()
{
float price;
float downPayment;
float annualIntRate;
float loanAmt;
float monPayment;
float monIntRate;
int noMonths[3];
int theMonths = 24;
int theCount=0;

getPrice(price);
getInterestRate(annualIntRate);
loanAmt = price - downPayment - tradeIn;
displayLoanSchedule(price, tradeIn, downPayment, loanAmt, annualIntRate);

monIntRate = annualIntRate / 12.0;

while (theCount <= 3)
{
calcMonPayment(price, monIntRate, theMonths, monPayment);
noMonths[theCount] = monPayment;
cout << "Payment for " << theMonths << setw(12) << monPayment << endl;
theCount = theCount + 1;
theMonths = theMonths + 12;
}
return 0;
}
But i am getting a BREAK after the return 0;. Seems the values for the array are negative numbers....

Code:
Run-Time Check Failure #2 - Stack around the variable 'noMonths' was corrupted.

Payment for 24 = -0.61
Payment for 36 = -0.41
Payment for 48 = -0.30
Payment for 60 = -0.24
David

I'm using Visual c++ .NET 2003
Last edited by StealthRT; December 5th, 2008 at 05:43 PM.
9. "while (count <= 3)" will attempt to populate a 3-element array with 4 items. That will cause a crash or, at the very least, garfle up some other local variable (in this case, "monIntRate").

Drgoodgosh, do not advise novices to ignore warnings. The program is not likely to "run anyway." Almost all warnings seen by novices are, indeed, errors. They're just not always fatal errors. Warning and error detection should be as severe as it can be set and both should be treated as errors unless the programmer knows very dam' well what he or she is doing. Such is obviously not the case, here.
10. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2008
Posts
84
Rep Power
71
I wasn't advising him to ignore them - just stating that the code would still execute. I only mentioned the distinction because he referred to them as 'errors'.
11. Sizeablegrin: U really need to stop hating on people. It's fine to express your opinion, but don't degrade others for helping. And i think it was a little childish of you to give me -859 Reputation for one of my later posts.

BTW sizeablegrin... I sent u a email a month or so ago stating that i wish for you not to comment on any of my posts anymore, so please do not.

drgoodtrips: Thanks again for your help. I thought that c++ arrays were like VB in the since that they all started with 0. So 0-3 would be 4 inputs. But i understand now that the number u put inside the bracket are the number of elements you want it to hold.

Everything works except i am still getting negitive numbers for the monPayment... :(

David

#### Comments on this post

• sizablegrin disagrees : If you don't want me commenting on or responding to your posts, then don't post them where I can read them, and don't denigrate your responses.
12. No Profile Picture
I haz teh codez!
Devshed Frequenter (2500 - 2999 posts)

Join Date
Dec 2003
Posts
2,574
Rep Power
2343
Suggestion, to go along with Grin's point, is to make your loop a standard for loop.
c Code:
for (; theCount < sizeof(noMonths); ++theCount)

and get rid of
c Code:
theCount = theCount + 1
within the loop.
13. ptr2void: the
Code:
for (; theCount < sizeof(noMonths); ++theCount)
Seems to give me a lot more array numbers than just the 4 that i need.

David
14. Here's a snippet with NO errors or warnings. It does NOT "execute anyway." Well, it tries; result:

Now, if you can't trust "errorless, warningless code," what does that tell you about warnings and errors?
Code:
#include <stdio.h>

void stuffIt (char *p, char c)
{
int i;
for (i = 0; i < 128; ++i)
p [i] = c++;
return;
}

int main (int argc, char* argv [])
{
char theChar = '\0';
char *theString = {"abc"};
stuffIt (theString, theChar);

return 0;
}
Originally Posted by Build Log
------ Rebuild All started: Project: school, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'school', configuration 'Debug|Win32'
Compiling...
useptrs.c
LINK : C:\Documents and Settings\David\My Documents\Visual Studio 2008\Projects\school\Debug\school.exe not found or not built by the last incremental link; performing full link
Embedding manifest...
Build log was saved at "file://c:\Documents and Settings\David\My Documents\Visual Studio 2008\Projects\school\school\Debug\BuildLog.htm"
school - 0 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
15. No Profile Picture
I haz teh codez!
Devshed Frequenter (2500 - 2999 posts)

Join Date
Dec 2003
Posts
2,574
Rep Power
2343
Sorry, should've been:
c Code:
for (; theCount < sizeof(noMonths) / sizeof(noMonths[0]); ++theCount)

#### Comments on this post

• StealthRT agrees
Page 1 of 2 12 Last