943,922 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3694
  • C++ RSS
Jun 25th, 2008
-1

Creating a program using 'While'

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int counter, grade, total, average;
  10.  
  11. total = 0;
  12. counter = 1;
  13.  
  14. while (counter <= 10) {
  15. cout>> "Enter Number: ";
  16. cin>>grade;
  17. total = total + grade;
  18. counter = counter + 1;
  19. }
  20. average = total / 10;
  21. cout << " The average of the numbers entered is: " << average << endl;
  22.  
  23. return 0;
  24. }

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 ==========
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
WillMcc is offline Offline
10 posts
since Jun 2008
Jun 25th, 2008
0

Re: Creating a program using 'While'

cout is followed by "<<" and not ">>"..

[EDIT]Check your cout in the while loop
Last edited by joshmo; Jun 25th, 2008 at 11:56 pm.
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Jun 26th, 2008
0

Re: Creating a program using 'While'

In the 10th line of your coding,
cout should be followed by "<<" and not ">>"
Rectify this and run the program.. The program will work..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rocky2008 is offline Offline
9 posts
since Jun 2008
Jun 26th, 2008
0

Re: Creating a program using 'While'

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:

C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int counter = 1, grade = 0, total = 0, average = 0;
  9.  
  10.  
  11. do
  12. {
  13. cout << "Enter Number :";
  14. cin >> grade;
  15.  
  16. total += grade;
  17. counter++;
  18.  
  19. cout << endl;
  20. }
  21.  
  22. while(counter <= 10);
  23.  
  24. average = total / 10;
  25.  
  26. cout << "The average of the numbers entered is: " << average << endl;
  27.  
  28. cin.get(); cin.get();
  29.  
  30. return 0;
  31. }

And btw at this moment in time you don't need #include "stdafx.h"
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Jun 26th, 2008
0

Re: Creating a program using 'While'

Won't there also be an issue with dividing int total by 10? Something should be a double, yeah?
Last edited by Shaun32887; Jun 26th, 2008 at 4:49 pm.
Reputation Points: 10
Solved Threads: 2
Light Poster
Shaun32887 is offline Offline
25 posts
since Jun 2008
Jun 26th, 2008
0

Re: Creating a program using 'While'

Click to Expand / Collapse  Quote originally posted by Shaun32887 ...
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.
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Jun 26th, 2008
0

Re: Creating a program using 'While'

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.
Last edited by Shaun32887; Jun 26th, 2008 at 5:10 pm.
Reputation Points: 10
Solved Threads: 2
Light Poster
Shaun32887 is offline Offline
25 posts
since Jun 2008
Jun 26th, 2008
0

Re: Creating a program using 'While'

Oh sorry about that. You are correct.

All you have to change is:

C++ Syntax (Toggle Plain Text)
  1. int counter = 1, grade = 0, total = 0, average = 0;
  2.  
  3. // to
  4.  
  5. int counter = 1;
  6. double grade = 0.0, total = 0.0, average = 0.0;
Last edited by Black Magic; Jun 26th, 2008 at 5:02 pm. Reason: 0 -> 0.0
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Jun 26th, 2008
0

Re: Creating a program using 'While'

I think it would be enough to do
C++ Syntax (Toggle Plain Text)
  1. average = total / 10.0;

Changing everything to doubles is probably a better idea though.
Last edited by Shaun32887; Jun 26th, 2008 at 5:10 pm.
Reputation Points: 10
Solved Threads: 2
Light Poster
Shaun32887 is offline Offline
25 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Selecting random word. Is this right?
Next Thread in C++ Forum Timeline: how to embed to softwares?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC