Question

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[], int & section);
bool again();


int main()
{
    char user_input;
    char first[SIZE];
    char last[SIZE];
    char des[SIZE];
    int section = 0;
    int crn = 0;

    read_name(first, last);
    do
    {
    read_course(crn, des, section);
      
    cout << "Name: " << last << ", " << first << endl;
    cout << "CRN: " <<crn <<endl<< "Destignator: " << des <<endl;
    cout << "Section: " << section << endl;

    again();
    while(again == true);


     return 0;
}

void read_name(char first[], char last[])
{

    cout << "Enter your first name then press enter: " << endl;
    cin.get(first, SIZE, '\n');
    cin.ignore(100, '\n');

    cout << "Enter your last name then press enter: " << endl;
    cin.get(last, SIZE, '\n');
    cin.ignore(100, '\n');

}

void read_course(int & crn, char des[], int & section)
{
    cout << "Enter valid CRN (e.g. 123456): " << endl;
    cin >> crn;
    if(crn <= 0)
    {
        cout << "Please enter a valid CRN: " << endl;
        cin >> crn;
    }
    cin.ignore(100, '\n');

    cout << "Enter Course Designator: " << endl;
    cin.get(des, SIZE, '\n');
    cin.ignore(100, '\n');
    for(int i = 0;des[i]<SIZE; ++i)
    {
        if(des[i] == ' ')
        {
            cout << "Do not enter spaces. " << endl;
            cout << "Enter Course Designator: " << endl;
            cin.get(des, SIZE, '\n');
        }
    }

    cout << "Enter section: " << endl;
    cin >> section;
    if(section <= 0)
    {
        cout << "Do not enter a negative number, Enter section: " << endl;
        cin >> section;
    }
    cin.ignore(100, '\n');

}

bool again()
{
    char user_input;
    user_input = toupper(user_input);
    cout << "Do you want to enter another class? (Y/N): " << endl;
    cin >> user_input;

    if(user_input != 'Y' || user_input != 'N')
    {
        cout << "Invalid selection, Please try again." << endl << endl;
        cout << "Do you want to continue to enroll classes? (y/n): " << endl;
        cin >> user_input;
    }
    if(user_input == 'Y')
        return true;
}

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

#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[], int & section);
bool again();


int main()
{
char first[SIZE];
char last[SIZE];
char des[SIZE];
int section = 0;
int crn = 0;

read_name(first, last);
do
{
read_course(crn, des, section);

cout << "Name: " << last << ", " << first << endl;
cout << "CRN: " <<crn <<endl<< "Destignator: " << des <<endl;
cout << "Section: " << section << endl;
}
while(again() == true); // Call again() function and compare its return value.
return 0;
}

void read_name(char first[], char last[])
{
cout << "Enter your first name then press enter: " << endl;
cin.get(first, SIZE, '\n');
cin.ignore(100, '\n');

cout << "Enter your last name then press enter: " << endl;
cin.get(last, SIZE, '\n');
cin.ignore(100, '\n');

}

void read_course(int & crn, char des[], int & section)
{
cout << "Enter valid CRN (e.g. 123456): " << endl;
cin >> crn;
if(crn <= 0)
{
cout << "Please enter a valid CRN: " << endl;
cin >> crn;
}
cin.ignore(100, '\n');

cout << "Enter Course Designator: " << endl;
cin.get(des, SIZE, '\n');
cin.ignore(100, '\n');
for(int i = 0; des[i]<SIZE; ++i)
{
if(des[i] == ' ')
{
cout << "Do not enter spaces. " << endl;
cout << "Enter Course Designator: " << endl;
cin.get(des, SIZE, '\n');
}
}

cout << "Enter section: " << endl;
cin >> section;
if(section <= 0)
{
cout << "Do not enter a negative number, Enter section: " << endl;
cin >> section;
}
cin.ignore(100, '\n');
}

bool again()
{
char user_input;
cout << "Do you want to enter another class? (Y/N): " << endl;
cin >> user_input; // Take input for user_input from user
user_input = toupper(user_input); // Convert it to uppercase

// Validate user_input. If it's not Y or N, keep asking user to enter a valid value
// DO this in a while loop
while(user_input != 'Y' && user_input != 'N')
{
cout << "Invalid selection, Please try again." << endl << endl;
cout << "Do you want to continue to enroll classes? (y/n): " << endl;
cin >> user_input;
user_input = toupper(user_input); // Convert it to uppercase
}
cin.ignore(100, '\n');

// If user_input is Y, return true else false
if(user_input == 'Y')
return true;
else
return false;
}

OUTPUT
EAPractice CheggC++\bin\Release\CheggC++.exe Enter your first name then press enter: Jane Enter your last name then press ent

Add a comment
Know the answer?
Add Answer to:
This is for a C++ program: I'm almost done with this program, I just need to...
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
  • I'm getting a segmentation fault for this function, this is for a c++ program. Is there...

    I'm getting a segmentation fault for this function, this is for a c++ program. Is there anything that you see that is obviously wrong? Am i using cin.get wrong? Please help if you can. number is my variable, and NUMBER is the const int limit void questions::addQuestion() {    int i = 0;    cout << "Enter the question number: " << endl; //after this line is where it seg faults    cin.get(number,NUMBER, '\n');    cin.ignore(100, '\n');    cout <<...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • Write a C++ program that uses a structure to store the following inventory information in a...

    Write a C++ program that uses a structure to store the following inventory information in a file: ⦁   Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁   Use a char array for item description and date. ⦁   The program should have a menu that allows the user to perform the following tasks: i.   Add a new record at the end of the file. ii.   Display any record in the file. iii.   Change any record...

  • 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);...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • 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;...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • i need help determining the output for these problems For ( int count = 1; count...

    i need help determining the output for these problems For ( int count = 1; count < 30; count+=9) Cout << (3 + count) << “ “ ; ________________ int I = 1;        While ( I <= 10)         {             If ( I > 5 && i ! = 9)                    Cout << ‘x’ ;              I++;         }       ____________________ double a [3] = {1.1, 2.2, 3.3};         cout << a [0] << “ “ << a...

  • Can you all fix this coding below, if so "ASAP" would be great? Write a program...

    Can you all fix this coding below, if so "ASAP" would be great? Write a program that prompts the user to input a string and outputs the string in uppercase letters using dynamic arrays. int main() {​ char *str; ​ ​ int strLen;​ int len;​ char ch;​ ​ int i;​ ​ cout << "Enter the size of the string: ";​ cin >> strLen;​ cout << endl;​ ​ cin.get(ch);​ ​ str = new char[strLen + 1];​ ​ cout << "Enter a...

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