Problem with Program

So I have a program in class that I believe in my mind everything is correct but I'm getting a bunch of errors and I'm not sure why...


This is the format: Write a program that asks the user to enter 5 integers and store them in an array. Then multiply each integer by 2 and put the answer back into the array. Finally, report the ending values back to the user.

Your program must:

Use a Loop structure to ask the user for the integers and store them in the array.
Use another Loop structure to multiply each integer by 2 and put the answer back into the array.
Use a third Loop structure to report the ending values back to the user

[code]
Put the code you need help with here.
/*
Author: Jude Cerra
Program Description: Make a program that asks the user to enter 5 numbers, then multiply each number by 2, reporting the values to the user
*/

#include <iostream>
#include <iomanip>

using namespace std;
int main()
{
int numArray[5];

cout << "Please enter 5 integers when prompted. " << endl;
for (int i = 0; i < 5; i++)
{
cout << "Enter Integer " << i + 1 << " : ";
cin << numArray[i];
//cout << endl;

}

for (int i = 0; i < 5; i++)
{
numArray[i] = numArray [i] * 2;
}

for (int i = 0; i < 5; i++)
{
cout << endl << " Result " << i + 1 << " : " << numArray[i];
}
cout << endl;

result 0;
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int numArray[5];

    cout << "Please enter 5 integers when prompted. " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "Enter Integer " << i + 1 << " : ";
        // cin << numArray[i]; // *** 
        cin >> numArray[i] ;
    }

    for (int i = 0; i < 5; i++)
    {
        numArray[i] = numArray [i] * 2;
    }

    for (int i = 0; i < 5; i++)
    {
        cout << endl << " Result " << i + 1 << " : " << numArray[i];
    }
    cout << endl;

    // result 0; // *** return 0?
}



Or:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    const int N = 5 ; // avoid magic numbers
    int numArray[N]{}; // initialise to all zeroes (input may fail)

    std::cout << "Please enter " << N << " integers when prompted.\n" ;
    for (int i = 0; i < N; i++)
    {
        std::cout << "Enter Integer " << i+1 << " : ";
        std::cin >> numArray[i] ;
    }

    for (int i = 0; i < N; i++) numArray[i] *= 2;
    // or( for int& v : numArray ) v *= 2 ;

    std::cout << '\n' ;
    for (int i = 0; i < N; i++)
        std::cout << "numArray[" << i+1 << "] : " << numArray[i] << '\n' ;
}
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) If you have errors, TELL US WHAT THEY ARE.
Line 18: Your input operator is the wrong direction.
Should be:
 
    cin >> numArray[i] ;


Line 34: Should be:
 
    return 0;  //  Not result 0 


Line 35: You need a closing }

Please learn to use code tags correctly. You have an opening code tag, but apparently no closing code tag. A closing code tag is:
[/code]


Last edited on
Topic archived. No new replies allowed.