Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++, just a quick fix I hope. Outputs correcttly, then a line of jibberish and r

ID: 3692096 • Letter: C

Question

C++, just a quick fix I hope. Outputs correcttly, then a line of jibberish and repeats last name again, Please help.

/*
Description: Programming Challenge 10.7
Write a program that asks for the users first, middle, and last names. The names should be stored in three different character arrays. The program should then store, in a fourth array, the name arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. For example, if the user entered Carol Lynn Smith, it should store Smith, Carol Lynn in the fourth array. Display the contents of the fourth array on the screen.
*/

//including necessary libraries
#include<iostream>
#include <string>

using namespace std;

//runs program
int main()
{
   char first[20], middle[20], last[30], name[75];
   int i = 0, j = 0;
   cout << "Enter first name: ";
   cin >> first;
   cout << "Enter middle name: ";
   cin >> middle;
   cout << "Enter last name: ";
   cin >> last;

   while (last[i] != '')
   {
       name[j++] = last[i++];
   }
   name[j++] = ',';
   name[j++] = ' ';
   i = 0;

   while (first[i] != '')
   {
       name[j++] = first[i++];
   }
   name[j++] = ' ';
   i = 0;

   while (middle[i] != '')
   {
       name[j++] = middle[i++];
   }
   cout << "Your name is: " << name << endl;

   //allows user to get console
   system("pause");
   return 0;
}

Explanation / Answer

#include<iostream>
#include <string>

using namespace std;

int main()
{
    char first[20], middle[20], last[30], name[72];
    int i = 0, j = 0;
    cout << "Enter first name: ";
    cin >> first;
    cout << "Enter middle name: ";
    cin >> middle;
    cout << "Enter last name: ";
    cin >> last;

    while (last[i] != '')
    {
        name[j++] = last[i++];
    }
    name[j++] = ',';
    name[j++] = ' ';
    i = 0;

    while (first[i] != '')
    {
        name[j++] = first[i++];
    }
    name[j++] = ' ';
    i = 0;

    while (middle[i] != '')
    {
        name[j++] = middle[i++];
    }
    cout << "Your name is: " << name << endl;

    //allows user to get console
   system("pause");
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote