Question

Topics c++ Do While loops cin.ignore Description Redo the last assignment with the following modifications: 1....

Topics c++

Do While loops

cin.ignore

Description

Redo the last assignment with the following modifications:

1. When the program asks the user to enter a student name, the user will enter the full name (e.g. John Smith). Make changes to the program to accommodate that.

2. When the program asks the user to enter the test score, the user will enter the test score as before. However, make changes to the program so that it will check that the score entered is within the range of 0 to 100. If the score entered is outside that range, the program will ask the user to enter the score again. The program will do that repeatedly until the user enters the score in the required range.

Requirements

Do this assignment using a While statement for the loop and a Do While statement for student score input validation.

Implementation Notes

When using a mixture of cin and getline in a program, follow each cin with a cin.ignore ( ).

To force the user to enter the score in the required range, use a Do While loop.

Testing

For turning in the assignment, perform the test run below using the input shown.

Test Run (User input is shown in bold).

Enter Student Name

Alan Adam

Enter Student Score <0-100>

101

Enter Student Score <0-100>

-1

Enter Student Score <0-100>

75

Alan Adam 75 C

Enter Student Name

Bob Boulder

Enter Student Score:

90

Bob Boulder 90 A

Enter Student Name

Cathy Cameron

Enter Student Score

80

Cathy Cameron 80 B

Enter Student Name

Dave Davenport

Enter Student Score:

55

Dave Davenport 55 F

Enter Student Name

Eve Einstein

Enter Student Score

85

Eve Einstein 85 B

Enter Student Name

//

Summary Report

Total Students count 5

A student count 1

B student count: 2

C student count 1

D student 0

F students 1

Sample Code

Note that the sample code below uses a mixture of cin and getline. Whenever such a mixture is used, make sure that every cin is followed by cin.ignore() as done in the sample code.

Also note that in the sample code below, a Do While loop is used to ensure that the user enters a valid score. Often, a Do While statement is used in this fashion for input validation.

int score;

string name;

//Initial setup

cout << "Enter Student name" << endl;

getline (cin, name);

//Test

while (name != "//")

{

       //validate score input

do

{

              cout << "Enter Student Score <0-100>" << endl;

              cin >> score;

              cin.ignore ( );

} while (score < 0 || score > 100);

//compute grade and display it

//Update setup

out << "Enter Student name" << endl;

getline (cin, name);

}

//display summary result

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include <iostream>

using namespace std;

int main() {

int count = 0,score, AGRADE = 0,BGRADE = 0,CGRADE = 0,GRADE = 0,DGRADE = 0,FGRADE = 0; // variable declaration

string name; // variable declaration

cout << "Enter Student name" << endl;

getline (cin, name); // Accept the name of the student

while (name != "//") // Check name as //

{

count++; // increment count

do

{

cout << "Enter Student Score <0-100>" << endl;

cin >> score; // Accept the score

cin.ignore ( ); // To ignore enter

} while (score < 0 || score > 100); // It check number between 0-100

/* Check the score and give corresponding grade*/

if(score>=90){

cout<<name<<" "<<score<<" "<<" A" << endl;

AGRADE++;

}

if(score>=80&&score<90){

cout<<name<<" "<<score<<" "<<" B" << endl;

BGRADE++;

}

if(score>=70&&score<80){

cout<<name<<" "<<score<<" "<<" C" << endl;

CGRADE++;

}

if(score>=60&&score<70){

cout<<name<<" "<<score<<" "<<" D" << endl;

DGRADE++;

}

if(score<60){

cout<<name<<" "<<score<<" "<<" F" << endl;

FGRADE++;

}

cout << "Enter Student name" << endl;

getline (cin, name); // Accept the name of the student

}

/* Print the reports according to the input*/

cout<<"Summary Report"<<endl;

cout<<"Total Students count "<<count<<endl;

cout<<"A student count: "<<AGRADE<<endl;

cout<<"B student count: "<<BGRADE<<endl;

cout<<"C student count: "<<CGRADE<<endl;

cout<<"D student count: "<<DGRADE<<endl;

cout<<"F student count: "<<FGRADE<<endl;

return 0;

}

Output:

Add a comment
Know the answer?
Add Answer to:
Topics c++ Do While loops cin.ignore Description Redo the last assignment with the following modifications: 1....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Topics c ++ Loops While Statement Description Write a program that will display a desired message...

    Topics c ++ Loops While Statement Description Write a program that will display a desired message the desired number of times. The program will ask the user to supply the message to be displayed. It will also ask the user to supply the number of times the message is to be displayed. It will then display that message the required number of times. Requirements Do this assignment using a While statement.    Testing For submitting, use the data in the...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • Professor Dolittle has asked some computer science students to write a program that will help him...

    Professor Dolittle has asked some computer science students to write a program that will help him calculate his final grades. Professor Dolittle gives two midterms and a final exam. Each of these is worth 100 points. In addition, he gives a number of homework assignments during the semester. Each homework assignment is worth 100 points. At the end of the semester, Professor Dolittle wants to calculate the median score on the homework assignments for the semester. He believes that the...

  • IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality...

    IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • C++ Assignment on Search and Sort for Chapter 8 Problem: Modify the student grade problem to...

    C++ Assignment on Search and Sort for Chapter 8 Problem: Modify the student grade problem to include the following: •   Write a Search function to search by student score •   Write a Search function to search by student last name •   Write a Sort function to sort the list by student score •   Write a Sort function to sort the list by student last name •   Write a menu function that lets user to choose any action he/she want to...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT