Ren
Member
Registered: 16th Oct 04
User status: Offline
|
(C++)
I'm writing a program that converts numbers input to the program into grades in a simple fashion. I have everything sorted, all the coding is fine, all the formula's are right except one... Im trying to make it so after a grade has been given, the program contunues after for the next number to convert to a grade - but right now it'll delete what was already there and start afresh.
Here's the code anyway:
#include <iostream>
#include <conio.h>
int main()
{
int igrade =0;
char keypress;
while (keypress!='q')
{
std::cout << "# STUDENT GRADE PROGRAM #\n";
std::cout << "---------------------------\n";
std::cout << "Please enter Student Grade \n";
std::cin >> igrade;
//Formula for converting numbers to grades
if (igrade < 40)
std::cout << "\nStudent Has FAILED\n\n";
else
if (igrade < 60)
std::cout << "\nStudent Has a GRADE C\n\n";
else
if (igrade < 80)
std::cout << "\nStudent has a GRADE B\n\n";
else
if (igrade >= 80)
std::cout << "\nStudent has a GRADE A\n\n";
std::cout << "---------------------------\n";
std::cout << "\n\nTo quit, press Q or enter another grade to continue\n";
keypress=getch();
system("cls");
}
return 0;
}
Anyhelp anyone?
[Edited on 27-10-2006 by Ren]
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
it seems like you need to work out the math formula rather than the programming language itself
|
Ren
Member
Registered: 16th Oct 04
User status: Offline
|
Was that supposed to be a twattish remark?
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
does it seem like a twattish remark?
|
Ren
Member
Registered: 16th Oct 04
User status: Offline
|
Well you didnt even attempt to help me out, and instead critisised my coding. So yeah
|
jaffa
Member
Registered: 27th Mar 00
Location: Stoke-on-Trent
User status: Offline
|
whats not working?
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
quote: Originally posted by Ren
Well you didnt even attempt to help me out, and instead critisised my coding. So yeah
where did i criticise your code
|
Ren
Member
Registered: 16th Oct 04
User status: Offline
|
Normally, when i'd do a while loop, the next set of calculations would be done directly under the ones you had just done. With this code, it removes what was there, and replaces it with the new solution.
Normally this wouldnt matter, but i need to see multiple grades for my assigment.
|
Ren
Member
Registered: 16th Oct 04
User status: Offline
|
quote: Originally posted by Steve
quote: Originally posted by Ren
Well you didnt even attempt to help me out, and instead critisised my coding. So yeah
where did i criticise your code
When you said i need to sort out my maths, i just assumed you were taking a dig
|
Steve
Premium Member
Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
|
i was saying the question you asked seemed to be more of a maths question than a programming one
|
jaffa
Member
Registered: 27th Mar 00
Location: Stoke-on-Trent
User status: Offline
|
quote: Originally posted by Ren
Normally, when i'd do a while loop, the next set of calculations would be done directly under the ones you had just done. With this code, it removes what was there, and replaces it with the new solution.
Normally this wouldnt matter, but i need to see multiple grades for my assigment.
so you need to display all results entered?
|
Ren
Member
Registered: 16th Oct 04
User status: Offline
|
yeah, one after another if you get what i mean
|
Reedy
Member
Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
|
#include <iostream>
using namespace std; //Means you dont have to put std everywhere
#include <conio.h>
int main()
{
int igrade[512];
int j = 0;
char keypress = ' ';
while ( keypress!='q' )
{
cout << "# STUDENT GRADE PROGRAM #\n";
cout << "---------------------------\n";
cout << "Please enter Student Grade \n";
cin >> igrade[j];
//Formula for converting numbers to grades
if ( igrade[j] < 40)
cout << "\nStudent Has FAILED\n\n";
else
if (igrade[j] < 60)
cout << "\nStudent Has a GRADE C\n\n";
else
if (igrade[j] < 80)
cout << "\nStudent has a GRADE B\n\n";
else
if (igrade[j] >= 80)
cout << "\nStudent has a GRADE A\n\n";
cout << "---------------------------\n";
j++;
cout << "\n\nTo quit, press Q or enter another grade to continue\n";
cin >> keypress;
system("cls");
}
return 0;
}
[Edited on 27-10-2006 by Reedy]
|
Reedy
Member
Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
|
Also that is a quick bodge as such.
Added you on msn, so if you come online i can do it privately
[Edited on 27-10-2006 by Reedy]
|
Reedy
Member
Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
|
try it now ive edited it and checked in visual stuido.
|
Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
I think Steve was meaning that you need to approach this problem as a design one rather than just altering code.
If you mean that you want to store all the results then you need an array, and you need to write the grade to the array at the end of the if conditionals then loop through that displaying all the grades when 'q' is pressed.
|
Reedy
Member
Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
|
i did put that code in but it wont come up as the forum code for italic is the same lol.
|
Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
Tick 'Turn BB code off' or whatever its called.
This type of question is better solved as theory, showing syntax is OK but you don't learn anything being given the entire code.
|
Ren
Member
Registered: 16th Oct 04
User status: Offline
|
quote: Originally posted by Steve
i was saying the question you asked seemed to be more of a maths question than a programming one
Ah right. I was just frustrated with my coding, and i just took what you said the wrong way.
Thanks to all that helped, i havent got it sorted yet, but im nearly there
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
quote: Originally posted by Ren
(C++)
I'm writing a program that converts numbers input to the program into grades in a simple fashion. I have everything sorted, all the coding is fine, all the formula's are right except one... Im trying to make it so after a grade has been given, the program contunues after for the next number to convert to a grade - but right now it'll delete what was already there and start afresh.
Here's the code anyway:
#include <iostream>
#include <conio.h>
int main()
{
int igrade =0;
char keypress;
while (keypress!='q')
{
std::cout << "# STUDENT GRADE PROGRAM #\n";
std::cout << "---------------------------\n";
std::cout << "Please enter Student Grade \n";
std::cin >> igrade;
//Formula for converting numbers to grades
if (igrade < 40)
std::cout << "\nStudent Has FAILED\n\n";
else
if (igrade < 60)
std::cout << "\nStudent Has a GRADE C\n\n";
else
if (igrade < 80)
std::cout << "\nStudent has a GRADE B\n\n";
else
if (igrade >= 80)
std::cout << "\nStudent has a GRADE A\n\n";
std::cout << "---------------------------\n";
std::cout << "\n\nTo quit, press Q or enter another grade to continue\n";
keypress=getch();
system("cls");
}
return 0;
}
Anyhelp anyone?
[Edited on 27-10-2006 by Ren]
Poor Code
I'd also make sure I had an include for stdio.h (standard input / output).
As pointed out by reedy, in the example above you have an integer, so you can only store 1 value in that integer at a time, so everytime you enter a grade the integer will get re-set to the new value (old value lost), so use an array, or some form of 'temp' value for storing the old grades.
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
Also if your worried about losing the last screen that showed you the last grade result when you type a new grade in, remove the system("cls");
I'd personally have the test for the keypress at the end of the loop, not the start.
Do
While (keypress!="q")
this is because when you quit from the menu (press q) keypress will = q, and if later you need the menu to be recalled again, when it gets to the first part of the loop
While (keypress!="q") It now won't do this code, because keypress is still = q.
Where as using a
Do
<code here>
While bla bla bla - Would at least execute the menu system once before offering the person the choice to change the value of keypress again.
|