| | |
So Lost writing this program...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Solved Threads: 0
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.
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)
C++ Syntax (Toggle Plain Text)
#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)
C++ Syntax (Toggle Plain Text)
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) }
Last edited by Ancient Dragon; Jun 21st, 2007 at 11:29 pm. Reason: removed excess bbcode and add code tags
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 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.
A while loop could only test for one thing, and it needs to be written correctly. The correct syntax is:
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...
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).
That takes care of your loop. You're for-loop could also use some cleaning...
The proper format for a for-loop is as follows:
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.
The first thing I noticed was that your while loop didn't do exactly what you want.
A while loop could only test for one thing, and it needs to be written correctly. The correct syntax is:
C++ Syntax (Toggle Plain Text)
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...
•
•
•
•
C++ Syntax (Toggle Plain Text)
// assuming you fix the test first... while (grade <1; grade>5;) { cout<<”Invalid data”<<; }
C++ Syntax (Toggle Plain Text)
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...
C++ Syntax (Toggle Plain Text)
For (grade = i[1], grade++) { cout <<grade 1 has collected”,” cans”<<; }
The proper format for a for-loop is as follows:
C++ Syntax (Toggle Plain Text)
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.
Last edited by eDeloa; Jun 22nd, 2007 at 12:43 am.
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Solved Threads: 0
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??
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?? •
•
Join Date: May 2007
Posts: 266
Reputation:
Solved Threads: 3
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.
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.
![]() |
Similar Threads
- Yikes! Lost File & Program Associations! W2K (Windows NT / 2000 / XP)
- script virus?? - Lost file & Program associations (Viruses, Spyware and other Nasties)
- C variables run time problem (C)
- Lottery Program (Java)
- help writing a grep program (C)
- program correction (C++)
- help (Java)
- Need direction on how start this program (C++)
Other Threads in the C++ Forum
- Previous Thread: Programing converting strings to ints within a program
- Next Thread: Program Time Counter
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





