question archive Consider the following C++ program

Consider the following C++ program

Subject:Computer SciencePrice: Bought3

Consider the following C++ program. It has two user defined functions called "GetConcert" and "GetNumTickets". If you look at the code in these functions, you will see that they print some messages, read some input from the user, and return the value of a variable.

#include <iostream>

using namespace std;

char GetConcert()

{   

char Concert;  

 cout << "The following concerts are available:n";   

cout << "     B for Beyoncen";

   cout << "     L for Lady Gagan";  

 cout << "     T for Taylor Swiftn";  

 cout << "Enter the letter for the concert you want:n";

   cin >> Concert;   return Concert;

}

int GetNumTickets()

{  

 int NumTickets;  

 cout << "Enter the number of tickets you want:n";  

 cin >> NumTickets;  

 while ((NumTickets < 0) || (NumTickets > 10))   

{      if (NumTickets < 0)       

  cout << "You can not sell tickets here.n";      

else if (NumTickets > 10)        

 cout << "You may not purchase more than 10 tickets.n";   

   cout << "Enter the number of tickets you want:n";     

 cin >> NumTickets;  

 }   

return NumTickets;

}

int main()

{  

 // Declare variables   

// Call function to find out the concert they want to attend   

// Call function to find out how many tickets they want   

// Print out the information you have collected.   

cout << "nThe customer has placed the following order:n";

   cout << "Concert: " << Concert << endl;  

 cout << "Number of Tickets: " << NumTickets << endl;  

 return 0;

}

 

 

Step 1: Copy this program into your C++ program editor, and compile it. You should see two error messages about variables "not declared in this scope". This is because we are using "Concert" and "NumTickets" inside main, but they are declared in another function. This is not allowed in C++. Variables can only be accessed in the function they are defined in.

 

Step 2: Declare two new variables called "Concert" and "NumTickets" in main, and recompile the program. You will see that the error messages have gone away. C++ does allow us to reuse variable names in different functions, which is a good thing because we would run out of good names otherwise. If you run your program, you will see that it simply prints out your initial values for these variables.

 

Step 3: Modify your program again to call the "GetConcert" function and save the return value in "Concert". Now when you run your program you should see a message asking the user for input, and your program should output the value they enter.

 

Step 4: Modify your program again to call the "GetNumTickets" function and save the return value in "NumTickets". Now when you run your program you should see two messages asking the user for input, and your program should output the value they enter.

 

Step 5: As you can see, the function "GetConcert" is supposed to return 'B', 'L', or 'T' to indicate the concert choice. Unfortunately, if the user enters 'X' the function will just return this character. Your next task is to modify the GetConcert function to perform error checking to ensure that the correct values are returned. If the user enters an incorrect value, they should be prompted to try again.

 

Step 6: Compile and run your program one last time. Test your program with a variety of input values to verify that both functions are working properly. Once you think your program is working correctly, upload your final program into the auto grader by following the instructions below.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE