So Lost writing this program...

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

Join Date: Jun 2007
Posts: 2
Reputation: reedla is an unknown quantity at this point 
Solved Threads: 0
reedla reedla is offline Offline
Newbie Poster

So Lost writing this program...

 
0
  #1
Jun 21st, 2007
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.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int grade[5];
  8. int numberofcans;
  9.  
  10. print header ();
  11.  
  12. while (grade <1; grade>5;)
  13. { cout<<”Invalid data”<<;}
  14.  
  15. int i;
  16.  
  17. For (grade = i[1], grade++)
  18. {cout <<grade 1 has collected”,” cans”<<;
  19. }
  20. }

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)
  1. int main()
  2. {
  3. /* declaration and initialization
  4.   delare variables to hold number of cans for each grade. This should be
  5.   an array (cans).
  6.   delcare any other variables.
  7.  
  8.  
  9.  
  10.   print_header ();
  11.   read record
  12.   while this is not last record
  13.   {
  14.   is_valid = check_record (grade, collected)
  15. if (is_valid)
  16. print data and process record
  17. else
  18. print data and an invalid message
  19.  
  20.   read record
  21.   }
  22.   print_header_per_grade ();
  23.   print_number_of_cans (cans);
  24.   return (0)
  25. }
  26.  
Last edited by Ancient Dragon; Jun 21st, 2007 at 11:29 pm. Reason: removed excess bbcode and add code tags
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 7
Reputation: eDeloa is an unknown quantity at this point 
Solved Threads: 0
eDeloa eDeloa is offline Offline
Newbie Poster

Re: So Lost writing this program...

 
0
  #2
Jun 22nd, 2007
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.

Originally Posted by reedla View Post
  1.  
  2. 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:

  1.  
  2. while (grade < THIS || grade > THAT)
  3. {
  4. // statements
  5. }

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...

Originally Posted by reedla View Post
  1.  
  2. // assuming you fix the test first...
  3. while (grade <1; grade>5;)
  4. {
  5. cout<<”Invalid data”<<;
  6. }
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).


  1. while (Test Goes Here)
  2. {
  3. cout << "Invalid input. Please enter another number.";
  4.  
  5. // ask user for the new grade
  6. }

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

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

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

  1.  
  2. For (initialization; test; update;)
  3. {
  4. // statements
  5. }
  6.  
  7. // for example
  8. For (int i = 0; i < 5; i++)
  9. {
  10. cout << "This is iteration #" << i+1;
  11. }

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.
Last edited by eDeloa; Jun 22nd, 2007 at 12:43 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: So Lost writing this program...

 
0
  #3
Jun 22nd, 2007
Originally Posted by reedla View Post
Am I close at all??
May be about 10% of your work is done.
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2
Reputation: reedla is an unknown quantity at this point 
Solved Threads: 0
reedla reedla is offline Offline
Newbie Poster

Re: So Lost writing this program...

 
0
  #4
Jun 22nd, 2007
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??
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 266
Reputation: quintoncoert is an unknown quantity at this point 
Solved Threads: 3
quintoncoert quintoncoert is offline Offline
Posting Whiz in Training

Re: So Lost writing this program...

 
0
  #5
Jun 22nd, 2007
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??

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.
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