Help Fixing Program

Hello, I am very new to the world of coding and I am trying to write a program that prompts the user to input five decimal numbers. The program should then add the five decimal numbers, convert the sum to the nearest integer, and print the result.
I am using Visual Studio 2015.
This is what I have right now and it doesn't work at all:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
#include <string>

namespace std
{ int main()
    {
        double Decimal1;
        double Decimal2;
        double Decimal3;
        double Decimal4;
        double Decimal5;
        cout << "Enter 5 decimals, pressing enter in between each decimal: ";
        cin >> Decimal1 >> Decimal2 >> Decimal3 >> Decimal4 >> Decimal5;
        cout << Decimal1 + Decimal2 + Decimal3 + Decimal4 + Decimal5;
        return 0;
    }
}
The main function must not be put inside the std namespace (you shouldn't really put anything there).
Everything in the code seems to have a problem with it and im not quite sure how to fix it; I moved the namespace std.
Picture of the code:
https://gyazo.com/b440abc8d88c9b0d56ca257aacf671be
Last edited on
What do you mean "it doesn't work at all"? Please be specific. Compile errors? run-time error? or what?

Other than the fact that you don't convert the sum to the nearest integer, it works fine for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{       double Decimal1;
        double Decimal2;
        double Decimal3;
        double Decimal4;
        double Decimal5;
        cout << "Enter 5 decimals, pressing enter in between each decimal: ";
        cin >> Decimal1 >> Decimal2 >> Decimal3 >> Decimal4 >> Decimal5;
        cout << Decimal1 + Decimal2 + Decimal3 + Decimal4 + Decimal5;
        system ("pause");
        return 0;    
}


Note the use of using namespace std;. When I tried to include your main inside a namespace std declaration as you had it, it caused a linker error.

okay, lets fix your code...

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{
      int  Decimal1, Decimal2, Decimal3, Decimal4, Decimal5 ;

cout << "Enter 5 decimals, pressing enter between each decimal:" << endl;
cin >> Decimal1 >> Decimal2 >> Decimal3 >> Decimal4 >> Decimal5;
cout << Decimal1 + Decimal2 + Decimal3 + Decimal4 + Decimal5;
    return 0;
}


That should work... but it's not really practical... You have 5 separate variables to add 5 numbers. Remembering when I started looking at C++, I judge you to be about 2 days... I did a program very simular to this back then. If you spread your program out a little more, it would look more like this...

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

using namespace std;

int main()
{
      int  Decimal1, Decimal2, Decimal3, Decimal4, Decimal5 ;

cout << "Enter 5 decimals, pressing enter between each decimal:" << endl;
cout << "Decimal 1 - ";
cin >> Decimal1;
cout << "Decimal 2 - ";
cin >> Decimal2;
cout << "Decimal 3 - ";
cin >> Decimal3;
cout << "Decimal 4 - ";
cin >> Decimal4;
cout << "Decimal 5 - ";
cin >> Decimal5;
cout << "Total is "<< Decimal1 + Decimal2 + Decimal3 + Decimal4 + Decimal5;
    return 0;
} 


However, there are easier ways.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{

cout << "Enter 5 decimals, pressing enter between each decimal:" << endl; // print message
int decimal, total=0;                            // Declare two variables @ zero   
for (int i=0; i<5 ;i++) {                      //Count to 5  ( zero to 4) with a for loop
        cout << "Please enter decimal then press enter  ";    // Ask nicely for input
        cin >> decimal;                         // Get the value you want                  
        total+=decimal;        // adds the total every time they put in a number
}           // makes the for loop add one and repeat as long as it's less than 5
cout << "Your total is " <<  total << endl;        // Print your result
    return 0;              // End main 


Don't get discouraged, Brighter days are in your future. I hopes that helps...
Last edited on
Topic archived. No new replies allowed.