SO I am trying to write a program in C++ that take in Grades 1-5 and how many cans they collect. This program needs to add the cans they collect then print them. I am very new to C++ and I am better at writing Pseudocode than actual C++ languange so bear with what I have so far.

#include <iostream>

using namespace std;

int main()
{
int grade[5];
int numberofcans;

print header ();

while (grade <1; grade>5;)
{ cout<<”Invalid data”<<;}

int i;

For (grade = i[1], grade++)
{cout <<grade 1 has collected”,” cans”<<;
}
}

My teacher gave me some template to go by but it is confusing me too much so I am trying to it my way. Am I close at all??

This is the template I have:
(it's in psuedocode)

int main()
{ 
/* declaration and initialization
  delare variables to hold number of cans for each grade.  This should be 
  an array (cans).  
  delcare any other variables.
  


  print_header ();
  read record
  while this is not last record  
  {
    is_valid  = check_record (grade, collected)
	if (is_valid)
	   print data and process record
	else
	   print data and an invalid message
	 
    read record
  }
  print_header_per_grade ();
  print_number_of_cans  (cans); 
  return (0) 
}

I'm in a hurry so I may have some grammar errors or use whatever terminology came to me at the time :-)

The first thing I noticed was that your while loop didn't do exactly what you want.

while (grade <1; grade>5;)

A while loop could only test for one thing, and it needs to be written correctly. The correct syntax is:

while (grade < THIS || grade > THAT)
      {
          // statements
     }

Notice that the OR logical operator (two verticle bars - shift + backslash) is used to test for two conditions. Only one needs to be true in order for the test to pass.

What you have inside your loop is also a little off...

// assuming you fix the test first...
     while (grade <1; grade>5;)
      {
           cout<<”Invalid data”<<;
      }

If you pass the test, then you have an infinite loop. If grade isn't changed inside your while loop, then the next time your loop tests for conditions, it'll always pass. What you need to do is include something inside your while loop that changes your 'grade' variable ... something like this (written in psuedocode).

while (Test Goes Here)
     {
          cout << "Invalid input.  Please enter another number.";

          // ask user for the new grade
     }

That takes care of your loop. You're for-loop could also use some cleaning...

For (grade = i[1], grade++)
     {
          cout <<grade 1 has collected”,” cans”<<;
     }

The proper format for a for-loop is as follows:

For (initialization; test; update;)
     {
           // statements
     }

     // for example
     For (int i = 0; i < 5; i++)
     {
          cout << "This is iteration #" << i+1;
     }

The first part of a for-loop is an initialization (typically of your looping/counting variable). The second is the test, in your case it's probably something like: countVariable < numberOfGrades

I hope that cleared things up. No time to spell check or anything, but if you need any clarification, I'd be happy to help you when I get back.

Am I close at all??

May be about 10% of your work is done.

Yes I think I am a little confused on the iteration. For each grade will I have to do the (i=0; i<5; i++;) WIll this add each grade?? So if a 2nd grader has gathered 5 cans and another 2nd grader collect 5 will this iteration add this two different inputs? Maybe I am not understanding what the iteration is actually doing. WIll I have to set the something like numberofcans = numberofcans + i??

Yes I think I am a little confused on the iteration. For each grade will I have to do the (i=0; i<5; i++:P WIll this add each grade?? So if a 2nd grader has gathered 5 cans and another 2nd grader collect 5 will this iteration add this two different inputs? Maybe I am not understanding what the iteration is actually doing. WIll I have to set the something like numberofcans = numberofcans + i??

I think that it would be better to use sentinel controlled looping here.

in it the while can receive one of two sets of numbers. one set of values is a sentinel. all other values are input.

//create input value which can be a sentinel
int inputGradeValue;
cout << "enter a grade from 1 to 5, but -1 to end program"

cin >> inputGradeValue;

while ( inputGradeValue != -1)
{
/* in here you create another integer and receive the number of cans in that integer. then depending on what value is in inputGradVale you know to add it in what element of the grade array. if inputGradeVale is -1 the program of course terminates. that should be in the case when there are no graders left to bring you any cans.

*/

/*receive now a new inputGradeValue which can of course be -1 in which case the while loop terminates or it can be another value from 1 to 5 which is another grader with more cans. the loop will continue for graders until you type in -1.
*/
cin >> inputGradeValue; //loop termnate if -1
} //end the while loop

-1 is of course the sentinel. please feel free to ask for more help on this loop if you need to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.