English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
0

The instructions for this problem are:
" Write a complete program that reads in 100 double values from standard input (keyboard) into a vector. Then output all 100 values to standard output (terminal) in the same order they were read in with one space between each value."

I do not need to worry about prompting the user with instructions.

The code I have so far is:

#include
#include

int main()
{
vector values(99);

while( int i = 0 ; i <= 99 ; i++ )
{
cin << vector[i];
}

for ( int i = 1 ; i < values.size()-1 ; i++ )
{
cout << vector[i] << " " ;
}

return 0;
}

Could someone please tell me what I am doing wrong? Thanks in advance.

2007-03-17 17:45:32 · 2 answers · asked by fortune_snookies 2 in Computers & Internet Programming & Design

2 answers

Here is correct code based on your code:

#include
#include

using namespace std;
const static int vec_sz = 100;
vector values(vec_sz);

int main( int ac, char * av[] )
{

for( int i = 0 ; i < vec_sz ; i++ )
{
cin >> values[i];
}

for ( int i = 0 ; i < vec_sz ; i++ )
{
cout << values[i] << " " ;
}
return 0;
}

2007-03-17 17:59:47 · answer #1 · answered by alakit013 5 · 0 0

cin >> values[i]; (not <<)

and cout << values[i], not vector[i]

if the cin doesn't work (vector::operator[] returns a reference so it should in theory) use a temporary double variable first then assign that into the vector.

2007-03-18 00:53:40 · answer #2 · answered by undercoloteal 3 · 0 0

fedest.com, questions and answers