Need Help with First Homework Assignment

Hello,

I need some help, I'm using DEV C++ and trying to complete making my first program as my homework assignment. It involves calculating the average of 6 variables listed and then dividing them by 6. For some reason when I try to compile it stops halfway through and I get one error message. Can someone please take a look at my code and tell me what I did wrong? I've been trying to figure it out for hours :|


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
30
31
  #include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

int 10,8,11,7,23,56,6;
float ave

cout <<"Average of the 6 Integers"<< endl;
cout <<"Enter the six numbers"<<endl;

cin >> 10;
cin >> 8;
cin >> 11;
cin >> 7;
cin >> 23;
cin >> 56;

ave= 10 + 8 + 11 + 7 + 23 + 56 / 6;

cout << "The average of the six numbers is: " <<ave <<endl;

return 0;

system("PAUSE");
return EXIT_SUCCESS;
}
Line 9: You are using variable declaration syntax but you put a bunch of integer literals instead of variable names. int firstNum; //etc.
Line 10: You need a semicolon at the end of the line.
Lines 15-20: You are attempting to read from standard input to an integer literal. Use variables instead. cin >> firstNum; //etc.
Line 22: Does not calculate the correct number because of order of operations. The division operation has a higher precedence than addition so it will occur first. It is the same as saying
10+8+11+7+23+(56 / 6);

Line 22: The literals should be replaced with the variables you read from the user on lines 15-20.
Line 22: Divide by 6.0 instead of 6, otherwise you will perform integer division which you don't want.
Lines 28 and 29 cannot ever execute since they occur after the return 0; statement on line 26.
Thank You for your Prompt reply. Would you be able to fix my code and show me exactly what you are talking about, I fixed lines 28,29 and line 22 with line 10 and included my updated version below. However, lines 9, 15-20 and line 22 I'm having difficulty knowing exactly what to do, as this is my first program.

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

int firstnum,secondnum,thirdnum,fourthnum,fifthnum,sixthnum,divisor;
float ave;

cout <<"Average of the 6 Integers"<< endl;
cout <<"Enter the six numbers"<<endl;

cin >> 10;
cin >> 8;
cin >> 11;
cin >> 7;
cin >> 23;
cin >> 56;

avg= 10 + 8 + 11 + 7 + 23 + 56;
avg= 115/6;


cout << "The average of the six numbers is: " <<ave <<endl;
system("PAUSE");
return EXIT_SUCCESS;
return 0;


}
Last edited on
Ask yourself the question: Do you want to use the same 6 numbers every time your program is run, or do you want the user to input 6 numbers each time? Right now it looks like you're trying to do both, syntax errors aside.

As for your actual errors, you're still doing what booradley60 said was bad, you can't cin >> into a literal, it has to be a variable you put console input into.
cin >> my_variable;

If you want the user to be entering numbers themselves, the only actual number literal you want in your program is 6 for the number of element. Everything else would be whatever the user decides.
Last edited on
You're heading in the direction of arrays, since typing firstnum, secondnum, thirdnum, etc. quickly grows tedious. However, since you aren't there yet, we'll stick to basics. Check out this code that I've commented. Try running it. The comments will hopefully help you understand what's going on.
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    //you've asked the compiler to set aside space for 6 integers, and
    //you've given the names by which you'd like to refer to them.
    //I removed divisor. We're going to 'hard-code' that to 6.
    int firstnum,secondnum,thirdnum,fourthnum,fifthnum,sixthnum;

    //you've asked the compiler to set aside space for 1 floating point
    //value, and given it a name by which you'll refer to it
    float ave;

    //print some info to the screen
    cout <<"Average of the 6 Integers"<< endl;
    cout <<"Enter the six numbers"<<endl;

    //read user input...
    //we've set aside space to hold some values. Let's put that
    //space to use!
    cin >> firstnum;
    cin >> secondnum;
    cin >> thirdnum;
    cin >> fourthnum;
    cin >> fifthnum;
    cin >> sixthnum;

    //now those spaces have numbers in them that the user has provided!
    //This is a much more useful program already, since the user
    //can put in whichever 6 numbers they choose.
    //
    //To average them, we take their sum and divide it by 6
    //Note, we're going to store that calculation's result in
    //ave, which we declared space for earlier! Also, we'll
    //use parentheses to enforce proper order of operations.
    ave = (firstnum + secondnum + thirdnum + fourthnum + fifthnum + sixthnum) / 6.0;

    cout << "The average of the six numbers is: " <<ave <<endl;
    system("PAUSE");
    return EXIT_SUCCESS; //we only need one of these return statements
    return 0;
}
Last edited on
Topic archived. No new replies allowed.