I’m actually going crazy. After spending a lot of time working on Decision making (if-else, switch and …), I really cannot figure out the basic difference between iteration and decision making. In some problems, the tutor asks us to use “While” loop and “break”, instead of “if-else” and it makes me confused. Why???
Yes, yes, I know that it creates a loop, but the problem is that the debugger doesn’t say anything. The program seems to be very well-organized (0 warning, 0 errors). It takes 10 numbers and nothing… freezes! Oh thanks my goodness!
Do something or at least tell me what the heck is wrong with my program.
Let’s start from the first question: There’s a table about the grading structure including ten grades (A,A-,B+,B,B-,C+,C,C-,D+,D,F). One should key-in ten scores (0-100; all integers). We should write a program using a “do-while” loop to print out the grades for every score entered.
So we have ten scores (s1,s2,…,s10) and ten grades (as mentioned above). For every value (score) we key-in, it should decide whether the student should be awarded with (A,A-,…,F). Isn’t it “Decision making”? How to use a loop then?
Let’s solve it first. The second question is even screwier.
Can you gimme a hand mates?
Thanks a bunch
P.S. I’m using a reference book named “Programming in C++: Lessons and Applications” by Timothy B. D’Orazio, McGraw-Hill. This chapter (iteration) is not very well-written (too short to me). Any better suggestion?
... the debugger doesn’t say anything. The program seems to be very well-organized (0 warning, 0 errors). It takes 10 numbers and nothing… freezes! Oh thanks my goodness!
Do something or at least tell me what the heck is wrong with my program.
) "0 warning, 0 errors" is coming from the compiler, not the debugger. And it doesn't say anything about how well-organized your program is. It just means you didn't make any syntax errors.
If the program doesn't do what you intended, there is probably something wrong with your logic. The compiler only knows what you tell it to do, not what you want it to do. If the compiler knew how your program should be written, what would it need you for?
Oh, and we could give you a hand correcting your code if we could see it.
Can you clarify the difference first (difference between if-else and do-while loop, in the structure)?
And where the loop should be used exactly? For taking the scores or for assigning the grades?
It's Ok. I know the rules. You won't help unless I show some effort. :D Not too bad.
Just don't laugh when you see this. Honestly, I have no idea to distinguish the structure of do-while from if-else.
BTW, I know about the ASCII characters, but it's also a bit confusing. This one actually shows 65(in ASCII indicates A only) instead of any grade. :)
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main ()
{
int s,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10;
int grade;
char A,B,C,D,F;
cout<<"Please key-in 10 scores (max=100)"<<endl;
cin>>s1>>s2>>s3>>s4>>s5>>s6>>s7>>s8>>s9>>s10;
do
{
grade='A';
} while ((s1>79 && s1<101)||(s2>79 && s2<101)||(s3>79 && s3<101)||(s4>79 && s4<101)||(s5>79 && s5<101)||\
(s6>79 && s6<101)||(s7>79 && s7<101)||(s8>79 && s8<101)||(s9>79 && s9<101)||(s10>79 && s10<101));
do
{
grade='A-';
} while
((s1>74 && s1<80)||(s2>74 && s2<80)||(s3>74 && s3<80)||(s4>74 && s4<80)||(s5>74 && s5<80)||
(s6>74 && s6<80)||(s7>74 && s7<80)||(s8>74 && s8<80)||(s9>74 && s9<80)||(s10>74 && s10<80));
do
{
grade='B+';
} while
((s1>69 && s1<75)||(s2>69 && s2<75)||(s3>69 && s3<75)||(s4>69 && s4<75)||(s5>69 && s5<75)||
(s6>69 && s6<75)||(s7>69 && s7<75)||(s8>69 && s8<75)||(s9>69 && s9<75)||(s10>69 && s10<75));
do
{
grade='B';
} while
((s1>64 && s1<70)||(s2>64 && s2<70)||(s3>64 && s3<70)||(s4>64 && s4<70)||(s5>64 && s5<70)||
(s6>64 && s6<70)||(s7>64 && s7<70)||(s8>64 && s8<70)||(s9>64 && s9<70)||(s10>64 && s10<70));
do
{
grade='B-';
} while
((s1>59 && s1<65)||(s2>59 && s2<65)||(s3>59 && s3<65)||(s4>59 && s4<65)||(s5>59 && s5<65)||
(s6>59 && s6<65)||(s7>59 && s7<65)||(s8>59 && s8<65)||(s9>59 && s9<65)||(s10>59 && s10<65));
do
{
grade='C+';
} while
((s1>54 && s1<60)||(s2>54 && s2<60)||(s3>54 && s3<60)||(s4>54 && s4<60)||(s5>54 && s5<60)||
(s6>54 && s6<60)||(s7>54 && s7<60)||(s8>54 && s8<60)||(s9>54 && s9<60)||(s10>54 && s10<60));
do
{
grade='C';
} while
((s1>49 && s1<55)||(s2>49 && s2<55)||(s3>49 && s3<55)||(s4>49 && s4<55)||(s5>49 && s5<55)||
(s6>49 && s6<55)||(s7>49 && s7<55)||(s8>49 && s8<55)||(s9>49 && s9<55)||(s10>49 && s10<55));
do
{
grade='C-';
} while
((s1>44 && s1<50)||(s2>44 && s2<50)||(s3>44 && s3<50)||(s4>44 && s4<50)||(s5>44 && s5<50)||
(s6>44 && s6<50)||(s7>44 && s7<50)||(s8>44 && s8<50)||(s9>44 && s9<50)||(s10>44 && s10<50));
do
{
grade='D+';
} while
((s1>39 && s1<45)||(s2>39 && s2<45)||(s3>39 && s3<45)||(s4>39 && s4<45)||(s5>39 && s5<45)||
(s6>39 && s6<45)||(s7>39 && s7<45)||(s8>39 && s8<45)||(s9>39 && s9<45)||(s10>39 && s10<45));
do
{
grade='D';
} while
((s1>34 && s1<40)||(s2>34 && s2<40)||(s3>34 && s3<40)||(s4>34 && s4<40)||(s5>34 && s5<40)||
(s6>34 && s6<40)||(s7>34 && s7<40)||(s8>34 && s8<40)||(s9>34 && s9<40)||(s10>34 && s10<40));
do
{
grade='F';
} while
(s1<35||s2<35||s3<35||s4<<35||s5<35||s6<35||s7<35||s8<35||s9<35||s10<35);*/
cout<<endl<<endl;
cout<<"Subject\t"<<"Numerical score\t"<<"Grade"<<endl
<<"-------\t"<<"---------------\t"<<"-----"<<endl
<<"1\t"<<s1<<"\t\t"<<grade<<endl
<<"2\t"<<s2<<"\t\t"<<grade<<endl
<<"3\t"<<s3<<"\t\t"<<grade<<endl
<<"4\t"<<s4<<"\t\t"<<grade<<endl
<<"5\t"<<s5<<"\t\t"<<grade<<endl
<<"6\t"<<s6<<"\t\t"<<grade<<endl
<<"7\t"<<s7<<"\t\t"<<grade<<endl
<<"8\t"<<s8<<"\t\t"<<grade<<endl
<<"9\t"<<s9<<"\t\t"<<grade<<endl
<<"10\t"<<s10<<"\t\t"<<grade<<endl<<endl;
ofstream outfile ("C:\\Users\\ALI REZA\\Documents\\newc\\KEP080703\\KEP080703_Lab5\\GRADE.dat");
outfile<<"Subject\t"<<"Numerical score\t"<<"Grade"<<endl
<<"-------\t"<<"---------------\t"<<"-----"<<endl
<<"1\t"<<s1<<"\t\t"<<grade<<endl
<<"2\t"<<s2<<"\t\t"<<grade<<endl
<<"3\t"<<s3<<"\t\t"<<grade<<endl
<<"4\t"<<s4<<"\t\t"<<grade<<endl
<<"5\t"<<s5<<"\t\t"<<grade<<endl
<<"6\t"<<s6<<"\t\t"<<grade<<endl
<<"7\t"<<s7<<"\t\t"<<grade<<endl
<<"8\t"<<s8<<"\t\t"<<grade<<endl
<<"9\t"<<s9<<"\t\t"<<grade<<endl
<<"10\t"<<s10<<"\t\t"<<grade<<endl<<endl;
outfile.close();
return 0;
}
if( ... ) { ... }
else { ... } is not a loop, it's a conditional block. It tests the logical expression in the ( ... ) and uses the result [true,false] to determine whether or not to execute a block of code:
if( [some logical condition is true] )
do { [whatever code is written in this block] }
otherwise
do { [whatever code is written in this block] }
------------------------------------------------------
do{ ... }while ( ... ) first executes the code in the { ... }, then tests the logical expression in ( ... ) and if it evaluates to "true", goes back and executes the code in { ... } again, then tests the expression in ( ... ) again, and so on. This loop continues as long as the logical expression in ( ... ) remains true.
From your description, it sounds like you would use two loops: the first one asks for user input and repeats as long as the user has more input to enter.
Then, another loop prints output, continuing as long as there is data to output.
Edit: Think about this, then see how you can revise your code to take advantage of the do-while loop.
Yup, but still if I want to rewrite the program, I will write the same thing as above. Can you do the first party (for one score, one grade only) for me?
I really don't understand how to use a loop for this assignment.
It's Ok. I know the rules.
Apparently not well enough to add code tags
[code=cplusplus] // your code here
[/code]
I knew that one either. I didn't know the code. ;)
Anyway, thanks for reminding.
Now let that alone for a sec; almost all the samples in my reference books are about increment and piece of cake to me.
This one, on the other hand, ain't. It has nothing to do with increment, at least from what you'd get from the first look.
This one, on the other hand, ain't. It has nothing to do with increment, at least from what you'd get from the first look.
Then you need to take a second look.
Instead of 10 independent variables to hold the scores, how about a 10-element array:
int scores[10]
Define an index variable with initial value 0:
int idx = 0;
Now use a loop to read in the data. The code in the loop stores input in scores[idx], then increments idx. The loop repetitions are controlled by testing the value of idx.
I almost know everything in theory, but when it comes to action, I'm blank. My mind tends to decision making as you see.
Can you be kind enough to write one of the loops for me?
Thanks pal ;)
First about your code: there's an extraneous "*/" at the end of line 84 which should prevent it from compiling, so I can't be certain about what code you're actually running.
Also, you declare a variable int grade, but in your do-while loops you attempt to assign values like 'A-' to it. Now there's nothing inherently wrong with assigning a char value to an int since a char actually is an integral type and the compiler handles that conversion implicitly. But 'A-' is not a char. It's a multi-character character constant and I have no idea what the compiler will do with that. I would guess that the behavior would be undefined, i.e., vary depending on which compiler you use. In short, don't do that.
But the major problem that causes your program to do "nothing" is in the logic of what you wrote. Look at the control expression of your first do-while loop:
(( s1>79 && s1<101 )||( s2>79 && s2<101 )||( s3>79 && s3<101 )||( s4>79 && s4<101 )||( s5>79 && s5<101 )||\
( s6>79 && s6<101 )||( s7>79 && s7<101 )||( s8>79 && s8<101 )||( s9>79 && s9<101 )||( s10>79 && s10<101 ) )
In English, "s1 is greater than 79 and less than 101, or s2 is greater than 79 and less than 101, or s3 is ...". That expression is true as long as the value of any one of your "s_" variables falls within that range. Since all of your "s_" values are assigned once and thereafter never change, if even a single one of them falls within that range your program never leaves that loop. It keeps doing
grade='A';
forever.
As if that weren't enough, your printing code won't do what you want either -- if your program ever got to that point (which, as explained above, it won't) it would have a single variable called grade, which would contain only the last value that had been assigned to it in the preceding code, and it would print that same value 10 times.
So, here's an example of a do-while loop to read in the scores:
int score[10];
int idx = 0;
do{
cout << "Please enter a score> ";
cin >> score[i];
++i;
} while( i < 10 );
and here's an example of code to print the appropriate grade for a particular score:
i = 5; // I just picked this arbitrarily
if( score[i]>79 && score[i]<101 ) {
cout << "Score = " << score[i] << "; grade is A" << endl;
} else if( score[i]>74 && score[i]<80 ) {
cout << "Score = " << score[i] << "; grade is A-" << endl;
} else if ( ... and so on ... ) {
} else { // you can use "else" without "if" to catch all possibilities that weren't included in any of the preceding "if" conditionals
}
You can wrap that in a loop that starts with i = 0 and goes through all of the scores that were input. You can add the file outputs to it (open the ofstream before starting this loop. Also, print whatever column headings you want before starting the loop.)
Since you already "almost know everything in theory", that should be enough to get you going. ;)
oh, I have to admit, I'm stupid. :-O
I was trying to write a loop for assigning the grades. :@
And nice tip on "how to use [] for the scores". I didn't know this one.:*
One more question. I need to print out the grades to a file. How can I say:
if( score[i]>79 && score[i]<101 ) {
grade = A};
or
if (score[i]>74 && score[i]<80) {
grade = A-};
so I can write the program in a much easier way:
ofstream outfile ("C:\\OUT.dat");
outfile<<"Subject\t"<<"Numerical score\t"<<"Grade"<<endl
<<"-------\t"<<"---------------\t"<<"-----"<<endl
<<"1\t"<<score[1]<<"\t\t"<<grade<<endl
.
.
.
Most probably, it will print out "65" or "-8752131...." or something like that. What's the solution for that?
if grade is declared as an int then you need to typecast it when printing it cout << (char)grade
It might be cleaner if you had an array of grades similar to the scores so that you could figure out all the grades first and then print them in a nice tidy loop.
A code would be more appreciated. :)
If it's a sort of array, we do have a table for grades:
80-100 : A
75-79 : A-
70-74 : B+
65-69 : B
60-64 : B-
55-59 : C+
50-54 : C
45-49 : C-
40-44 : D+
35-39 : D
less-35: F
Ok, normally wouldn't do it but this thread is getting pretty long and you have tried pretty hard. Here is how I would code it.
#include <iostream>
#include <string>
using namespace std;
struct grades
{
int score;
string grade;
};
int lower_limits[10] = {
80, 75, 70, 65, 60, 55, 50, 45, 40, 35
};
string letters[10] = {
"A", "A-","B","B-","C","C-","D","D-"
};
int main()
{
const int MAXGRADES = 10;
struct grades theGrades[MAXGRADES];
for(int i = 0; i < MAXGRADES; i++)
{
cout << "Enter score #" << i+1 << "\n";
cin >> theGrades[i].score;
// now find the grade
for(int j = 0; j < 10; j++)
{
if(theGrades[i].score > lower_limits[j])
{
theGrades[i].grade = letters[j];
break;
}
}
if(theGrades[i].grade == "")
theGrades[i].grade = "F";
}
for(int i = 0; i < MAXGRADES; i++)
{
cout << "score " << theGrades[i].score << " = " << theGrades[i].grade << "\n";
}
}
Thanks for your patience, but we cannot use "string" and "struct" yet.
Let's see what the idea of "r.stiltskin" is.
If you think it's something uncommon in your forum, you can close the thread. ;)
I thought, there should be something like
char A-, .... (-, cannot be complied though).
I guess I didn't explain clearly enough. Your assignments of (for example)
grade = 'A-'
are both erroneous and unnecessary. Erroneous because 'A-' is not a character nor an integer. It's a string. 'A' is a character. '-' is a character. Single quotes are used to enclose a character literal. Your use of them to enclose a string is a syntax error. And assigning that "multi-character character literal" to an int variable is also a syntax error (I don't know why the compiler doesn't report it as an error, and I don't know exactly how the compiler will handle it, but that doesn't matter in this case.)
You are also making a semantic error. grade=[anything]
is not an output, it's an assignment. grade=[anything]
isn't outputting the grades to the file. It's just assigning one value after another to the variable "grade" which is never used.
Instead, as I tried to tell you in post #11, all you need to do is open the file before the beginning of the printing block of code, and add a single line to each "if" block:
ofstream outfile("outputfilename");
i = 5; // I just picked this arbitrarily
if( score[i]>79 && score[i]<101 ) {
cout << "Score = " << score[i] << "; grade is A" << endl;
outfile << "Score = " << score[i] << "; grade is A" << endl;
} else if( score[i]>74 && score[i]<80 ) {
cout << "Score = " << score[i] << "; grade is A-" << endl;
outfile << "Score = " << score[i] << "; grade is A" << endl;
} else if ( ... and so on ... ) {
} else { // you can use "else" without "if" to catch all possibilities that weren't included in any of the preceding "if" conditionals
}
This way, you can do both the screen output and the file output in the same loop. Just change the quoted words to exactly what you want to output.
Thanks for your patience, but we cannot use "string" and "struct" yet.
.
replace string with character arrays and replace the struct with two arrays, one array of ints and the other of charcter arrays.
Our tutor is a bit strict. He demanded the table below:
Number///////Numerical Scores/////Grade
---------- ---------------------- --------
1///////////////score[1]////////////////...
... and so on
/=space
Just change the quoted words to exactly what you want to output.
I've practically written it for you already. Think.
If you use int i;
as the index for the loop, i is also the index for the array of scores, and it also does the counting for the "Number" that you want to print. Since i starts out at 0, you can print the first score by writing:
cout << i+1 << " " << scores[i] << " A" << endl;
and similarly for outfile.
Put whatever number of spaces you want, wherever you want, between the quotes.
you can also use setw() to specify the width of the print field cout << left << setw(20) << "Number" << setw(25) << "Numerical Scores" << setw(15) << "Grade"
You also will have to include <iomanip> header file for the above to compile.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main ()
{
int score[10];
int i = 0;
do{
cout<<"Please enter a score (max=100)"<<endl;
cin>>score[i];
++i;
} while(i < 10);
ofstream outfile("C:\\Users\\ALI REZA\\Documents\\newc\\KEP080703\\KEP080703_Lab5\\GRADE.dat");
i = 5; // I just picked this arbitrarily
cout<<"Subject\t"<<"Numerical score\t"<<"Grade"<<endl
<<"-------\t"<<"---------------\t"<<"-----"<<endl;
outfile<<"Subject\t"<<"Numerical score\t"<<"Grade"<<endl
<<"-------\t"<<"---------------\t"<<"-----"<<endl;
if( score[i]>79 && score[i]<101 ) {
cout<<i+1<<"\t"<<score[i]<<"\t\tA"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tA"<<endl;
} else if( score[i]>74 && score[i]<80) {
cout<<i+1<<"\t"<<score[i]<<"\t\tA-"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tA-"<<endl;
} else if (score[i]>69 && score[i]<75) {
cout<<i+1<<"\t"<<score[i]<<"\t\tB+"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tB+"<<endl;
} else if( score[i]>64 && score[i]<70) {
cout<<i+1<<"\t"<<score[i]<<"\t\tB"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tB"<<endl;
} else if (score[i]>59 && score[i]<65) {
cout<<i+1<<"\t"<<score[i]<<"\t\tB-"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tB-"<<endl;
} else if( score[i]>54 && score[i]<60) {
cout<<i+1<<"\t"<<score[i]<<"\t\tC+"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tC+"<<endl;
} else if (score[i]>49 && score[i]<55) {
cout<<i+1<<"\t"<<score[i]<<"\t\tC"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tC"<<endl;
} else if( score[i]>44 && score[i]<50) {
cout<<i+1<<"\t"<<score[i]<<"\t\tC-"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tC-"<<endl;
} else if (score[i]>39 && score[i]<45) {
cout<<i+1<<"\t"<<score[i]<<"\t\tD+"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tD+"<<endl;
} else if (score[i]>34 && score[i]<40) {
cout<<i+1<<"\t"<<score[i]<<"\t\tD"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tD"<<endl;
} else {
cout<<i+1<<"\t"<<score[i]<<"\t\tF"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tF"<<endl;
}
outfile.close();
return 0;
}
And here's what I get:
Attachment
That's because you're just copying what i wrote & not thinking. Why would you write
"i = 5; // I just picked this arbitrarily"
in your program?
If it wasn't clear before, it should certainly be clear now that you see your output that I was just giving you an example of how to print a single line of your output. I did say, didn't I, that you have to put all of that into a loop to print your output?
I will leave it to you to figure out how to do that in order to print all 10 lines of output. You already have the input loop to serve as a model to help you.
PS: you don't need #include <iomanip>. You would only need that if you were going to use the setw() function that AncientDragon mentioned.
I've tried many ways already. By mistake, I didn't remove that "i=5 ...".
And I know that "manip" manipulation. It was included for the manipulation of header.
You mean that I should have a nested if-else command in a do-while loop? what would be the increment order?
All the if statements are the same. Why does it print out randomly?
:D , a new one: almost with the same code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main ()
{
int score[10];
int i=0;
do{
cout<<"Please enter a score (max=100):"<<endl;
cin>>score[i];
++i;
} while(i<10);
ofstream outfile("C:\\Users\\ALI REZA\\Documents\\newc\\KEP080703\\KEP080703_Lab5\\GRADE.dat");
cout<<"Subject\t"<<"Numerical score\t"<<"Grade"<<endl
<<"-------\t"<<"---------------\t"<<"-----"<<endl;
outfile<<"Subject\t"<<"Numerical score\t"<<"Grade"<<endl
<<"-------\t"<<"---------------\t"<<"-----"<<endl;
i=0;
if(score[i]>79 && score[i]<101 ) {
cout<<"1"<<"\t"<<score[i]<<"\t\tA"<<endl;
outfile<<"1"<<"\t"<<score[i]<<"\t\tA"<<endl;
};
i=1;
if(score[i]>74 && score[i]<80) {
cout<<"2"<<"\t"<<score[i]<<"\t\tA-"<<endl;
outfile<<"2"<<"\t"<<score[i]<<"\t\tA-"<<endl;
};
i=2;
if (score[i]>69 && score[i]<75) {
cout<<"3"<<"\t"<<score[i]<<"\t\tB+"<<endl;
outfile<<3<<"\t"<<score[i]<<"\t\tB+"<<endl;
};
i=3;
if(score[i]>64 && score[i]<70) {
cout<<"4"<<"\t"<<score[i]<<"\t\tB"<<endl;
outfile<<"4"<<"\t"<<score[i]<<"\t\tB"<<endl;
};
i=4;
if (score[i]>59 && score[i]<65) {
cout<<"5"<<"\t"<<score[i]<<"\t\tB-"<<endl;
outfile<<"5"<<"\t"<<score[i]<<"\t\tB-"<<endl;
};
i=5;
if(score[i]>54 && score[i]<60) {
cout<<"6"<<"\t"<<score[i]<<"\t\tC+"<<endl;
outfile<<"6"<<"\t"<<score[i]<<"\t\tC+"<<endl;
};
i=6;
if (score[i]>49 && score[i]<55) {
cout<<"7"<<"\t"<<score[i]<<"\t\tC"<<endl;
outfile<<"7"<<"\t"<<score[i]<<"\t\tC"<<endl;
};
i=7;
if( score[i]>44 && score[i]<50) {
cout<<"8"<<"\t"<<score[i]<<"\t\tC-"<<endl;
outfile<<"8"<<"\t"<<score[i]<<"\t\tC-"<<endl;
};
i=8;
if (score[i]>39 && score[i]<45) {
cout<<"9"<<"\t"<<score[i]<<"\t\tD+"<<endl;
outfile<<"9"<<"\t"<<score[i]<<"\t\tD+"<<endl;
};
i=9;
if (score[i]>34 && score[i]<40) {
cout<<"10"<<"\t"<<score[i]<<"\t\tD"<<endl;
outfile<<"10"<<"\t"<<score[i]<<"\t\tD"<<endl;
};
i=10;
if (score[i]<35) {
cout<<i+1<<"\t"<<score[i]<<"\t\tF"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tF"<<endl;
};
outfile.close();
return 0;
}
All you have to do is put that code in a loop. Simple.
for(i = 0; i < 10; i++)
{
if( score[i]>79 && score[i]<101 ) {
cout<<i+1<<"\t"<<score[i]<<"\t\tA"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tA"<<endl;
} else if( score[i]>74 && score[i]<80) {
cout<<i+1<<"\t"<<score[i]<<"\t\tA-"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tA-"<<endl;
} else if (score[i]>69 && score[i]<75) {
cout<<i+1<<"\t"<<score[i]<<"\t\tB+"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tB+"<<endl;
} else if( score[i]>64 && score[i]<70) {
cout<<i+1<<"\t"<<score[i]<<"\t\tB"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tB"<<endl;
} else if (score[i]>59 && score[i]<65) {
cout<<i+1<<"\t"<<score[i]<<"\t\tB-"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tB-"<<endl;
} else if( score[i]>54 && score[i]<60) {
cout<<i+1<<"\t"<<score[i]<<"\t\tC+"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tC+"<<endl;
} else if (score[i]>49 && score[i]<55) {
cout<<i+1<<"\t"<<score[i]<<"\t\tC"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tC"<<endl;
} else if( score[i]>44 && score[i]<50) {
cout<<i+1<<"\t"<<score[i]<<"\t\tC-"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tC-"<<endl;
} else if (score[i]>39 && score[i]<45) {
cout<<i+1<<"\t"<<score[i]<<"\t\tD+"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tD+"<<endl;
} else if (score[i]>34 && score[i]<40) {
cout<<i+1<<"\t"<<score[i]<<"\t\tD"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tD"<<endl;
} else {
cout<<i+1<<"\t"<<score[i]<<"\t\tF"<<endl;
outfile<<i+1<<"\t"<<score[i]<<"\t\tF"<<endl;
}
}
yes!!!!!!!!!!
yes!
Although I've tried to turn it into a do-while loop and came up with an infinite loop, but "For" loop works like a charm.
Thanks
All you have to do is put that code in a loop. Simple.
Well, there you go. Drag your feet long enough and someone will do it for you.
All the if statements are the same. Why does it print out randomly?
Not sure what you meant by "randomly". It was printing the information for the 6th score of your input, wasn't it?
Nope, it was random. First try: 6th score, second try: 9th, third try: 5th and ...
Anyway, I believe that no one could do this assignment perfectly. The tutor will explain more about it, hopefully.
Thanks everyone for helping
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.