Creating a program using 'While'

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 10
Reputation: WillMcc is an unknown quantity at this point 
Solved Threads: 0
WillMcc WillMcc is offline Offline
Newbie Poster

Creating a program using 'While'

 
0
  #1
Jun 25th, 2008
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.

  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 ==========
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: Creating a program using 'While'

 
0
  #2
Jun 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 9
Reputation: rocky2008 is an unknown quantity at this point 
Solved Threads: 0
rocky2008 rocky2008 is offline Offline
Newbie Poster

Re: Creating a program using 'While'

 
0
  #3
Jun 26th, 2008
In the 10th line of your coding,
cout should be followed by "<<" and not ">>"
Rectify this and run the program.. The program will work..
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: Creating a program using 'While'

 
0
  #4
Jun 26th, 2008
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:

  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"
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 25
Reputation: Shaun32887 is an unknown quantity at this point 
Solved Threads: 2
Shaun32887 Shaun32887 is offline Offline
Light Poster

Re: Creating a program using 'While'

 
0
  #5
Jun 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: Creating a program using 'While'

 
0
  #6
Jun 26th, 2008
Originally Posted by Shaun32887 View Post
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.
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 25
Reputation: Shaun32887 is an unknown quantity at this point 
Solved Threads: 2
Shaun32887 Shaun32887 is offline Offline
Light Poster

Re: Creating a program using 'While'

 
0
  #7
Jun 26th, 2008
Originally Posted by Black Magic View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: Creating a program using 'While'

 
0
  #8
Jun 26th, 2008
Oh sorry about that. You are correct.

All you have to change is:

  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
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 25
Reputation: Shaun32887 is an unknown quantity at this point 
Solved Threads: 2
Shaun32887 Shaun32887 is offline Offline
Light Poster

Re: Creating a program using 'While'

 
0
  #9
Jun 26th, 2008
I think it would be enough to do
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC