c++ program with code

I have done one c++ program and I have another one very similar to the one that I did, but i could not do it. I have attached a file with the code that I did and its output. Also the file has the output that I want it to do at the end. thats mean the code works file for the first output, but i need to do it for the second ouput