I need help with my code

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<conio.h>
using namespace std;
class String
{
public:
    char a[20],b[20];
    int compare(char a[], char b[])
    {
        int i=0,j=0;
        while(a[i]!='\0')
        {
            i=i+1;
        }
        while(b[j]!='\0')
        {
            j=j+1;
        }
        if(i==j)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
};
int main()
{
    int k;
    String a1,a2;
    cout<<"Enter first string : ";
    cin>>a1.a;
    cout<<"\nEnter second string : ";
    cin>>a2.b;
    k=a1.compare(a1.a,a2.b);
    if(k==1)
    {
        cout<<;
    }
    else{
        cout<<;
    }
    return 0;
}


The question is :
Write a program that reads a person’s name in the following format: first name, then middle name or initial, and then last name. The program then outputs the name in the following format:
last name, first name. middle initial.
For example the input
Mary Average User
should produce the output
User, Mary A.
Your program should work the same and place a full stop after the middle initial even if the input did not contain a full stop. Your program should allow for users who give no middle name or initial. In that case, the output of courses contains no middle name or initial. For example, the input
Mary User
should produce the output
User, Mary
Your program should also accept names in lowercase, uppercase or a mix of lowercase and uppercase, and display that in the correct format, e.g. if the input is
mArY average USER
should produce the output
User, Mary A.
Use C-strings and assume that each name is at most 20 characters long.

I am still a newly coder , so please no critism , Can anyone help me? I still need to edit the last few lines with the "cout"
What has the posted code got to do with the question?????
I don't know how to write a 3 C-string :( , I was doing what I thought I could (which I thought was right) but after a while , I couldn't be able to let it do what I wanted to.
Well if you really have to use c-strings (why??), then perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cctype>

constexpr size_t maxstr {20};

char* toCapital(char* word)
{
	*word = static_cast<char>(std::toupper(static_cast<unsigned char>(*word)));

	for (auto c {word + 1}; *c; ++c)
		*c = static_cast<char>(std::tolower(static_cast<unsigned char>(*c)));

	return word;
}

int main()
{
	char first[maxstr] {}, mid[maxstr] {}, last[maxstr] {}, name[3 * maxstr] {};

	std::cout << "Enter full name (first [mid/init] last): ";
	std::cin.getline(name, 3 * maxstr);

	std::istringstream iss(name);

	iss >> std::setw(maxstr) >> first >> std::setw(maxstr) >> mid;
	if (!(iss >> std::setw(maxstr) >> last)) {
		strcpy(last, mid);
		*mid = 0;
	}

	std::cout << toCapital(last) << ", " << toCapital(first);
	if (*mid)
		std::cout << ' ' << *toCapital(mid) << '.';

	std::cout << '\n';
}



Enter full name (first [mid/init] last): Mary Average User
User, Mary A.

Enter full name (first [mid/init] last): Mary User
User, Mary

Enter full name (first [mid/init] last): mArY average USER
User, Mary A.

Last edited on
ok thank you seeplus , and they want us to use a 3 C-string.

so one question what does that

std::cout part mean? like can't you just type cout? or is it cause you using a string?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cctype>
using namespace std;


char *title( char *name )
{
   *name = toupper( *name );
   for ( char *p = name + 1; *p; p++ ) *p = tolower( *p );
   return name;
}


int main()
{
   const int SIZE = 63;
   char A[SIZE] = { 0 };
   cout << "Enter a name: ";
   cin.getline( A, SIZE, '\n' );
   int nwords = 0;
   char *start[3];
   bool space = true;
   for ( char *p = A; *p; p++ )
   {
      if ( space && isalpha( *p ) ) 
      {
         start[nwords] = p;
         nwords++;
         space = false;
      }
      else if ( !isalpha( *p ) )
      {
         *p = '\0';
         space = true;
      }
   }            
   if ( nwords == 2 ) cout << title( start[1] ) << ", " << title( start[0] ) << '\n';
   else               cout << title( start[2] ) << ", " << title( start[0] ) << " " << (char)toupper( *start[1] ) << ".\n";
}


Enter a name: mArY average USER
User, Mary A.
Last edited on
std::cout part mean? like can't you just type cout? or is it cause you using a string?


cout is part of the std::namespace. If you have:

 
using namespace std;


then you can leave off the std:: part. If you don't have using... (as in my code above) then you need to specify std:: before.
Last edited on
Or without using istringstream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <utility>
#include <cctype>
#include <cstring>

char* toCapital(char* word)
{
	*word = static_cast<char>(std::toupper(static_cast<unsigned char>(*word)));

	for (auto c {word + 1}; *c; ++c)
		*c = static_cast<char>(std::tolower(static_cast<unsigned char>(*c)));

	return word;
}

int main()
{
	constexpr size_t maxstr {63};
	const char* const delims {" \n\t\r"};
	char name[maxstr] {};

	std::cout << "Enter full name (first [mid/init] last): ";
	std::cin.getline(name, maxstr);

	char* start[3] {std::strtok(name, delims), std::strtok(nullptr, delims), std::strtok(nullptr, delims)};

	if (start[2] == nullptr)
		std::swap(start[1], start[2]);

	std::cout << toCapital(start[0]) << ", " << toCapital(start[2]);
	if (start[1])
		std::cout << ' ' << *toCapital(start[1]) << '.';

	std::cout << '\n';
}

trying to clean up names is a lost cause lol.
Connor McCloud //will you screw up his name to Mccloud?
or Patty O'Furniture?
I suspect the professor will take just first letter caps solution but its a deep and dark rabbit hole when you get into international names, even when converted to english like names.
Last edited on
Topic archived. No new replies allowed.