1

I'm working on this assignment for class and I've ran into a problem.

The objective of the program is to have the user to

  • Input their first then last name.
  • Then the program will ask to in put a nick name.
  • Finally the program will output your

    1. first name
    2. nick name all caps in quotes
    3. last name

a more detailed version of the instructions can be found here

So my problem lies on how will I manipulate a string to place the nickname in between the first and the last.

I'm using cin.getline() to input the first and last name at the beginning. I can't seem to find the last name in the string. Is there a way to read a string/char[] into another string after a certain location in the array.

like if there is a function in some c++ native library that went (pseudo code)

readFromPoint(array, otherArray, beginPoint, stopPoint);

or am i just going to have to build my own loop.

I would like to input my first and last name into one string. Then place them into two separate strings. Then I place (first, nickname, last) into the first and original string.

I know if I just used two cin statements it could save me a lot of heart ache. (I believe I've heard others call it dangerous)

Here's my code:

#include <iostream>
#include <cstring>
using namespace std;

char Name[46], // the full names leangth - 2
     firstName[16],
     lastName[16],
     nickName[16];
     int numOSpaces;

//void rearrangeName();
int FindSpace(string, int);
int FindNull(string);
int main(){

    cout << "Pleast enter your first then last name...\n";
    cin.getline(Name, 30);
    int ender =  FindSpace(Name, strlen(Name));

    // here we check if you entered the right amount of spaces
    while (numOSpaces > 1 || numOSpaces < 1){
        if (numOSpaces > 1)
        cout << "There are too many spaces only put one space:\n";
        if (numOSpaces < 1)
        cout << "Please separate your first and last name:\n";
        cin.getline(Name, 30);
        ender =  FindSpace(Name, strlen(Name));
    }

    cout << "Hello " << Name <<".\n";

    cout << "\n\nEnter your nickname...\n";\

    cin.getline(nickName, 30);
    cout << "Whats up " <<nickName << "!" << endl << endl;

    // Now we separate the first name and the last name 
    //rearrangeName();
}

// used to tell where user stopped inputting into the string
int FindNull(string find){
    int index = 0;
    do{
        index++;
    }while (isprint(find[index])); // this stops at the null terminator
//      }while (find[index] != '\0');  // as will this 
    return index;
}   

// here we find the first space 
int FindSpace(string mystring, int size){
    int Space;
    int spaceCount = 0;
    for (int index; index < size; index++){
        if (isspace(mystring[index])){
            Space = index;
            spaceCount++;
        }
    }
    numOSpaces = spaceCount;
    return Space;
}

void findName(string fullName, string otherName){

}

And I can't use any outside library of course if I want to turn this in that is. Only those native to C++.

2
  • 6
    Use std::string which is part of the C++ standard library, and then it all becomes easy. Using raw char arrays in C++ is a terrible idea. Commented Nov 8, 2015 at 17:42
  • 1
    char Name[46] et al. Why aren't you use std::string for those as well, as you're using it already for your function parameters? Commented Nov 8, 2015 at 17:44

3 Answers 3

2

Look at this program which does your job pretty well using std::string becuase of it's fancy functions like find, insert, etc :-

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string name, nickname;
    getline(cin,name);
    getline(cin,nickname);
    nickname.insert(0," ");     // insert a space before nickname
    int pos = name.find(" ");   // find where space is becuase space separates first & last name
    name.insert(pos, nickname);     // insert nickname at the space, it doesn't trouble because we have a space before nickname
    cout << name;
    return 0;
}

Output will be :-

Ankit Acharya
Programmer
Ankit Programmer Acharya 

Hope this will solve your problem pretty well !!

Sign up to request clarification or add additional context in comments.

Comments

0

If you use std::string, you'll have a much easier time.

#include <iostream>
#include <string>

void capitalize(std::string& str) {
  for(size_t i = 0; i < str.length(); ++i) {
    str[i] = toupper(str[i]);
  }
}

int main() {
  std::string first_name, last_name, nickname;
  std::cout << "Enter full name: " << std::endl;
  std::cin >> first_name >> last_name;
  std::cout << "Enter nickname: " << std::endl;
  std::cin >> nickname;

  capitalize(nickname);

  std::cout << first_name << " \"" << nickname << "\" " << last_name << std::endl;
}

Here's the ideone with full name "Hello World" and nickname "nickname".

Comments

0

The reason you are not getting the last name with cin.getline() is because it only gets the first string it sees, before the space. Repeat cin.getline() twice(or use the following code) to get both the first and last name:

std::string firstName, lastName;
std::cin >> firstName >> lastName;

This uses only native c++ stuff, included with <iostream> and <cstring>. If you need any help with converting the nickname to caps, let me know.

Edit: NEVER use c style strings unless you are forced to; all the tools you need are included in <string>.

1 Comment

That's not what getline does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.