The basic purpose of the program is for the user to enter 10 numbers, then the program to add them all up as they are entered until it gets to 10 numbers entered which at that point it divides total by 10.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
int counter, grade, total, average;

total = 0;
counter = 1;

while (counter <= 10) {
	cout>> "Enter Number: ";
		cin>>grade;
		total = total + grade;
		counter = counter + 1;
}
average = total / 10;
cout << " The average of the numbers entered is: " << average << endl;

return 0;
}

here is the error message:


------ Build started: Project: Usiing while, Configuration: Debug Win32 ------
Compiling...
Usiing while.cpp
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1021) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1021) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1021) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1021) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1014) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1014) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1014) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1014) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1007) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1007) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1007) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1007) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1000) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1000) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1000) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(1000) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(975) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(975) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(975) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(975) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(934) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(934) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(934) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\istream(934) : see declaration of 'std::operator >>'
.\Usiing while.cpp(16) : error C2676: binary '>>' : 'std::ostream' does not define this operator or a conversion to a type acceptable to the predefined operator
Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Usiing while\Usiing while\Debug\BuildLog.htm"
Usiing while - 25 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

In the 10th line of your coding,
cout should be followed by "<<" and not ">>"
Rectify this and run the program.. The program will work..

Your Program should work but a few things could be shortened.

For example:
counter = counter + 1; .. counter++;
total = total + grade; .. total += grade;

I'm not demanding you to change these just trying to help :)

Improved version:

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
int counter = 1, grade = 0, total = 0, average = 0;


do
{
    cout << "Enter Number :";
    cin >> grade;
    
    total += grade;
    counter++;
    
    cout << endl;
}

while(counter <= 10);

average = total / 10;

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

cin.get(); cin.get();

return 0;
}

And btw at this moment in time you don't need #include "stdafx.h"

Won't there also be an issue with dividing int total by 10? Something should be a double, yeah?

Won't there also be an issue with dividing int total by 10? Something should be a double, yeah?

Well the program atm is fine. But if the user entered a decimal or a higher number than 65,000 (and something) then there would be an issue.

Well the program atm is fine. But if the user entered a decimal or a higher number than 65,000 (and something) then there would be an issue.

What if the sum total of the ten entered values is a number like 42? 42 and 10 are both of type int, so won't basic / division return an int as well? 42/10 would return 4, not 4.2

I think.

Oh sorry about that. You are correct.

All you have to change is:

int counter = 1, grade = 0, total = 0, average = 0;

// to

int counter = 1;
double grade = 0.0, total = 0.0, average = 0.0;

I think it would be enough to do

average = total / 10.0;

Changing everything to doubles is probably a better idea though.

This article has been dead for over six months. Start a new discussion instead.