What is suppose to happen is that I am suppose to take a string like "Micheal Jordan" and reverse it and return it with a comma after the last name. like "Jordan, Micheal" but that is not what i am getting can you help please. I have also included the directions for this part as well.

The public String GetInfoLastNameFirst() method should create a first name string and a last name string from this.name. You must use a loop to create the first name string. You must use a loop to create the last name string. (HINT: we created new strings from old strings in lecture, exercises, and studio of September 22/23.) Then, similarly to the method GetAllInfo(), this method should return all the info about the candidate, but do so with the last name first, followed by a comma, followed by the first name, then tab, then party, then tab, then votes, then new line. You must use a loop, to create the first name and last name strings. (Clever coding can do it with one loop; straightforward coding uses two.)

/// <summary>
        /// reverses the first and last name and puts a comma in between
        /// </summary>
        /// <returns></returns>
        public String GetInfoLastNameFirst()
        {
            string FN = name;
            string LN = name;
            int i = 0;
            int j = 0;
            int location = name.IndexOf(" ");
            while (i < location)
            {
                
                FN = i.ToString() ;
                i++;
                
            }
            while (j > location)
            {
                LN = j.ToString();
                j++;
            }

            return LN + ',' + ' ' + FN + "\t" + this.party + "\t" + this.votes + "\r\n";
            
        }

What is suppose to happen is that I am suppose to take a string like "Micheal Jordan" and reverse it and return it with a comma after the last name. like "Jordan, Micheal" but that is not what i am getting can you help please. I have also included the directions for this part as well.

The public String GetInfoLastNameFirst() method should create a first name string and a last name string from this.name. You must use a loop to create the first name string. You must use a loop to create the last name string. (HINT: we created new strings from old strings in lecture, exercises, and studio of September 22/23.) Then, similarly to the method GetAllInfo(), this method should return all the info about the candidate, but do so with the last name first, followed by a comma, followed by the first name, then tab, then party, then tab, then votes, then new line. You must use a loop, to create the first name and last name strings. (Clever coding can do it with one loop; straightforward coding uses two.)

/// <summary>
/// reverses the first and last name and puts a comma in between
/// </summary>
/// <returns></returns>
public String GetInfoLastNameFirst()
{
string FN = name;
string LN = name;
int i = 0;
int j = 0;
int location = name.IndexOf(" ");
while (i < location)
{

FN = i.ToString() ;
i++;

}
while (j > location)
{
LN = j.ToString();
j++;
}

return LN + ',' + ' ' + FN + "\t" + this.party + "\t" + this.votes + "\r\n";

}

One, where does name come from? Is it a global variable or were you supposed to pass it the function? Two, what are the ToString and IndexOf functions? This line doesn't make sense.

FN = i.ToString() ;

You defined i as an int. int is not a class, so you can't use the dot. It looks like you are trying to use Java functions in C++. There is no String class in C++, unless you wrote it yourself.

public String GetInfoLastNameFirst()

There IS a String class in Java. Is this a Java question or a C++ question?

Here is the full code for my Candidate class

namespace HW3
{
    class Candidate
    {
        private String name;
        private String party;
        private int votes;

        public Candidate(String newName, String newParty, int newVotes)
        {
            this.name = newName;
            this.party = newParty;
            this.votes = newVotes;
        }

        /// <summary>
        /// formats all information about a candidate as a string
        /// </summary>
        /// <returns>all information about a candidate</returns>
        public String GetAllInfo()
        {
            return this.name + "\t" + this.party + "\t" + this.votes + "\r\n";
        }

        /// <summary>
        /// returns the candidate's name
        /// </summary>
        /// <returns>the candidate's name</returns>
        public String GetName()
        {
            return this.name;
        }

        /// <summary>
        /// returns the candidate's party ("Dem" or "Rep")
        /// </summary>
        /// <returns>the candidate's party</returns>
        public String GetParty()
        {
            return this.party;
        }

        /// <summary>
        /// returns the number of votes for a candidate
        /// </summary>
        /// <returns>the candidate's number of votes</returns>
        public int GetVotes()
        {
            return this.votes;
        }
        /// <summary>
        /// This updates the original votes with the newvotes and combine them
        /// </summary>
        /// <param name="newVotes"></param>
        public void AddVotes(int newVotes)
        {
            votes = votes + newVotes;
        }
        /// <summary>
        /// reverses the first and last name and puts a comma in between
        /// </summary>
        /// <returns></returns>
        public String GetInfoLastNameFirst()
        {
            string FN = name;
            string LN = name;
            int i = 0;
            int j = 0;
            int location = name.IndexOf(" ");
            while (i < location)
            {
                
                FN = i.ToString() ;
                i++;
                
            }
            while (j > location)
            {
                LN = j.ToString();
                j++;
            }

            return LN + ',' + ' ' + FN + "\t" + this.party + "\t" + this.votes + "\r\n";
            
        }
    }

}

yes i realize now that, that line makes no sense and it is in c++

Here is the full code for my Candidate class

namespace HW3
{
    class Candidate
    {
        private String name;
        private String party;
        private int votes;

        public Candidate(String newName, String newParty, int newVotes)
        {
            this.name = newName;
            this.party = newParty;
            this.votes = newVotes;
        }

        /// <summary>
        /// formats all information about a candidate as a string
        /// </summary>
        /// <returns>all information about a candidate</returns>
        public String GetAllInfo()
        {
            return this.name + "\t" + this.party + "\t" + this.votes + "\r\n";
        }

        /// <summary>
        /// returns the candidate's name
        /// </summary>
        /// <returns>the candidate's name</returns>
        public String GetName()
        {
            return this.name;
        }

        /// <summary>
        /// returns the candidate's party ("Dem" or "Rep")
        /// </summary>
        /// <returns>the candidate's party</returns>
        public String GetParty()
        {
            return this.party;
        }

        /// <summary>
        /// returns the number of votes for a candidate
        /// </summary>
        /// <returns>the candidate's number of votes</returns>
        public int GetVotes()
        {
            return this.votes;
        }
        /// <summary>
        /// This updates the original votes with the newvotes and combine them
        /// </summary>
        /// <param name="newVotes"></param>
        public void AddVotes(int newVotes)
        {
            votes = votes + newVotes;
        }
        /// <summary>
        /// reverses the first and last name and puts a comma in between
        /// </summary>
        /// <returns></returns>
        public String GetInfoLastNameFirst()
        {
            string FN = name;
            string LN = name;
            int i = 0;
            int j = 0;
            int location = name.IndexOf(" ");
            while (i < location)
            {
                
                FN = i.ToString() ;
                i++;
                
            }
            while (j > location)
            {
                LN = j.ToString();
                j++;
            }

            return LN + ',' + ' ' + FN + "\t" + this.party + "\t" + this.votes + "\r\n";
            
        }
    }

}

yes i realize now that, that line makes no sense and it is in c++

Well if it's C++, get rid of ToString and IndexOf and make String lower case if you didn't write these functions and didn't write a String class.

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/clibrary/cctype/


These libraries might contain some functions that are useful to you. I don't understand what you are trying to do in that function, but to make it understandable at all, get rid of all the Java-like function calls.

Well if it's C++, get rid of ToString and IndexOf and make String lower case if you didn't write these functions and didn't write a String class.

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/clibrary/cctype/


These libraries might contain some functions that are useful to you. I don't understand what you are trying to do in that function, but to make it understandable at all, get rid of all the Java-like function calls.

What i am trying to do is take a full name like "Adam Sandler" and break it up so that i can return it as "Sandler, Adam" but I don't know how to break it up so i was trying to count the letters until i got to the space in between "Adam Sandler" and then return that as FN (aka first name) and then make all char's after the space be LN (aka last name).

>i was trying to count the letters until i got to the space in between "Adam Sandler" and then
>return that as FN (aka first name) and then make all char's after the space be LN (aka last
>name).
That's a reasonable solution. It looks like you're using a non-standard string class, but this might help you figure out how to fix your code. It's based on your solution:

#include <iostream>
#include <string>

int main()
{
  std::string src = "Adam Sandler";
  std::string first;
  std::string last;
  std::string::size_type i = 0;

  while ( i < src.size() && src[i] != ' ' )
    first += src[i++];

  // Skip the whitespace
  while ( i < src.size() && src[i] == ' ' )
    ++i;

  while ( i < src.size() )
    last += src[i++];

  std::cout<< last <<", "<< first <<'\n';
}

thanks for your help i appriciate your honesty and your helpfullness, thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.