473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Looping and relational (if else) question

hey guys need help or some guidances please. Basically its a complicated looping and relational question.

Write a program to calculate te percentage score of the mark of the students.The program will read in any number of marks in integer, and print out the percentage score of the five categories. You should reject all the invalid marks from the input

The grade should follow the table below
mark grade
<50 fail
50-59 D
60-69 C
70-79 B
80-89 A
90-100 Distinction


sample output:
Any mark to enter?yes or no? : yes
please enter the mark : 55
Any mark to enter?yes or no? : yes
please enter the mark : 75
Any mark to enter?yes or no? : yes
please enter the mark : -5
Please enter valid mark between 0 and 100. <----- guide/help needed here
Any mark to enter?yes or no? : yes
please enter the mark : 85
Any mark to enter?yes or no? : yes
please enter the mark : 35
Any mark to enter?yes or no? : yes
please enter the mark : 65
Any mark to enter?yes or no? : yes
please enter the mark : 72
Any mark to enter?yes or no? : yes
please enter the mark : 44
Any mark to enter?yes or no? : yes
please enter the mark : 81
Any mark to enter?yes or no? : no
Total entry is 8 <------guide/help needed here
The percentage score in grade Fail is 37.5%
The percentage score in grade D is 12.5%
The percentage score in grade C is 12.5%
The percentage score in grade B is 37.5%
The percentage score in grade A is 37.5%
The percentage score in grade Distinction is 0%

thanks... i will be happy if anyone can give me help on this prog.
Nov 28 '06 #1
7 2133
Ganon11
3,652 Expert 2GB
[quote=jwatson]You should reject all the invalid marks from the input
Any mark to enter?yes or no? : yes
please enter the mark : 55
Any mark to enter?yes or no? : yes
please enter the mark : -5
Please enter valid mark between 0 and 100. <----- guide/help needed here
Any mark to enter?yes or no? : yes
please enter the mark : 85[quote]

All right, let's think about this logically.

You will need two different loops. First, you want to keep executing the process while the user has input. You should have a string variable holding the response from the "Any mark to enter?" question - if the string equals "yes", keep going, right? You'll have to get the first user input before you start the loop, so the code might look like this:

Expand|Select|Wrap|Line Numbers
  1. cout << "Any mark to enter?yes or no? : ";
  2. cin >> decision;
  3.  
  4. while (decision == "yes" || decision == "YES" || decision == "Yes") { // Checks any possible mispelling of "yes"
  5.    cout << "please enter the mark : ";
  6.    ...
  7.    ...
  8.    ...
  9.    cout << "Any mark to enter?yes or no? : ";
  10.    cin >> decision;  // Gets user input to determine if the loop continues
  11. }
Also inside this loop, you can have an integer variable called count while will keep track of every time the loop executes - i.e. every time the user enters a new mark.

Next, when determining whether the mark entered is legal, you can take two approaches. Either you repeat the statement "please enter the mark : " or a similar prompt while the user enters bad input (stopping when valid input is entered) or, as in the example above, simply skip to the next "Any mark" question.

For the first solution, you will use a smaller loop similar to the large outer loop, except you will use a do...while loop (since, logically, if the user wants to enter information, they should be able to try at least once). Inside the do...while loop, you can prompt the user for input, input their mark into your variable, and loop while the input is less than 0 or greater than 100. Once the input is valid, this loop will exit, and you will stop asking for input.

Expand|Select|Wrap|Line Numbers
  1. do {
  2.    cout << "please enter the mark : ";
  3.    cin >> num;
  4. } while (num is bad input);
For the second solution, you prompt the user once for input. If the input is valid, no problems! Continue with calculations. Otherwise, you need to display the warning message ("Please enter valid mark between 0 and 100."), ask the "Any marks to enter?" question, input into decision, and then use the continue statement to stop performing calculations and go directly to the conditional statement at the end of the loop.

Expand|Select|Wrap|Line Numbers
  1. cout << "please enter the mark : ";
  2. cin >> num;
  3. if (num is bad input) {
  4.    cout << "Please enter valid mark between 0 and 100." << endl;
  5.    cout << "Any mark to enter?yes or no? : ";
  6.    cin >> decision;
  7.    continue;
  8. } else {
  9.    // Continue with calculations
  10. }
Both of these will work, though in slightly different ways. Come back if you need more help.
Nov 28 '06 #2
thanks i for the help..... let me try writing it.
Nov 28 '06 #3
i tried build this for a part that ive done but after i type "yes" in the program, it doesnt execute to the while loop??? strange... and help or error in my prog?

#include <stdio.h>

main(){

int mark,count;
char decision;

printf("Any mark to enter?yes or no? : ");
scanf("%d",&decision);

while (decision == "yes" || decision == "YES" || decision == "Yes")
{
printf("please enter the mark : ");
scanf("%d",&mark);
if (mark<=0){
printf("Please enter valid mark between 0 and 100.");
}
else{
printf("Any mark to enter?yes or no? : ");
scanf("%d",&decision);
}


}

return (0);
}
Dec 1 '06 #4
i tried build this for a part that ive done but after i type "yes" in the program, it doesnt execute to the while loop??? strange... and help or error in my prog?

#include <stdio.h>

main(){

int mark,count;
char decision;

printf("Any mark to enter?yes or no? : ");
scanf("%d",&decision);

while (decision == "yes" || decision == "YES" || decision == "Yes")
{
printf("please enter the mark : ");
scanf("%d",&mark);
if (mark<=0){
printf("Please enter valid mark between 0 and 100.");
}
else{
printf("Any mark to enter?yes or no? : ");
scanf("%d",&decision);
}


}

return (0);
}
Hi,

From first look... this is what i found.

Have a close look at this line in your program...

scanf("%d",&decision);

you are using %d???(It is used for integers) also "decision" is declared as a character which mean it wont be able to accommodate "YES"!!!!.

Regards,
ShaggY@FtF
Dec 1 '06 #5
Ganon11
3,652 Expert 2GB
Yes, you will have to change decision to a string, or test if character == 'y' or 'Y'
Dec 1 '06 #6
hi there,

i think you need a prototype on this, because your function does'nt declare on it, you should put #include <string.h> , #include <iostream.h> on your header


before your while statement place

char car[5] ="yes" ; //and the others, coz if not you will see a compiler warning

do not use %d coz its not an integer

actually theres a lot of problem in your program, your declaration are lack of supporting functions and prototypes, try to revised it again

try to simplified your while statement by using this:

while ( stricmp( decision, "yes") !=0)

then proceed to others,

ok,

regards,
Dec 2 '06 #7
hey guys i still cant get this work i have tried my best. but i still cant get the "yes" choice to work.... and the Total count part of the program. i would appreciated if anyone can give me a complete brief solution of this program.
Heres what i have done so far... pretty messy but i tried being new to C programming:
#include <stdio.h>

main(){

int mark,count;
char decision;

printf("Any mark to enter?yes or no? : ");
scanf("%c",&decision);

while (decision=='y')
{
printf("please enter the mark : ");
scanf("%d",&mark);
if (mark>=0){
printf("Any mark to enter?yes or no? : ");
scanf("%d",&decision);

}
else{
printf("Please enter valid mark between 0 and 100.");
break;
}
}I am stuck for the rest of the program... HELP PLS


return (0);
}
Dec 6 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: | last post by:
I need to write a relational algebra query for the following: Show the item_ID, item description and category description for all items where the supplier uses "rail" as the delivery_method. ...
4
by: Shane | last post by:
Hello, I'm pretty new to setting up databases, but so far I'm getting along swimmingly. But I have one question. I'm setting up a database for a client who wants to sell tickets to their theater...
13
by: Joseph Oget | last post by:
If I use the code below in a VB.NET form, even if the file exists, and is then executed, the Else statement produces the "Sorry cannot find the File " error message. The MessageBox.Show(...
9
by: Barton | last post by:
Hello, I just made to move to ASP. I have been developing in PHP the past two years. I must say that I'm a little disappointed in the quality of the beginners tutorials. They don't go further...
0
by: =?Utf-8?B?Uml6d2Fu?= | last post by:
I have these tables: create table cd_language_saaa ( saaapk int not null PRIMARY KEY, code varchar(50) not null ) create table cd_calendar_saaj (
4
by: planetmatt | last post by:
I am a Python beginner. I am trying to loop through a CSV file which I can do. What I want to change though is for the loop to start at row 2 in the file thus excluding column headers. At...
3
by: SAM | last post by:
ll a écrit : <script type="text/javascript"> function checkform( f ) // f is the form to check { var txt1 = 'Please make a selection for the Learning Outcome :', txt2 = '\nYour...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.