Re: issue with "cin"...

8 views
Skip to first unread message
Message has been deleted

Art Werschulz

unread,
Dec 26, 2009, 6:36:53 PM12/26/09
to ppp-p...@googlegroups.com
Hi.

On Dec 25, 2009, at 9:39 PM, pantera wrote:

> Hi,
>
> Say I have 2 vectors to read in values of 2 different kinds. I can
> read in values for the first vector successfully, but when it comes to
> the 2nd vector, it always cannot read in anything. A fragment of codes
> look like this:
>
> for (int i=0; i<n; ++i) {
> cout << "Enter date of cash flow " << i << ": ";
> cin >> tcf;
> tcashflow.push_back(tcf); // HERE: Success in reading in
> }
>
> for (int j=0; j<n; ++j) {
> cout << "Enter value of cash flow " << j << ": ";
> cin >> vcf;
> vcashflow.push_back(vcf); // BUT in this vector, nothing can be read
> in
> }
>
> The output looks like below:
>
> Enter # of cash flows: 4
> Enter date of cash flow 0: 3
> Enter date of cash flow 1: 4
> Enter date of cash flow 2: 3
> Enter date of cash flow 3: 3
> Enter value of cash flow 0: Enter value of cash flow 1: Enter value of
> cash flow 2: Enter value of cash flow 3: Bond price: 4.92424

I'm not having that problem. Program:

#include <std_lib_facilities.h>

int main()
{
vector<int> tcashflow, vcashflow;

cout << "How many cash flows? ";
int n;
cin >> n;
for (int i=0; i<n; ++i) {
int tcf;
cout << "Enter date of cash flow " << i << ": ";
cin >> tcf;
tcashflow.push_back(tcf);
}

cout << "Here are the cash flow dates: ";
for (int i = 0; i < tcashflow.size(); i++)
cout << tcashflow[i] << " ";
cout << endl;

for (int j=0; j<n; ++j) {
int vcf;
cout << "Enter value of cash flow " << j << ": ";
cin >> vcf;
vcashflow.push_back(vcf);
}

cout << "Here are the cash flow values: ";
for (int i = 0; i < vcashflow.size(); i++)
cout << vcashflow[i] << " ";
cout << endl;

}

Execution:

$ foo
How many cash flows? 4
Enter date of cash flow 0: 10
Enter date of cash flow 1: 4
Enter date of cash flow 2: 9
Enter date of cash flow 3: 11
Here are the cash flow dates: 10 4 9 11
Enter value of cash flow 0: 3
Enter value of cash flow 1: 5
Enter value of cash flow 2: 7
Enter value of cash flow 3: 0
Here are the cash flow values: 3 5 7 0

I've run this on Mac OS X, with g++ --version reporting
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
and on Fedora 12 Linux, with g++ --version reporting
g++ (GCC) 4.4.2 20091027 (Red Hat 4.4.2-7)

Which compiler are you using? Which operating system?

Art Werschulz
207 Stoughton Avenue, Cranford NJ 07016-2838
(908) 272-1146

pantera

unread,
Dec 26, 2009, 8:43:08 PM12/26/09
to PPP-public
Hi Art,

Thanks a lot for your time. I've run it on Mac OS X, with i686-apple-
darwin10-gcc-4.2.1. It's running perfectly well now. After restarting
the TextWrangler & Terminal, and also logging off and back in, it's
somehow working just fine. I didn't change anything at all in the
program.

// Bond pricing exercise
#include "std_lib_facilities.h"

int main()
{
double bondprice = 0;
double n = 0; // number of cash flows
double vcf = 0; // value of cash flows
double tcf = 0; // date of cash flows
vector <double> rzero; // zero rates
vector <double> disc; // discounted value
vector <double> vcashflow; // vector of value of cash flows
vector <double> tcashflow; // vector of date of cash flows


cout << "Enter # of cash flows: ";
cin >> n;

for (int i=0; i<n; ++i) {

cout << "Enter date of cash flow " << i+1 << ": ";
cin >> tcf;
tcashflow.push_back(tcf);
}

for (int j=0; j<n; ++j) {

cout << "Enter value of cash flow " << j+1 << ": ";
cin >> vcf;
vcashflow.push_back(vcf);
}

for (int k=0; k<n; ++k) {
rzero.push_back(0.0525+(1.0/200.0)*log(1+2*tcashflow[k])); // zero
rate curves
}

for (int i=0; i<n; ++i) {

disc.push_back(exp(-tcashflow[i]*rzero[i]));
bondprice = bondprice + vcashflow[i]*disc[i];
}
cout << "Bond price: " << bondprice << endl;

Reply all
Reply to author
Forward
0 new messages